mirror of
https://github.com/azaion/detections.git
synced 2026-06-21 12:51:08 +00:00
Fixed stabilize e2e SSE event handling
This commit is contained in:
@@ -1,10 +1,7 @@
|
||||
import json
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
import sseclient
|
||||
|
||||
|
||||
def _ai_config_video() -> dict:
|
||||
@@ -31,50 +28,16 @@ def test_ft_p08_immediate_async_response(
|
||||
|
||||
@pytest.mark.timeout(30)
|
||||
def test_ft_p09_sse_event_delivery(
|
||||
warm_engine, http_client, jwt_token
|
||||
warm_engine, http_client, jwt_token, sse_events_until_terminal
|
||||
):
|
||||
media_id = f"event-{uuid.uuid4().hex}"
|
||||
channel_id = str(uuid.uuid4())
|
||||
body = _ai_config_video()
|
||||
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 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)
|
||||
collected.append(data)
|
||||
first_event.set()
|
||||
if data.get("mediaStatus") in ("AIProcessed", "Error"):
|
||||
break
|
||||
except BaseException as e:
|
||||
thread_exc.append(e)
|
||||
finally:
|
||||
connected.set()
|
||||
first_event.set()
|
||||
|
||||
th = threading.Thread(target=_listen, daemon=True)
|
||||
th.start()
|
||||
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
|
||||
collected = sse_events_until_terminal(channel_id, headers=auth_header, timeout=20)
|
||||
assert collected, "no SSE events received"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user