2026-11-10 23:56:50 +08:00
|
|
|
"""Every visual request is a separate finish-current admission boundary."""
|
2026-11-14 14:07:26 +08:00
|
|
|
|
2026-11-10 23:56:50 +08:00
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
|
import pytest
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2026-11-14 14:07:26 +08:00
|
|
|
@pytest.mark.parametrize("reason", ["window_closed", "explicit_cancel"])
|
|
|
|
|
async def test_structure_rechecks_admission_before_each_http_request(system, monkeypatch, reason):
|
|
|
|
|
from app.adapters.provider import client as providers
|
|
|
|
|
from app.adapters.provider import qwen
|
|
|
|
|
|
|
|
|
|
provider = providers.Provider(system.settings)
|
2026-11-10 23:56:50 +08:00
|
|
|
await provider.client.aclose()
|
2026-11-14 14:07:26 +08:00
|
|
|
requests = []
|
|
|
|
|
|
2026-11-10 23:56:50 +08:00
|
|
|
async def respond(request):
|
|
|
|
|
requests.append(request)
|
2026-11-14 14:07:26 +08:00
|
|
|
return httpx.Response(
|
|
|
|
|
200,
|
|
|
|
|
json={
|
|
|
|
|
"choices": [
|
|
|
|
|
{
|
|
|
|
|
"message": {
|
|
|
|
|
"content": json.dumps(
|
|
|
|
|
{"scene": "frame", "subjects": [], "ocr": [], "tags": []}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
provider.client = httpx.AsyncClient(transport=httpx.MockTransport(respond))
|
|
|
|
|
source = system.settings.asset_root / "video.mp4"
|
|
|
|
|
source.write_bytes(b"video")
|
|
|
|
|
work = system.settings.job_work_root / "sample"
|
|
|
|
|
work.mkdir()
|
|
|
|
|
|
|
|
|
|
async def inspect(*args):
|
|
|
|
|
return {"kind": "video", "duration_seconds": 2}
|
|
|
|
|
|
|
|
|
|
async def extract(argv, **kwargs):
|
|
|
|
|
Image.new("RGB", (8, 8), "red").save(argv[-1])
|
|
|
|
|
return b"", b"pts_time:0.5"
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(qwen, "inspect_media", inspect)
|
|
|
|
|
monkeypatch.setattr(qwen, "run_process", extract)
|
|
|
|
|
admissions = []
|
|
|
|
|
|
|
|
|
|
async def gate(phase, pool):
|
|
|
|
|
admissions.append((phase, pool))
|
|
|
|
|
if pool == "qwen_http" and requests:
|
|
|
|
|
raise RuntimeError(reason)
|
|
|
|
|
|
|
|
|
|
token = providers.current_phase_callback.set(gate)
|
2026-11-10 23:56:50 +08:00
|
|
|
try:
|
2026-11-14 14:07:26 +08:00
|
|
|
with pytest.raises(RuntimeError, match=reason):
|
|
|
|
|
await provider.run_cpu(
|
|
|
|
|
"media.structure", {"asset_id": "v"}, {"frame_count": 2}, {"v": source}, work
|
|
|
|
|
)
|
|
|
|
|
assert len(requests) == 1
|
|
|
|
|
assert len([x for x in admissions if x[1] == "qwen_http"]) == 2
|
|
|
|
|
assert not (work / "structure.json").exists()
|
2026-11-10 23:56:50 +08:00
|
|
|
finally:
|
|
|
|
|
providers.current_phase_callback.reset(token)
|
|
|
|
|
await provider.aclose()
|