@@ -35,13 +35,13 @@ async def on_send(self, message: MessageType, *, sender: Agent | None, recipient
35
35
return message
36
36
37
37
handler = DebugInterventionHandler ()
38
- router = SingleThreadedAgentRuntime (before_send = handler )
38
+ runtime = SingleThreadedAgentRuntime (before_send = handler )
39
39
40
- long_running = LoopbackAgent ("name" , router )
41
- response = router .send_message (MessageType (), recipient = long_running )
40
+ long_running = LoopbackAgent ("name" , runtime )
41
+ response = runtime .send_message (MessageType (), recipient = long_running )
42
42
43
43
while not response .done ():
44
- await router .process_next ()
44
+ await runtime .process_next ()
45
45
46
46
assert handler .num_messages == 1
47
47
assert long_running .num_calls == 1
@@ -54,13 +54,13 @@ async def on_send(self, message: MessageType, *, sender: Agent | None, recipient
54
54
return DropMessage # type: ignore
55
55
56
56
handler = DropSendInterventionHandler ()
57
- router = SingleThreadedAgentRuntime (before_send = handler )
57
+ runtime = SingleThreadedAgentRuntime (before_send = handler )
58
58
59
- long_running = LoopbackAgent ("name" , router )
60
- response = router .send_message (MessageType (), recipient = long_running )
59
+ long_running = LoopbackAgent ("name" , runtime )
60
+ response = runtime .send_message (MessageType (), recipient = long_running )
61
61
62
62
while not response .done ():
63
- await router .process_next ()
63
+ await runtime .process_next ()
64
64
65
65
with pytest .raises (MessageDroppedException ):
66
66
await response
@@ -76,15 +76,63 @@ async def on_response(self, message: MessageType, *, sender: Agent, recipient: A
76
76
return DropMessage # type: ignore
77
77
78
78
handler = DropResponseInterventionHandler ()
79
- router = SingleThreadedAgentRuntime (before_send = handler )
79
+ runtime = SingleThreadedAgentRuntime (before_send = handler )
80
80
81
- long_running = LoopbackAgent ("name" , router )
82
- response = router .send_message (MessageType (), recipient = long_running )
81
+ long_running = LoopbackAgent ("name" , runtime )
82
+ response = runtime .send_message (MessageType (), recipient = long_running )
83
83
84
84
while not response .done ():
85
- await router .process_next ()
85
+ await runtime .process_next ()
86
86
87
87
with pytest .raises (MessageDroppedException ):
88
88
await response
89
89
90
90
assert long_running .num_calls == 1
91
+
92
+ @pytest .mark .asyncio
93
+ async def test_intervention_raise_exception_on_send () -> None :
94
+
95
+ class InterventionException (Exception ):
96
+ pass
97
+
98
+ class ExceptionInterventionHandler (DefaultInterventionHandler ): # type: ignore
99
+ async def on_send (self , message : MessageType , * , sender : Agent | None , recipient : Agent ) -> MessageType | type [DropMessage ]: # type: ignore
100
+ raise InterventionException
101
+
102
+ handler = ExceptionInterventionHandler ()
103
+ runtime = SingleThreadedAgentRuntime (before_send = handler )
104
+
105
+ long_running = LoopbackAgent ("name" , runtime )
106
+ response = runtime .send_message (MessageType (), recipient = long_running )
107
+
108
+ while not response .done ():
109
+ await runtime .process_next ()
110
+
111
+ with pytest .raises (InterventionException ):
112
+ await response
113
+
114
+ assert long_running .num_calls == 0
115
+
116
+ @pytest .mark .asyncio
117
+ async def test_intervention_raise_exception_on_respond () -> None :
118
+
119
+ class InterventionException (Exception ):
120
+ pass
121
+
122
+ class ExceptionInterventionHandler (DefaultInterventionHandler ): # type: ignore
123
+ async def on_response (self , message : MessageType , * , sender : Agent , recipient : Agent | None ) -> MessageType | type [DropMessage ]: # type: ignore
124
+ raise InterventionException
125
+
126
+ handler = ExceptionInterventionHandler ()
127
+ runtime = SingleThreadedAgentRuntime (before_send = handler )
128
+
129
+ long_running = LoopbackAgent ("name" , runtime )
130
+ response = runtime .send_message (MessageType (), recipient = long_running )
131
+
132
+ while not response .done ():
133
+ await runtime .process_next ()
134
+
135
+ with pytest .raises (InterventionException ):
136
+ await response
137
+
138
+ assert long_running .num_calls == 1
0 commit comments