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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import pytest
  2. from autogen_core import (
  3. AgentId,
  4. DefaultSubscription,
  5. DefaultTopicId,
  6. SingleThreadedAgentRuntime,
  7. TopicId,
  8. TypeSubscription,
  9. )
  10. from autogen_core.exceptions import CantHandleException
  11. from autogen_test_utils import LoopbackAgent, MessageType
  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")) is False
  15. assert sub.is_match(TopicId(type="t1", source="s1")) is True
  16. assert sub.is_match(TopicId(type="t1", source="s2")) is 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 LoopbackAgent.register(runtime, "MyAgent", LoopbackAgent, skip_class_subscriptions=True)
  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(
  31. AgentId("MyAgent", key="default"), type=LoopbackAgent
  32. )
  33. assert agent_instance.num_calls == 0
  34. # Subscribed
  35. default_subscription = TypeSubscription("default", "MyAgent")
  36. await runtime.add_subscription(default_subscription)
  37. runtime.start()
  38. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())
  39. await runtime.stop_when_idle()
  40. assert agent_instance.num_calls == 1
  41. # Publish to a different unsubscribed topic
  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 == 1
  46. # Add a subscription to the other topic
  47. await runtime.add_subscription(TypeSubscription("other", "MyAgent"))
  48. runtime.start()
  49. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId(type="other"))
  50. await runtime.stop_when_idle()
  51. assert agent_instance.num_calls == 2
  52. # Remove the subscription
  53. await runtime.remove_subscription(default_subscription.id)
  54. # Publish to the default topic
  55. runtime.start()
  56. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())
  57. await runtime.stop_when_idle()
  58. assert agent_instance.num_calls == 2
  59. # Publish to the other topic
  60. runtime.start()
  61. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId(type="other"))
  62. await runtime.stop_when_idle()
  63. assert agent_instance.num_calls == 3
  64. @pytest.mark.asyncio
  65. async def test_skipped_class_subscriptions() -> None:
  66. runtime = SingleThreadedAgentRuntime()
  67. await LoopbackAgent.register(runtime, "MyAgent", LoopbackAgent, skip_class_subscriptions=True)
  68. runtime.start()
  69. await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())
  70. await runtime.stop_when_idle()
  71. # Not subscribed
  72. agent_instance = await runtime.try_get_underlying_agent_instance(
  73. AgentId("MyAgent", key="default"), type=LoopbackAgent
  74. )
  75. assert agent_instance.num_calls == 0
  76. @pytest.mark.asyncio
  77. async def test_subscription_deduplication() -> None:
  78. runtime = SingleThreadedAgentRuntime()
  79. agent_type = "MyAgent"
  80. # Test TypeSubscription
  81. type_subscription_1 = TypeSubscription("default", agent_type)
  82. type_subscription_2 = TypeSubscription("default", agent_type)
  83. await runtime.add_subscription(type_subscription_1)
  84. with pytest.raises(ValueError, match="Subscription already exists"):
  85. await runtime.add_subscription(type_subscription_2)
  86. # Test DefaultSubscription
  87. default_subscription = DefaultSubscription(agent_type=agent_type)
  88. with pytest.raises(ValueError, match="Subscription already exists"):
  89. await runtime.add_subscription(default_subscription)