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.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import pytest
  2. from autogen_core import AgentId, DefaultSubscription, DefaultTopicId, TopicId, TypeSubscription
  3. from autogen_core.application import SingleThreadedAgentRuntime
  4. from autogen_core.exceptions import CantHandleException
  5. from test_utils import LoopbackAgent, MessageType
  6. def test_type_subscription_match() -> None:
  7. sub = TypeSubscription(topic_type="t1", agent_type="a1")
  8. assert sub.is_match(TopicId(type="t0", source="s1")) is False
  9. assert sub.is_match(TopicId(type="t1", source="s1")) is True
  10. assert sub.is_match(TopicId(type="t1", source="s2")) is True
  11. def test_type_subscription_map() -> None:
  12. sub = TypeSubscription(topic_type="t1", agent_type="a1")
  13. assert sub.map_to_agent(TopicId(type="t1", source="s1")) == AgentId(type="a1", key="s1")
  14. with pytest.raises(CantHandleException):
  15. _agent_id = sub.map_to_agent(TopicId(type="t0", source="s1"))
  16. @pytest.mark.asyncio
  17. async def test_non_default_default_subscription() -> None:
  18. runtime = SingleThreadedAgentRuntime()
  19. await LoopbackAgent.register(runtime, "MyAgent", LoopbackAgent, skip_class_subscriptions=True)
  20. runtime.start()
  21. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())
  22. await runtime.stop_when_idle()
  23. # Not subscribed
  24. agent_instance = await runtime.try_get_underlying_agent_instance(
  25. AgentId("MyAgent", key="default"), type=LoopbackAgent
  26. )
  27. assert agent_instance.num_calls == 0
  28. # Subscribed
  29. default_subscription = TypeSubscription("default", "MyAgent")
  30. await runtime.add_subscription(default_subscription)
  31. runtime.start()
  32. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())
  33. await runtime.stop_when_idle()
  34. assert agent_instance.num_calls == 1
  35. # Publish to a different unsubscribed topic
  36. runtime.start()
  37. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId(type="other"))
  38. await runtime.stop_when_idle()
  39. assert agent_instance.num_calls == 1
  40. # Add a subscription to the other topic
  41. await runtime.add_subscription(TypeSubscription("other", "MyAgent"))
  42. runtime.start()
  43. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId(type="other"))
  44. await runtime.stop_when_idle()
  45. assert agent_instance.num_calls == 2
  46. # Remove the subscription
  47. await runtime.remove_subscription(default_subscription.id)
  48. # Publish to the default topic
  49. runtime.start()
  50. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())
  51. await runtime.stop_when_idle()
  52. assert agent_instance.num_calls == 2
  53. # Publish to the other topic
  54. runtime.start()
  55. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId(type="other"))
  56. await runtime.stop_when_idle()
  57. assert agent_instance.num_calls == 3
  58. @pytest.mark.asyncio
  59. async def test_skipped_class_subscriptions() -> None:
  60. runtime = SingleThreadedAgentRuntime()
  61. await LoopbackAgent.register(runtime, "MyAgent", LoopbackAgent, skip_class_subscriptions=True)
  62. runtime.start()
  63. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())
  64. await runtime.stop_when_idle()
  65. # Not subscribed
  66. agent_instance = await runtime.try_get_underlying_agent_instance(
  67. AgentId("MyAgent", key="default"), type=LoopbackAgent
  68. )
  69. assert agent_instance.num_calls == 0
  70. @pytest.mark.asyncio
  71. async def test_subscription_deduplication() -> None:
  72. runtime = SingleThreadedAgentRuntime()
  73. agent_type = "MyAgent"
  74. # Test TypeSubscription
  75. type_subscription_1 = TypeSubscription("default", agent_type)
  76. type_subscription_2 = TypeSubscription("default", agent_type)
  77. await runtime.add_subscription(type_subscription_1)
  78. with pytest.raises(ValueError, match="Subscription already exists"):
  79. await runtime.add_subscription(type_subscription_2)
  80. # Test DefaultSubscription
  81. default_subscription = DefaultSubscription(agent_type=agent_type)
  82. with pytest.raises(ValueError, match="Subscription already exists"):
  83. await runtime.add_subscription(default_subscription)