Spaces:
Running
Running
test: integration tests için analysis queue concurrency ve deduplication eklendi
Browse files- tests/test_analyze_queue.py +103 -0
tests/test_analyze_queue.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Ten visitors at once, through the real endpoints and queue.
|
| 2 |
+
|
| 3 |
+
The analysis itself is replaced by a stand-in that holds the CPU slot for
|
| 4 |
+
a moment, so this checks the wiring (queue positions, polling, cancel,
|
| 5 |
+
shared uploads) without running any model.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import asyncio
|
| 11 |
+
from collections import defaultdict
|
| 12 |
+
import io
|
| 13 |
+
import math
|
| 14 |
+
import struct
|
| 15 |
+
|
| 16 |
+
import httpx
|
| 17 |
+
import pytest
|
| 18 |
+
|
| 19 |
+
from app.main import app
|
| 20 |
+
from app.routes import analyze as route
|
| 21 |
+
from app.routes.analyze_schemas import AnalyzeResponse
|
| 22 |
+
from app.services import analysis_jobs as aj
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def _wav(seed: int) -> bytes:
|
| 26 |
+
"""A short, distinct WAV per visitor (distinct content, distinct job)."""
|
| 27 |
+
sr, n = 8000, 8000
|
| 28 |
+
frames = b"".join(struct.pack("<h", int(8000 * math.sin(2 * math.pi * (200 + seed) * i / sr))) for i in range(n))
|
| 29 |
+
header = b"RIFF" + struct.pack("<I", 36 + len(frames)) + b"WAVEfmt " + struct.pack("<IHHIIHH", 16, 1, 1, sr, sr * 2, 2, 16)
|
| 30 |
+
return header + b"data" + struct.pack("<I", len(frames)) + frames
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@pytest.fixture
|
| 34 |
+
def stub_pipeline(monkeypatch):
|
| 35 |
+
monkeypatch.setattr(aj, "cpu_gate", aj.CpuGate(1))
|
| 36 |
+
monkeypatch.setattr(route, "cpu_gate", aj.cpu_gate)
|
| 37 |
+
monkeypatch.setattr(aj, "result_cache", aj.ResultCache(8, 3600))
|
| 38 |
+
monkeypatch.setattr(route, "result_cache", aj.result_cache)
|
| 39 |
+
monkeypatch.setattr(aj, "job_store", aj.JobStore())
|
| 40 |
+
monkeypatch.setattr(route, "job_store", aj.job_store)
|
| 41 |
+
monkeypatch.setattr(route, "_analyze_rate_store", defaultdict(list))
|
| 42 |
+
|
| 43 |
+
async def fake_pipeline(request_id, content, *, progress, ticket, **_):
|
| 44 |
+
async with aj.cpu_gate.slot(ticket, progress):
|
| 45 |
+
progress.running("features")
|
| 46 |
+
await asyncio.sleep(0.05)
|
| 47 |
+
progress.set("features", aj.STEP_DONE)
|
| 48 |
+
progress.check()
|
| 49 |
+
return AnalyzeResponse(warnings=["stub"], errors=[])
|
| 50 |
+
|
| 51 |
+
monkeypatch.setattr(route, "_run_pipeline", fake_pipeline)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
async def _post(client: httpx.AsyncClient, seed: int, ip: str) -> httpx.Response:
|
| 55 |
+
return await client.post(
|
| 56 |
+
"/api/analyze/jobs",
|
| 57 |
+
data={"sourceType": "file"},
|
| 58 |
+
files={"file": (f"t{seed}.wav", io.BytesIO(_wav(seed)), "audio/wav")},
|
| 59 |
+
headers={"x-forwarded-for": ip},
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def test_ten_visitors_at_once_queue_and_finish(stub_pipeline) -> None:
|
| 64 |
+
async def scenario() -> None:
|
| 65 |
+
transport = httpx.ASGITransport(app=app)
|
| 66 |
+
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
|
| 67 |
+
responses = await asyncio.gather(*(_post(client, i, f"10.0.0.{i}") for i in range(10)))
|
| 68 |
+
assert all(r.status_code == 202 for r in responses)
|
| 69 |
+
ids = [r.json()["jobId"] for r in responses]
|
| 70 |
+
assert len(set(ids)) == 10
|
| 71 |
+
|
| 72 |
+
snaps = [(await client.get(f"/api/analyze/jobs/{i}")).json() for i in ids]
|
| 73 |
+
positions = sorted(s["queue"]["position"] for s in snaps if s["status"] == "queued")
|
| 74 |
+
assert positions == list(range(1, len(positions) + 1))
|
| 75 |
+
assert len(positions) >= 8
|
| 76 |
+
|
| 77 |
+
# One visitor gives up; the others keep their order.
|
| 78 |
+
cancelled = ids[-1]
|
| 79 |
+
assert (await client.delete(f"/api/analyze/jobs/{cancelled}")).status_code == 200
|
| 80 |
+
|
| 81 |
+
for _ in range(80):
|
| 82 |
+
snaps = [(await client.get(f"/api/analyze/jobs/{i}")).json() for i in ids]
|
| 83 |
+
if all(s["status"] in ("done", "error") for s in snaps):
|
| 84 |
+
break
|
| 85 |
+
await asyncio.sleep(0.05)
|
| 86 |
+
by_id = dict(zip(ids, snaps))
|
| 87 |
+
assert by_id[cancelled]["response"]["errors"] == ["cancelled"]
|
| 88 |
+
assert all(by_id[i]["status"] == "done" for i in ids[:-1])
|
| 89 |
+
|
| 90 |
+
health = (await client.get("/api/health")).json()
|
| 91 |
+
assert health["models"]["jobs"]["pending"] == 0
|
| 92 |
+
|
| 93 |
+
asyncio.run(scenario())
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def test_same_upload_twice_shares_one_job(stub_pipeline) -> None:
|
| 97 |
+
async def scenario() -> None:
|
| 98 |
+
transport = httpx.ASGITransport(app=app)
|
| 99 |
+
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
|
| 100 |
+
a, b = await asyncio.gather(_post(client, 1, "10.0.1.1"), _post(client, 1, "10.0.1.2"))
|
| 101 |
+
assert a.json()["jobId"] == b.json()["jobId"]
|
| 102 |
+
|
| 103 |
+
asyncio.run(scenario())
|