From 70f91d3a1e7121b37ee0eb0eb69780866c022bd4 Mon Sep 17 00:00:00 2001 From: Mirko Holler Date: Sun, 22 Nov 2026 16:30:58 +0100 Subject: [PATCH] test(rt): count only RT data monitor warnings in test_rt_data_monitor The tests patched warning() on the shared BEC logger. With random test order a leftover WebpageGenerator thread logged its own warning meanwhile and test_slow_first_batch_keeps_scan_alive_while_controller_advances failed in CI. Record only messages starting with 'No RT ' and pass others through. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01M8zoDygkWk6LupmHh3tyET --- tests/tests_devices/test_rt_data_monitor.py | 38 ++++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/tests/tests_devices/test_rt_data_monitor.py b/tests/tests_devices/test_rt_data_monitor.py index 367b82d2..82fdb800 100644 --- a/tests/tests_devices/test_rt_data_monitor.py +++ b/tests/tests_devices/test_rt_data_monitor.py @@ -1,5 +1,6 @@ """RT progress warnings and timeouts use the status already polled by the flyer.""" +import contextlib import importlib import threading from collections import deque @@ -11,6 +12,27 @@ import pytest from csaxs_bec.devices.omny.rt import rt_ophyd +@contextlib.contextmanager +def rt_warnings(): + """Record only the RT data monitor's own warnings. + + rt_ophyd.logger is the shared BEC logger: background threads left over from other + tests (e.g. WebpageGenerator, random test order) can log warnings meanwhile and + must not count as monitor warnings. Other warnings pass through unchanged. + """ + recorder = mock.MagicMock() + original = rt_ophyd.logger.warning + + def warning(msg, *args, **kwargs): + if isinstance(msg, str) and msg.startswith("No RT "): + recorder(msg, *args, **kwargs) + else: + original(msg, *args, **kwargs) + + with mock.patch.object(rt_ophyd.logger, "warning", side_effect=warning): + yield recorder + + @pytest.fixture(params=[("flomni", "RtFlomni"), ("lamni", "RtLamni"), ("omny", "RtOMNY")]) def flyer(request, monkeypatch): suffix, class_prefix = request.param @@ -75,7 +97,7 @@ def run_readout(flyer, clock, states, *, finish=False, failure=False): @pytest.mark.parametrize("mode", [2, 3]) def test_warns_once_at_configured_timeout_without_samples(flyer, clock, mode): - with mock.patch.object(rt_ophyd.logger, "warning") as warning: + with rt_warnings() as warning: run_readout(flyer, clock, [(0, mode, 0), (2.999, mode, 0)]) warning.assert_not_called() @@ -87,7 +109,7 @@ def test_warns_once_at_configured_timeout_without_samples(flyer, clock, mode): def test_samples_rearm_warning_and_new_scan_resets_timeout(flyer, clock): - with mock.patch.object(rt_ophyd.logger, "warning") as warning: + with rt_warnings() as warning: run_readout( flyer, clock, [(0, 2, 0), (3, 2, 0), (4, 2, 6), (6.999, 2, 6), (7, 2, 6), (20, 2, 6)] ) @@ -101,14 +123,14 @@ def test_samples_rearm_warning_and_new_scan_resets_timeout(flyer, clock): def test_available_samples_are_read_before_timeout_check(flyer, clock): - with mock.patch.object(rt_ophyd.logger, "warning") as warning: + with rt_warnings() as warning: run_readout(flyer, clock, [(0, 2, 0), (4, 2, 6), (6.999, 2, 6)]) warning.assert_not_called() assert flyer.data.set.call_count == 5 def test_finished_scan_drains_samples_without_stall_warning(flyer, clock): - with mock.patch.object(rt_ophyd.logger, "warning") as warning: + with rt_warnings() as warning: run_readout(flyer, clock, [(0, 2, 0), (70, 0, 3)], finish=True) warning.assert_not_called() assert flyer.data.set.call_count == 10 @@ -136,7 +158,7 @@ def test_stopped_device_does_not_warn(flyer, clock): flyer._data_monitor.reset() clock.now += 10 flyer.shutdown_event.set() - with mock.patch.object(rt_ophyd.logger, "warning") as warning: + with rt_warnings() as warning: status = mock.Mock() flyer.read_positions_from_sampler(status) status.set_finished.assert_called_once_with() @@ -146,7 +168,7 @@ def test_stopped_device_does_not_warn(flyer, clock): @pytest.mark.parametrize("mode", [2, 3]) def test_scan_timeout_fails_status_without_stopping_axes(flyer, clock, mode): flyer.scan_timeout = 8 - with mock.patch.object(rt_ophyd.logger, "warning") as warning: + with rt_warnings() as warning: status = run_readout( flyer, clock, [(0, mode, 0), (3, mode, 0), (7.999, mode, 0), (8, mode, 0)], failure=True ) @@ -186,7 +208,7 @@ def test_slow_first_batch_keeps_scan_alive_while_controller_advances(flyer, cloc flyer.data_timeout = 15 states = [(0, 2, 0), (11, 2, 1), (22, 2, 2), (33, 2, 3), (44, 2, 4)] states += [(55, 2, 5), (60, 2, 5), (66, 2, 6), (77, 0, 10)] - with mock.patch.object(rt_ophyd.logger, "warning") as warning: + with rt_warnings() as warning: run_readout(flyer, clock, states, finish=True) warning.assert_not_called() assert flyer.data.set.call_count == 10 @@ -221,6 +243,6 @@ def test_timeouts_can_be_disabled_independently(flyer, clock, data_timeout, scan flyer.data_timeout = data_timeout flyer.scan_timeout = scan_timeout states = [(0, 2, 0), (3, 2, 0), (8, 2, 0), (70, 2, 0), (71, 0, 10)] - with mock.patch.object(rt_ophyd.logger, "warning") as warning: + with rt_warnings() as warning: run_readout(flyer, clock, states, finish=not fails, failure=fails) assert warning.call_count == int(warns)