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_intervention.py 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import pytest
  2. from agnext.application import SingleThreadedAgentRuntime
  3. from agnext.core import AgentId
  4. from agnext.core.exceptions import MessageDroppedException
  5. from agnext.core.intervention import DefaultInterventionHandler, DropMessage
  6. from test_utils import LoopbackAgent, MessageType
  7. @pytest.mark.asyncio
  8. async def test_intervention_count_messages() -> None:
  9. class DebugInterventionHandler(DefaultInterventionHandler):
  10. def __init__(self) -> None:
  11. self.num_messages = 0
  12. async def on_send(self, message: MessageType, *, sender: AgentId | None, recipient: AgentId) -> MessageType:
  13. self.num_messages += 1
  14. return message
  15. handler = DebugInterventionHandler()
  16. runtime = SingleThreadedAgentRuntime(intervention_handler=handler)
  17. loopback = await runtime.register_and_get("name", LoopbackAgent)
  18. run_context = runtime.start()
  19. _response = await runtime.send_message(MessageType(), recipient=loopback)
  20. await run_context.stop()
  21. assert handler.num_messages == 1
  22. loopback_agent = await runtime.try_get_underlying_agent_instance(loopback, type=LoopbackAgent)
  23. assert loopback_agent.num_calls == 1
  24. @pytest.mark.asyncio
  25. async def test_intervention_drop_send() -> None:
  26. class DropSendInterventionHandler(DefaultInterventionHandler):
  27. async def on_send(self, message: MessageType, *, sender: AgentId | None, recipient: AgentId) -> MessageType | type[DropMessage]:
  28. return DropMessage
  29. handler = DropSendInterventionHandler()
  30. runtime = SingleThreadedAgentRuntime(intervention_handler=handler)
  31. loopback = await runtime.register_and_get("name", LoopbackAgent)
  32. run_context = runtime.start()
  33. with pytest.raises(MessageDroppedException):
  34. _response = await runtime.send_message(MessageType(), recipient=loopback)
  35. await run_context.stop()
  36. loopback_agent = await runtime.try_get_underlying_agent_instance(loopback, type=LoopbackAgent)
  37. assert loopback_agent.num_calls == 0
  38. @pytest.mark.asyncio
  39. async def test_intervention_drop_response() -> None:
  40. class DropResponseInterventionHandler(DefaultInterventionHandler):
  41. async def on_response(self, message: MessageType, *, sender: AgentId, recipient: AgentId | None) -> MessageType | type[DropMessage]:
  42. return DropMessage
  43. handler = DropResponseInterventionHandler()
  44. runtime = SingleThreadedAgentRuntime(intervention_handler=handler)
  45. loopback = await runtime.register_and_get("name", LoopbackAgent)
  46. run_context = runtime.start()
  47. with pytest.raises(MessageDroppedException):
  48. _response = await runtime.send_message(MessageType(), recipient=loopback)
  49. await run_context.stop()
  50. @pytest.mark.asyncio
  51. async def test_intervention_raise_exception_on_send() -> None:
  52. class InterventionException(Exception):
  53. pass
  54. class ExceptionInterventionHandler(DefaultInterventionHandler): # type: ignore
  55. async def on_send(self, message: MessageType, *, sender: AgentId | None, recipient: AgentId) -> MessageType | type[DropMessage]: # type: ignore
  56. raise InterventionException
  57. handler = ExceptionInterventionHandler()
  58. runtime = SingleThreadedAgentRuntime(intervention_handler=handler)
  59. long_running = await runtime.register_and_get("name", LoopbackAgent)
  60. run_context = runtime.start()
  61. with pytest.raises(InterventionException):
  62. _response = await runtime.send_message(MessageType(), recipient=long_running)
  63. await run_context.stop()
  64. long_running_agent = await runtime.try_get_underlying_agent_instance(long_running, type=LoopbackAgent)
  65. assert long_running_agent.num_calls == 0
  66. @pytest.mark.asyncio
  67. async def test_intervention_raise_exception_on_respond() -> None:
  68. class InterventionException(Exception):
  69. pass
  70. class ExceptionInterventionHandler(DefaultInterventionHandler): # type: ignore
  71. async def on_response(self, message: MessageType, *, sender: AgentId, recipient: AgentId | None) -> MessageType | type[DropMessage]: # type: ignore
  72. raise InterventionException
  73. handler = ExceptionInterventionHandler()
  74. runtime = SingleThreadedAgentRuntime(intervention_handler=handler)
  75. long_running = await runtime.register_and_get("name", LoopbackAgent)
  76. run_context = runtime.start()
  77. with pytest.raises(InterventionException):
  78. _response = await runtime.send_message(MessageType(), recipient=long_running)
  79. await run_context.stop()
  80. long_running_agent = await runtime.try_get_underlying_agent_instance(long_running, type=LoopbackAgent)
  81. assert long_running_agent.num_calls == 1