Skip to content

Commit 40061a6

Browse files
committed
Stabilize rec manager async write tests
Add a small `_wait_until` polling helper and use it in recorder-manager tests that assert write calls or recorder removal after `write_frame`. This removes race-prone immediate assertions against asynchronous work, making timestamp and stable-camera-ID behavior tests deterministic.
1 parent 3f79790 commit 40061a6

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

‎tests/gui/test_rec_manager.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import time
4+
35
import numpy as np
46
import pytest
57

@@ -36,6 +38,16 @@ def current_frames(_active_cams_two):
3638
return frames
3739

3840

41+
def _wait_until(predicate, timeout: float = 1.0) -> None:
42+
deadline = time.monotonic() + timeout
43+
while time.monotonic() < deadline:
44+
if predicate():
45+
return
46+
time.sleep(0.01)
47+
48+
raise AssertionError("Condition was not reached before timeout")
49+
50+
3951
@pytest.mark.unit
4052
def test_start_all_creates_recorders_and_returns_run_dir(
4153
recording_settings, _active_cams_two, current_frames, patch_video_recorder, patch_build_run_dir
@@ -204,6 +216,7 @@ def test_write_frame_uses_given_timestamp(
204216
mgr.write_frame(cam0_id, frame, timestamp=123.0)
205217

206218
rec = mgr.recorders[cam0_id]
219+
_wait_until(lambda: len(rec.write_calls) > 0)
207220
assert rec.write_calls[-1][1] == 123.0
208221

209222

@@ -223,6 +236,7 @@ def test_write_frame_uses_time_when_timestamp_missing(
223236
mgr.write_frame(cam0_id, frame, timestamp=None)
224237

225238
rec = mgr.recorders[cam0_id]
239+
_wait_until(lambda: len(rec.write_calls) > 0)
226240
assert rec.write_calls[-1][1] == 999.0
227241

228242

@@ -238,6 +252,7 @@ def test_write_frame_removes_recorder_on_exception(
238252
rec.raise_on_write = True
239253

240254
mgr.write_frame(cam0_id, current_frames[cam0_id], timestamp=1.0)
255+
_wait_until(lambda: cam0_id not in mgr.recorders)
241256
assert cam0_id not in mgr.recorders
242257

243258

@@ -346,11 +361,14 @@ def test_recording_manager_uses_stable_camera_id_not_display_id(
346361
assert rec.frame_size == (480, 640)
347362

348363
mgr.write_frame(stable_id, frame, timestamp=123.0)
364+
_wait_until(lambda: len(rec.write_calls) > 0)
349365
assert len(rec.write_calls) == 1
350366
assert rec.write_calls[-1][1] == 123.0
351367

352368
# Display ID is GUI-only and must not route frames internally.
353369
mgr.write_frame(display_id, frame, timestamp=456.0)
370+
# No async work for unknown ID
371+
time.sleep(0.05)
354372
assert len(rec.write_calls) == 1
355373

356374

@@ -461,7 +479,7 @@ def test_write_frame_passes_timestamp_metadata(
461479
mgr.write_frame(cam0_id, frame, timestamp=123.0, timestamp_metadata=meta)
462480

463481
rec = mgr.recorders[cam0_id]
464-
assert len(rec.write_calls) == 1
482+
_wait_until(lambda: len(rec.write_calls) > 0)
465483

466484
written_frame, written_timestamp, written_metadata = rec.write_calls[0]
467485
assert written_frame is frame

0 commit comments

Comments
 (0)