Spaces:
Running on T4
Running on T4
Download backend/tests/integration/test_report_queue.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 5.93 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/integration/test_report_queue.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/tests/integration/test_report_queue.py
-
curl -L -o test_report_queue.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/integration/test_report_queue.py
5.93 kB
| """Queued reports (the default): POST .../swing-report stores the payload, a background worker builds the report and | |
| the owner is notified.""" | |
| import asyncio | |
| from app.modules.billing import entitlement | |
| from app.modules.swing_reports import queue | |
| from app.modules.swing_reports import service as report_service | |
| from tests.factories import set_session | |
| from tests.integration.test_swing_reports import PAYLOAD, load_session, make_account, make_session, payload | |
| def url(session_id) -> str: | |
| return f"/v1/sessions/{session_id}/swing-report" | |
| async def workers_done() -> None: | |
| """Wait for every worker task. queue.WORKERS is not enough: a worker leaves it before its last look at the queue, | |
| and may still build one report after that.""" | |
| while tasks := [t for t in asyncio.all_tasks() if getattr(t.get_coro(), "__name__", "") == "_drain"]: | |
| await asyncio.gather(*tasks) | |
| async def results(client, headers) -> list[dict]: | |
| items = (await client.get("/v1/notifications", headers=headers)).json()["items"] | |
| return [n for n in items if n["kind"] == "result"] | |
| async def test_many_swings_are_queued_built_and_each_notified(client): | |
| owner, headers = await make_account() | |
| sids = [await make_session(owner) for _ in range(5)] | |
| for sid in sids: | |
| response = await client.post(url(sid), json=PAYLOAD, headers=headers) | |
| assert response.status_code == 202, response.text | |
| assert response.json() == {"session_id": str(sid), "status": "queued"} | |
| await workers_done() | |
| for sid in sids: | |
| row = await load_session(sid) | |
| assert row.status == "done" and row.report["overall_score"] == 78 | |
| assert row.measurement_payload == PAYLOAD and row.queued_payload is None and row.analyzed_at is not None | |
| detail = (await client.get(f"/v1/sessions/{sid}", headers=headers)).json()["session"] | |
| assert detail["status"] == "done" and detail["report_error"] is None | |
| notes = await results(client, headers) | |
| assert {n["target"]["session_id"] for n in notes} == {str(s) for s in sids} | |
| assert all(n["title"] == "Your swing report is ready" and n["target"]["outcome"] == "ready" for n in notes) | |
| async def test_queued_session_reads_as_queued_until_built(client, monkeypatch): | |
| monkeypatch.setattr(queue, "kick", lambda: None) # no worker yet | |
| owner, headers = await make_account() | |
| sid = await make_session(owner) | |
| assert (await client.post(url(sid), json=PAYLOAD, headers=headers)).status_code == 202 | |
| detail = (await client.get(f"/v1/sessions/{sid}", headers=headers)).json()["session"] | |
| assert detail["status"] == "queued" and detail["report"] is None | |
| listed = (await client.get("/v1/sessions", headers=headers)).json()["items"] | |
| assert [s["status"] for s in listed] == ["queued"] | |
| monkeypatch.undo() | |
| queue.kick() # what startup does for work left by the last process | |
| await workers_done() | |
| assert (await load_session(sid)).status == "done" | |
| async def test_checks_still_answer_in_the_request(client): | |
| owner, headers = await make_account() | |
| sid = await make_session(owner) | |
| bad = await client.post(url(sid), json=payload(schema_version="9"), headers=headers) | |
| assert bad.status_code == 422 and bad.json()["error"]["code"] == "INVALID_PAYLOAD" | |
| _, other = await make_account() | |
| assert (await client.post(url(sid), json=PAYLOAD, headers=other)).status_code == 403 | |
| assert (await load_session(sid)).queued_payload is None | |
| async def test_reported_revision_is_not_queued_again(client): | |
| owner, headers = await make_account() | |
| sid = await make_session(owner) | |
| assert (await client.post(url(sid), params={"queue": "false"}, json=PAYLOAD, headers=headers)).status_code == 200 | |
| again = await client.post(url(sid), json=PAYLOAD, headers=headers) | |
| assert again.status_code == 202 and again.json()["status"] == "done" | |
| await workers_done() | |
| assert await results(client, headers) == [] # the synchronous call and the repeat notify nobody | |
| async def test_failed_build_is_reported_and_can_be_retried(client, monkeypatch): | |
| def broken(*args, **kwargs): | |
| raise RuntimeError("model down") | |
| monkeypatch.setattr(report_service, "build_report", broken) | |
| owner, headers = await make_account() | |
| bad, good = await make_session(owner), await make_session(owner) | |
| assert (await client.post(url(bad), json=PAYLOAD, headers=headers)).status_code == 202 | |
| await workers_done() | |
| detail = (await client.get(f"/v1/sessions/{bad}", headers=headers)).json()["session"] | |
| assert detail["status"] == "failed" and detail["report_error"] == "REPORT_FAILED" and detail["report"] is None | |
| [note] = await results(client, headers) | |
| assert note["title"] == "We couldn't finish your swing report" and note["target"]["outcome"] == "failed" | |
| monkeypatch.undo() # a failed build does not block the queue, and the same session can be posted again | |
| for sid in (good, bad): | |
| assert (await client.post(url(sid), json=PAYLOAD, headers=headers)).status_code == 202 | |
| await workers_done() | |
| assert [(await load_session(s)).status for s in (good, bad)] == ["done", "done"] | |
| assert (await load_session(bad)).report_error is None | |
| assert len(await results(client, headers)) == 3 | |
| async def test_queued_swings_reserve_the_plan(client, monkeypatch): | |
| monkeypatch.setattr(entitlement, "FREE_WEEKLY_LIMIT", 2) | |
| monkeypatch.setattr(queue, "kick", lambda: None) | |
| owner, headers = await make_account() | |
| first, second, third = [await make_session(owner) for _ in range(3)] | |
| for sid in (first, second): | |
| assert (await client.post(url(sid), json=PAYLOAD, headers=headers)).status_code == 202 | |
| over = await client.post(url(third), json=PAYLOAD, headers=headers) | |
| assert over.status_code == 402 and over.json()["error"]["code"] == "PLAN_LIMIT" | |
| await set_session(first, queued_payload=None) | |
| await set_session(second, queued_payload=None) | |