Files
detections/e2e/tests/test_streaming_video_upload.py
T
Oleksandr Bezdieniezhnykh 07c2afb62e [AZ-178] Add real-video streaming test, update e2e tests, mark task done
- Add tests/test_az178_realvideo_streaming.py: integration test that validates
  frame decoding begins while upload is still in progress using a real video fixture
- Add conftest.py: pytest plugin for per-test duration reporting
- Update e2e tests (async_sse, performance, security, streaming_video_upload, video)
  and run-tests.sh for updated test suite
- Move AZ-178 task to done/; add data/ to .gitignore (StreamingBuffer temp files)
- Update autopilot state to step 12 (Security Audit) for new feature cycle

Made-with: Cursor
2026-04-01 05:02:25 +03:00

112 lines
3.3 KiB
Python

"""
AZ-178: True streaming video detection — e2e tests.
Both tests upload video_test01.mp4 (12 MB), wait for the first SSE event,
then stop. The goal is to prove the service starts and produces detections,
not to process the whole file.
Run with: pytest e2e/tests/test_streaming_video_upload.py -s -v
"""
import json
import threading
import time
from pathlib import Path
import pytest
import sseclient
FIXTURES_DIR = Path(__file__).resolve().parent.parent / "fixtures"
_TIMEOUT = 5.0
_STOP_AFTER = 5
def _fixture_path(name: str) -> str:
p = FIXTURES_DIR / name
if not p.is_file():
pytest.skip(f"missing fixture {p}")
return str(p)
def _chunked_reader(path: str, chunk_size: int = 64 * 1024):
with open(path, "rb") as f:
while True:
chunk = f.read(chunk_size)
if not chunk:
break
yield chunk
def _start_sse_listener(http_client) -> tuple[list[dict], list[BaseException], threading.Event]:
events: list[dict] = []
errors: list[BaseException] = []
first_event = threading.Event()
def _listen():
try:
with http_client.get("/detect/stream", stream=True, timeout=_TIMEOUT + 2) as resp:
resp.raise_for_status()
for event in sseclient.SSEClient(resp).events():
if not event.data or not str(event.data).strip():
continue
events.append(json.loads(event.data))
if len(events) >= _STOP_AFTER:
first_event.set()
break
except BaseException as exc:
errors.append(exc)
finally:
first_event.set()
threading.Thread(target=_listen, daemon=True).start()
return events, errors, first_event
@pytest.mark.timeout(10)
def test_streaming_video_detections_appear_during_upload(warm_engine, http_client):
# Arrange
video_path = _fixture_path("video_test01.mp4")
events, errors, first_event = _start_sse_listener(http_client)
time.sleep(0.3)
# Act
r = http_client.post(
"/detect/video",
data=_chunked_reader(video_path),
headers={"X-Filename": "video_test01.mp4", "Content-Type": "application/octet-stream"},
timeout=8,
)
first_event.wait(timeout=_TIMEOUT)
# Assert
assert not errors, f"SSE thread error: {errors}"
assert r.status_code == 200
assert len(events) >= 1, "Expected at least one SSE event within 5s"
print(f"\n First {len(events)} SSE events:")
for e in events:
print(f" {e}")
@pytest.mark.timeout(10)
def test_non_faststart_video_still_works(warm_engine, http_client):
# Arrange
video_path = _fixture_path("video_test01.mp4")
events, errors, first_event = _start_sse_listener(http_client)
time.sleep(0.3)
# Act
r = http_client.post(
"/detect/video",
data=_chunked_reader(video_path),
headers={"X-Filename": "video_test01_plain.mp4", "Content-Type": "application/octet-stream"},
timeout=8,
)
first_event.wait(timeout=_TIMEOUT)
# Assert
assert not errors, f"SSE thread error: {errors}"
assert r.status_code == 200
assert len(events) >= 1, "Expected at least one SSE event within 5s"
print(f"\n First {len(events)} SSE events:")
for e in events:
print(f" {e}")