|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import hashlib |
| 4 | +import json |
| 5 | +import os |
| 6 | +import socket |
| 7 | +from concurrent.futures import ThreadPoolExecutor |
| 8 | +from pathlib import Path |
| 9 | +from threading import Event |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from loopx.control_plane import effect_runtime |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize("snapshot", [False, True], ids=["inline", "snapshot"]) |
| 17 | +@pytest.mark.parametrize( |
| 18 | + "result_bytes", |
| 19 | + [b"[" * 100_000 + b"0" + b"]" * 100_000, b'"\xff"'], |
| 20 | + ids=["excessive-depth", "invalid-utf8"], |
| 21 | +) |
| 22 | +def test_undecodable_response_is_ambiguous_without_redispatch( |
| 23 | + tmp_path: Path, monkeypatch, snapshot: bool, result_bytes: bytes, |
| 24 | +) -> None: |
| 25 | + monkeypatch.setattr(effect_runtime, "_runtime_dir", lambda: tmp_path) |
| 26 | + fingerprint = effect_runtime._runtime_fingerprint_for_request() |
| 27 | + finished = Event() |
| 28 | + received_requests = [] |
| 29 | + with socket.socket() as listener: |
| 30 | + listener.bind(("127.0.0.1", 0)) |
| 31 | + listener.listen() |
| 32 | + listener.settimeout(0.05) |
| 33 | + info = { |
| 34 | + "schema_version": effect_runtime.EFFECT_RUNTIME_INFO_SCHEMA_VERSION, |
| 35 | + "fingerprint": fingerprint, |
| 36 | + "pid": os.getpid(), |
| 37 | + "host": "127.0.0.1", |
| 38 | + "port": listener.getsockname()[1], |
| 39 | + "token": "fixture-token", |
| 40 | + } |
| 41 | + locator = effect_runtime._runtime_info_path(fingerprint) |
| 42 | + locator.write_text(json.dumps(info), encoding="utf-8") |
| 43 | + |
| 44 | + def serve_responses() -> None: |
| 45 | + while not finished.is_set(): |
| 46 | + try: |
| 47 | + connection = listener.accept()[0] |
| 48 | + except TimeoutError: |
| 49 | + continue |
| 50 | + with connection: |
| 51 | + connection.settimeout(2) |
| 52 | + request_bytes = b"" |
| 53 | + while b"\n" not in request_bytes: |
| 54 | + chunk = connection.recv(65536) |
| 55 | + if not chunk: |
| 56 | + raise AssertionError("request ended before its frame") |
| 57 | + request_bytes += chunk |
| 58 | + request = json.loads(request_bytes) |
| 59 | + received_requests.append(request) |
| 60 | + envelope = { |
| 61 | + "schema_version": effect_runtime.EFFECT_RUNTIME_RESPONSE_SCHEMA_VERSION, |
| 62 | + "request_id": request["request_id"], |
| 63 | + "ok": True, |
| 64 | + } |
| 65 | + response = json.dumps(envelope).encode()[:-1] + b',"result":' + result_bytes + b"}\n" |
| 66 | + if snapshot: |
| 67 | + Path(request["response_sink"]).write_bytes(response) |
| 68 | + envelope["result_ref"] = { |
| 69 | + "byte_count": len(response), |
| 70 | + "sha256": hashlib.sha256(response).hexdigest(), |
| 71 | + } |
| 72 | + response = json.dumps(envelope).encode() + b"\n" |
| 73 | + connection.sendall(response) |
| 74 | + |
| 75 | + with ThreadPoolExecutor(max_workers=1) as executor: |
| 76 | + server = executor.submit(serve_responses) |
| 77 | + try: |
| 78 | + with pytest.raises(effect_runtime.EffectRuntimeResponseAmbiguous) as error: |
| 79 | + effect_runtime.effect_runtime_request( |
| 80 | + "goal.checkpoint_read_context.commit" if snapshot else "turn_journal.write", |
| 81 | + {"operation_id": "fixture-operation"}, |
| 82 | + timeout=2, |
| 83 | + large_local_snapshot=snapshot, |
| 84 | + ) |
| 85 | + finally: |
| 86 | + finished.set() |
| 87 | + server.result(timeout=3) |
| 88 | + |
| 89 | + assert error.value.diagnostic_code == "runtime_response_ambiguous" |
| 90 | + assert len(received_requests) == 1 |
| 91 | + assert json.loads(locator.read_text(encoding="utf-8")) == info |
0 commit comments