From bcf4c15cb6fd1cb3e93d04d2e6b5b9090183c378 Mon Sep 17 00:00:00 2001 From: wyzula-jan Date: Sat, 3 Oct 2026 18:40:11 +0200 Subject: [PATCH] test(devices): cover laser delay PV failures in the middle of a move --- tests/tests_devices/test_laser_delay.py | 72 ++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/tests/tests_devices/test_laser_delay.py b/tests/tests_devices/test_laser_delay.py index b4a4a27..7e5ba9b 100644 --- a/tests/tests_devices/test_laser_delay.py +++ b/tests/tests_devices/test_laser_delay.py @@ -20,7 +20,7 @@ from bec_lib.devicemanager import DeviceConfigError from bec_server.device_server.devices.config_update_handler import ConfigUpdateHandler from bec_server.device_server.devices.device_serializer import get_device_base_class from bec_server.device_server.devices.devicemanager import DeviceManagerDS -from ophyd.signal import InternalSignalError +from ophyd.signal import ConnectionTimeoutError, InternalSignalError from ophyd.utils import LimitError from ophyd_devices.interfaces.base_classes.psi_device_base import DeviceStoppedError from ophyd_devices.tests.utils import MockPV, patched_device @@ -589,6 +589,76 @@ def test_destroy_fails_the_move_status_even_if_the_task_hangs(): assert wait_until(lambda: not device._hw_lock.locked()) +def fail_on_put(signal, call: int, exc: Exception) -> None: + """Make the ``call``-th put to ``signal`` raise ``exc`` (the IOC went away).""" + pv = signal._write_pv + original, count = pv.put, {"n": 0} + + def put(value, *args, **kwargs): + count["n"] += 1 + if count["n"] == call: + raise exc + return original(value, *args, **kwargs) + + pv.put = put + + +def test_pv_lost_mid_ramp_fails_the_move_and_keeps_the_phase_bookkeeping(calibrated): + """V2 stops answering in the middle of the ramp: the move fails with the EPICS error, the device + is idle again, the readback is NaN, the stored phase follows the last complete V1/V2 pair and + the next move (IOC back) works.""" + old_phase_t = calibrated.phase_t.get() + plan = calibrated.plan_move(-4.9) + log = record_puts(calibrated) + error = ConnectionTimeoutError("X10DA-ES4:AO-01 could not connect within 1.0 s") + fail_on_put(calibrated.phase_v2, 6, error) + status = calibrated.set(-4.9) + with pytest.raises(ConnectionTimeoutError): + status.wait(timeout=5) + assert wait_until(lambda: not calibrated.moving) + assert calibrated.motor_is_moving.get() == 0 + assert math.isnan(calibrated.readback.get()) + # DELAY, FINE, 5 complete pairs, then V1 of the 6th step (V2 failed) + assert [pv for pv, _ in log[:2]] == ["X10DA-ES2-TIM:SET1-DELAY", "X10DA-ES2-TIM:SET1-FINE"] + assert len(log) == 2 + 2 * 5 + 1 + offsets = phase_ramp_offsets(plan["phase_delta"], 2.0, 36) + assert calibrated.phase_t.get() == pytest.approx(old_phase_t + offsets[4]) + # the half-written pair leaves less than one ramp step of mismatch; phase_mismatch shows it + assert abs(calibrated.read_phase_mismatch()) < 2.0 / 36 + + calibrated.set(-4.9).wait(timeout=10) + assert calibrated.read()["laser_delay"]["value"] == -4.9 + assert calibrated.phase_t.get() == plan["phase_target"] + + +def test_delay_card_write_failure_leaves_the_phase_shifter_alone(calibrated): + old_phase_t = calibrated.phase_t.get() + log = record_puts(calibrated) + fail_on_put(calibrated.delay_fine, 1, ConnectionTimeoutError("SET1-FINE not connected")) + status = calibrated.set(-4.9) + with pytest.raises(ConnectionTimeoutError): + status.wait(timeout=5) + assert [pv for pv, _ in log] == ["X10DA-ES2-TIM:SET1-DELAY"] + assert calibrated.phase_t.get() == old_phase_t + assert math.isnan(calibrated.readback.get()) + assert calibrated.motor_is_moving.get() == 0 + + +def test_phase_read_failure_after_the_delay_card_fails_the_move(calibrated): + old_phase_t = calibrated.phase_t.get() + log = record_puts(calibrated) + with mock.patch.object( + calibrated.phase_v1, "get", side_effect=ConnectionTimeoutError("AO-02 not connected") + ): + status = calibrated.set(-4.9) + with pytest.raises(ConnectionTimeoutError): + status.wait(timeout=5) + assert [pv for pv, _ in log] == ["X10DA-ES2-TIM:SET1-DELAY", "X10DA-ES2-TIM:SET1-FINE"] + assert calibrated.phase_t.get() == old_phase_t + assert math.isnan(calibrated.readback.get()) + assert not calibrated.moving + + def test_stored_vs_readback_reference_when_voltages_were_touched(calibrated): calibrated.set(1.0).wait(timeout=10) # somebody turns the phase shifter by hand