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 3.0 kB

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