[AZ-180] Refactor detection event handling and improve SSE support

- Updated the detection image endpoint to require a channel ID for event streaming.
- Introduced a new endpoint for streaming detection events, allowing clients to receive real-time updates.
- Enhanced the internal buffering mechanism for detection events to manage multiple channels.
- Refactored the inference module to support the new event handling structure.

Made-with: Cursor
This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-04-03 02:42:05 +03:00
parent 2c35e59a77
commit 8baa96978b
26 changed files with 819 additions and 413 deletions
+27 -17
View File
@@ -4,6 +4,7 @@ import time
import uuid
import pytest
import sseclient
def _ai_config_video() -> dict:
@@ -19,36 +20,43 @@ def test_ft_p08_immediate_async_response(
):
media_id = f"async-{uuid.uuid4().hex}"
body = _ai_config_image()
headers = {"Authorization": f"Bearer {jwt_token}"}
channel_id = str(uuid.uuid4())
headers = {"Authorization": f"Bearer {jwt_token}", "X-Channel-Id": channel_id}
t0 = time.monotonic()
r = http_client.post(f"/detect/{media_id}", json=body, headers=headers)
elapsed = time.monotonic() - t0
assert elapsed < 2.0
assert r.status_code == 200
assert r.json() == {"status": "started", "mediaId": media_id}
assert r.status_code == 202
@pytest.mark.timeout(10)
def test_ft_p09_sse_event_delivery(
warm_engine, http_client, jwt_token, sse_client_factory
warm_engine, http_client, jwt_token
):
media_id = f"sse-{uuid.uuid4().hex}"
channel_id = str(uuid.uuid4())
body = _ai_config_video()
headers = {"Authorization": f"Bearer {jwt_token}"}
auth_header = {"Authorization": f"Bearer {jwt_token}"}
post_headers = {**auth_header, "X-Channel-Id": channel_id}
collected: list[dict] = []
thread_exc: list[BaseException] = []
first_event = threading.Event()
connected = threading.Event()
def _listen():
try:
with sse_client_factory(media_id) as sse:
time.sleep(0.3)
for event in sse.events():
with http_client.get(
f"/detect/events/{channel_id}",
stream=True,
timeout=600,
headers=auth_header,
) as resp:
resp.raise_for_status()
connected.set()
for event in sseclient.SSEClient(resp).events():
if not event.data or not str(event.data).strip():
continue
data = json.loads(event.data)
if data.get("mediaId") != media_id:
continue
collected.append(data)
first_event.set()
if len(collected) >= 5:
@@ -56,13 +64,14 @@ def test_ft_p09_sse_event_delivery(
except BaseException as e:
thread_exc.append(e)
finally:
connected.set()
first_event.set()
th = threading.Thread(target=_listen, daemon=True)
th.start()
time.sleep(0.5)
r = http_client.post(f"/detect/{media_id}", json=body, headers=headers)
assert r.status_code == 200
connected.wait(timeout=5)
r = http_client.post(f"/detect/{media_id}", json=body, headers=post_headers)
assert r.status_code == 202
first_event.wait(timeout=5)
th.join(timeout=5)
assert not thread_exc, thread_exc
@@ -74,8 +83,9 @@ def test_ft_n04_duplicate_media_id_409(
):
media_id = "dup-test"
body = _ai_config_image()
headers = {"Authorization": f"Bearer {jwt_token}"}
r1 = http_client.post(f"/detect/{media_id}", json=body, headers=headers)
assert r1.status_code == 200
r2 = http_client.post(f"/detect/{media_id}", json=body, headers=headers)
headers1 = {"Authorization": f"Bearer {jwt_token}", "X-Channel-Id": str(uuid.uuid4())}
headers2 = {"Authorization": f"Bearer {jwt_token}", "X-Channel-Id": str(uuid.uuid4())}
r1 = http_client.post(f"/detect/{media_id}", json=body, headers=headers1)
assert r1.status_code == 202
r2 = http_client.post(f"/detect/{media_id}", json=body, headers=headers2)
assert r2.status_code == 409