You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_subscription.py 2.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import pytest
  2. from autogen_core.application import SingleThreadedAgentRuntime
  3. from autogen_core.base import AgentId, TopicId
  4. from autogen_core.base.exceptions import CantHandleException
  5. from autogen_core.components import DefaultTopicId, TypeSubscription
  6. from test_utils import LoopbackAgent, MessageType
  7. def test_type_subscription_match() -> None:
  8. sub = TypeSubscription(topic_type="t1", agent_type="a1")
  9. assert sub.is_match(TopicId(type="t0", source="s1")) is False
  10. assert sub.is_match(TopicId(type="t1", source="s1")) is True
  11. assert sub.is_match(TopicId(type="t1", source="s2")) is True
  12. def test_type_subscription_map() -> None:
  13. sub = TypeSubscription(topic_type="t1", agent_type="a1")
  14. assert sub.map_to_agent(TopicId(type="t1", source="s1")) == AgentId(type="a1", key="s1")
  15. with pytest.raises(CantHandleException):
  16. _agent_id = sub.map_to_agent(TopicId(type="t0", source="s1"))
  17. @pytest.mark.asyncio
  18. async def test_non_default_default_subscription() -> None:
  19. runtime = SingleThreadedAgentRuntime()
  20. await runtime.register("MyAgent", LoopbackAgent)
  21. runtime.start()
  22. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())
  23. await runtime.stop_when_idle()
  24. # Not subscribed
  25. agent_instance = await runtime.try_get_underlying_agent_instance(
  26. AgentId("MyAgent", key="default"), type=LoopbackAgent
  27. )
  28. assert agent_instance.num_calls == 0
  29. # Subscribed
  30. default_subscription = TypeSubscription("default", "MyAgent")
  31. await runtime.add_subscription(default_subscription)
  32. runtime.start()
  33. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())
  34. await runtime.stop_when_idle()
  35. assert agent_instance.num_calls == 1
  36. # Publish to a different unsubscribed topic
  37. runtime.start()
  38. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId(type="other"))
  39. await runtime.stop_when_idle()
  40. assert agent_instance.num_calls == 1
  41. # Add a subscription to the other topic
  42. await runtime.add_subscription(TypeSubscription("other", "MyAgent"))
  43. runtime.start()
  44. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId(type="other"))
  45. await runtime.stop_when_idle()
  46. assert agent_instance.num_calls == 2
  47. # Remove the subscription
  48. await runtime.remove_subscription(default_subscription.id)
  49. # Publish to the default topic
  50. runtime.start()
  51. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())
  52. await runtime.stop_when_idle()
  53. assert agent_instance.num_calls == 2
  54. # Publish to the other topic
  55. runtime.start()
  56. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId(type="other"))
  57. await runtime.stop_when_idle()
  58. assert agent_instance.num_calls == 3