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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 DefaultSubscription, 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 LoopbackAgent.register(runtime, "MyAgent", LoopbackAgent, skip_class_subscriptions=True)
  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
  59. @pytest.mark.asyncio
  60. async def test_skipped_class_subscriptions() -> None:
  61. runtime = SingleThreadedAgentRuntime()
  62. await LoopbackAgent.register(runtime, "MyAgent", LoopbackAgent, skip_class_subscriptions=True)
  63. runtime.start()
  64. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())
  65. await runtime.stop_when_idle()
  66. # Not subscribed
  67. agent_instance = await runtime.try_get_underlying_agent_instance(
  68. AgentId("MyAgent", key="default"), type=LoopbackAgent
  69. )
  70. assert agent_instance.num_calls == 0
  71. @pytest.mark.asyncio
  72. async def test_subscription_deduplication() -> None:
  73. runtime = SingleThreadedAgentRuntime()
  74. agent_type = "MyAgent"
  75. # Test TypeSubscription
  76. type_subscription_1 = TypeSubscription("default", agent_type)
  77. type_subscription_2 = TypeSubscription("default", agent_type)
  78. await runtime.add_subscription(type_subscription_1)
  79. with pytest.raises(ValueError, match="Subscription already exists"):
  80. await runtime.add_subscription(type_subscription_2)
  81. # Test DefaultSubscription
  82. default_subscription = DefaultSubscription(agent_type=agent_type)
  83. with pytest.raises(ValueError, match="Subscription already exists"):
  84. await runtime.add_subscription(default_subscription)