Spaces:
Running on T4
Running on T4
File size: 5,931 Bytes
be69d1e e980186 be69d1e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | """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)
|