|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import threading |
| 5 | + |
| 6 | +from server.progress import ProgressManager as CodeProgressManager |
| 7 | +from server.qa_progress import ProgressManager as QAProgressManager |
| 8 | + |
| 9 | + |
| 10 | +def test_progress_manager_is_thread_safe() -> None: |
| 11 | + async def run() -> None: |
| 12 | + pm = CodeProgressManager() |
| 13 | + run_id = "run_test" |
| 14 | + await pm.start_run(run_id, {"kind": "code"}) |
| 15 | + queue = await pm.subscribe(run_id) |
| 16 | + |
| 17 | + init_event = await asyncio.wait_for(queue.get(), timeout=1) |
| 18 | + assert init_event["type"] == "init" |
| 19 | + |
| 20 | + errors: list[BaseException] = [] |
| 21 | + |
| 22 | + def publish() -> None: |
| 23 | + try: |
| 24 | + pm.publish_attempt(run_id, {"task_id": "t", "model": "m"}) |
| 25 | + except BaseException as exc: # pragma: no cover |
| 26 | + errors.append(exc) |
| 27 | + |
| 28 | + thread = threading.Thread(target=publish) |
| 29 | + thread.start() |
| 30 | + thread.join(timeout=2) |
| 31 | + assert not errors |
| 32 | + |
| 33 | + attempt_event = await asyncio.wait_for(queue.get(), timeout=1) |
| 34 | + assert attempt_event["type"] == "attempt" |
| 35 | + assert attempt_event["task_id"] == "t" |
| 36 | + |
| 37 | + def complete() -> None: |
| 38 | + pm.complete(run_id, {"ok": True}) |
| 39 | + |
| 40 | + thread = threading.Thread(target=complete) |
| 41 | + thread.start() |
| 42 | + thread.join(timeout=2) |
| 43 | + |
| 44 | + complete_event = await asyncio.wait_for(queue.get(), timeout=1) |
| 45 | + assert complete_event["type"] == "complete" |
| 46 | + |
| 47 | + await pm.unsubscribe(run_id, queue) |
| 48 | + assert run_id not in pm._runs |
| 49 | + |
| 50 | + asyncio.run(run()) |
| 51 | + |
| 52 | + |
| 53 | +def test_qa_progress_manager_is_thread_safe() -> None: |
| 54 | + async def run() -> None: |
| 55 | + pm = QAProgressManager() |
| 56 | + run_id = "qa_test" |
| 57 | + await pm.start_run(run_id, {"kind": "qa"}) |
| 58 | + queue = await pm.subscribe(run_id) |
| 59 | + |
| 60 | + init_event = await asyncio.wait_for(queue.get(), timeout=1) |
| 61 | + assert init_event["type"] == "init" |
| 62 | + |
| 63 | + def publish() -> None: |
| 64 | + pm.publish_attempt(run_id, {"question_number": 1, "model": "m"}) |
| 65 | + |
| 66 | + thread = threading.Thread(target=publish) |
| 67 | + thread.start() |
| 68 | + thread.join(timeout=2) |
| 69 | + |
| 70 | + attempt_event = await asyncio.wait_for(queue.get(), timeout=1) |
| 71 | + assert attempt_event["type"] == "attempt" |
| 72 | + assert attempt_event["question_number"] == 1 |
| 73 | + |
| 74 | + def fail() -> None: |
| 75 | + pm.fail(run_id, "boom") |
| 76 | + |
| 77 | + thread = threading.Thread(target=fail) |
| 78 | + thread.start() |
| 79 | + thread.join(timeout=2) |
| 80 | + |
| 81 | + error_event = await asyncio.wait_for(queue.get(), timeout=1) |
| 82 | + assert error_event["type"] == "error" |
| 83 | + assert error_event["message"] == "boom" |
| 84 | + |
| 85 | + await pm.unsubscribe(run_id, queue) |
| 86 | + assert run_id not in pm._runs |
| 87 | + |
| 88 | + asyncio.run(run()) |
0 commit comments