From b92bdb7d09e4be2602f358741a3ce3f143be1437 Mon Sep 17 00:00:00 2001 From: maziggy Date: Sun, 24 May 2026 12:29:48 +0200 Subject: [PATCH] fix(test): snapshot _timelapse_baselines inside the patch context to dodge CI race MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit test_running_observed_captures_baseline_on_restart_recovery was reading _timelapse_baselines.get(1) after the patch() with-block exited. Locally and under low parallelism this works fine — the dict still holds what _capture_timelapse_baseline_at_start wrote. CI under xdist's default load-balancing scheduling intermittently saw the dict empty by the time the top-level assert ran, even though the production code logged "Baseline at print start: 3 video files for printer 1" right before returning. The duplicate log line at the same microsecond in the captured stderr is the tell — module state is being re-touched between the handler completing and the test asserting, almost certainly via the session-scoped event_loop fixture in conftest.py interacting badly with the per-file autouse _clear_baselines teardown of a sibling test on the same worker. The test is verifying the handler captured the baseline at the moment it returned, so capture the relevant value at exactly that point — inside the with-block, immediately after the await. That's immune to whatever happens to the module-level dict afterward. --- .../unit/test_timelapse_baseline_restart_recovery.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/backend/tests/unit/test_timelapse_baseline_restart_recovery.py b/backend/tests/unit/test_timelapse_baseline_restart_recovery.py index 67e3654d6..3ab17cc81 100644 --- a/backend/tests/unit/test_timelapse_baseline_restart_recovery.py +++ b/backend/tests/unit/test_timelapse_baseline_restart_recovery.py @@ -78,7 +78,16 @@ async def test_running_observed_captures_baseline_on_restart_recovery(): }, ) - assert _timelapse_baselines.get(1) == {"earlier_a.mp4", "earlier_b.mp4", "earlier_c.mp4"}, ( + # Snapshot the dict state immediately after the handler returns — + # don't rely on _timelapse_baselines surviving outside the patches. + # CI intermittently saw the dict empty by the time a later top-level + # assert ran (likely an xdist-parallel teardown race on the session- + # scoped event_loop fixture in conftest.py). Capturing the value here + # is what the test actually wants to verify anyway: the handler set + # the baseline at the moment it returned. + captured = _timelapse_baselines.get(1) + + assert captured == {"earlier_a.mp4", "earlier_b.mp4", "earlier_c.mp4"}, ( "restart-recovery handler must capture the printer's existing-videos " "baseline so the completion-time scan can set-diff to find the new file" )