|
2 | 2 | from __future__ import annotations |
3 | 3 |
|
4 | 4 | import asyncio |
| 5 | +import contextlib |
5 | 6 | import json |
6 | 7 | import random |
| 8 | +import re |
7 | 9 | from datetime import datetime, timedelta, timezone |
8 | 10 | from zoneinfo import ZoneInfo |
9 | 11 |
|
@@ -1133,6 +1135,90 @@ def do_POST(self): |
1133 | 1135 | server.shutdown() |
1134 | 1136 |
|
1135 | 1137 |
|
| 1138 | +@contextlib.asynccontextmanager |
| 1139 | +async def _executor_answering_after(seconds: float): |
| 1140 | + """A loopback executor whose message/send, like a deployed one's, answers only once its task ends `seconds` later; |
| 1141 | + its task polls answer at once. Yields its URL.""" |
| 1142 | + from pytest_socket import enable_socket |
| 1143 | + |
| 1144 | + enable_socket() # re-disabled by the autouse fixture's next setup |
| 1145 | + handlers: set[asyncio.Task] = set() |
| 1146 | + |
| 1147 | + async def serve(reader, writer): |
| 1148 | + handlers.add(asyncio.current_task()) |
| 1149 | + try: |
| 1150 | + while True: # one connection carries the send and then the polls |
| 1151 | + head = await reader.readuntil(b"\r\n\r\n") |
| 1152 | + req = json.loads(await reader.readexactly(int(re.search(rb"(?i)content-length: *(\d+)", head)[1]))) |
| 1153 | + if req["method"] == "message/send": |
| 1154 | + await asyncio.sleep(seconds) |
| 1155 | + body = json.dumps({"jsonrpc": "2.0", "id": req["id"], "result": {"id": "task-1", "status": { |
| 1156 | + "state": "completed", "message": {"parts": [{"kind": "text", "text": "replied"}]}}}}).encode() |
| 1157 | + writer.write(b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s" |
| 1158 | + % (len(body), body)) |
| 1159 | + await writer.drain() |
| 1160 | + except (asyncio.IncompleteReadError, ConnectionError): |
| 1161 | + pass # the caller hung up |
| 1162 | + finally: |
| 1163 | + writer.close() |
| 1164 | + |
| 1165 | + server = await asyncio.start_server(serve, "127.0.0.1", 0) |
| 1166 | + try: |
| 1167 | + yield f"http://127.0.0.1:{server.sockets[0].getsockname()[1]}" |
| 1168 | + finally: |
| 1169 | + server.close() |
| 1170 | + for task in handlers: |
| 1171 | + task.cancel() |
| 1172 | + await asyncio.gather(*handlers, return_exceptions=True) |
| 1173 | + |
| 1174 | + |
| 1175 | +def _reply_then_post(url: str, timeout_seconds: float) -> dict: |
| 1176 | + return {"executor": {"a2a_url": url, "role": "executor", "timeout_seconds": timeout_seconds}, |
| 1177 | + "triggers": [{"id": "x", "when": {"type": "action", "tool": "t"}, |
| 1178 | + "actions": [{"type": "nl", "instruction": "reply to the client"}, |
| 1179 | + {"type": "tool", "tool": "slack_send_message", |
| 1180 | + "args": {"channel": "#c", "text": "the client replied"}}]}]} |
| 1181 | + |
| 1182 | + |
| 1183 | +async def _settled(engine, tid: str, within: float = 10) -> None: |
| 1184 | + for _ in range(int(within / 0.05)): |
| 1185 | + if engine._triggers[tid]["status"] not in ("firing", "queued"): |
| 1186 | + return |
| 1187 | + await asyncio.sleep(0.05) |
| 1188 | + raise AssertionError(f"trigger {tid!r} still {engine._triggers[tid]['status']} after {within}s") |
| 1189 | + |
| 1190 | + |
| 1191 | +@pytest.mark.asyncio |
| 1192 | +async def test_an_executor_slower_than_the_http_timeout_is_waited_out_and_the_later_actions_run(engine, monkeypatch): |
| 1193 | + """FD-5333: a contact slower than 30 s used to fail its fire on a read timeout, though its reply still landed.""" |
| 1194 | + monkeypatch.setattr(triggers_module, "_EXECUTOR_HTTP_TIMEOUT_S", 0.2) # stands in for the 30 s |
| 1195 | + async with _executor_answering_after(1.0) as url: |
| 1196 | + engine.register(_reply_then_post(url, timeout_seconds=10)) |
| 1197 | + engine.on_tool_call("default", "t", {}, _result()) |
| 1198 | + await asyncio.sleep(0.4) |
| 1199 | + assert engine._triggers["x"]["status"] == "firing" # past the HTTP timeout, still in flight |
| 1200 | + await _settled(engine, "x") |
| 1201 | + |
| 1202 | + assert engine._triggers["x"]["status"] == "fired", engine.state()["events"][-3:] |
| 1203 | + assert [e["action_index"] for e in engine._events if e["kind"] == "action_ok"] == [0, 1] |
| 1204 | + assert engine._test_gw.calls == [("slack_send_message", {"channel": "#c", "text": "the client replied"})] |
| 1205 | + |
| 1206 | + |
| 1207 | +@pytest.mark.asyncio |
| 1208 | +async def test_an_executor_slower_than_its_own_timeout_still_fails_the_fire(engine, monkeypatch): |
| 1209 | + monkeypatch.setattr(triggers_module, "_EXECUTOR_HTTP_TIMEOUT_S", 0.2) |
| 1210 | + async with _executor_answering_after(3.0) as url: |
| 1211 | + engine.register(_reply_then_post(url, timeout_seconds=1.0)) |
| 1212 | + engine.on_tool_call("default", "t", {}, _result()) |
| 1213 | + await asyncio.sleep(0.4) |
| 1214 | + assert engine._triggers["x"]["status"] == "firing" |
| 1215 | + await _settled(engine, "x", within=2.5) |
| 1216 | + |
| 1217 | + [failure] = [e for e in engine._events if e["kind"] == "action_failed"] |
| 1218 | + assert (failure["action_index"], failure["detail"]) == (0, "ReadTimeout") |
| 1219 | + assert engine._triggers["x"]["status"] == "armed" and engine._test_gw.calls == [] |
| 1220 | + |
| 1221 | + |
1136 | 1222 | @pytest.mark.asyncio |
1137 | 1223 | async def test_broken_check_is_fail_open(engine): |
1138 | 1224 | engine.register({"triggers": [ |
|
0 commit comments