diff --git a/sfm/device_access/access.py b/sfm/device_access/access.py index 0f3f39b..4dcb3f0 100644 --- a/sfm/device_access/access.py +++ b/sfm/device_access/access.py @@ -121,7 +121,11 @@ class DeviceAccess: with self._sessions_guard: session = self._sessions.get(address.key) if session is not None and session.serial != serial: - session.hang_up() + if session.is_open: + raise WrongUnit( + f"a Series-4 line to {session.serial} is held at {address.key}; " + f"{serial} cannot be reached there -- POST /device/hangup first " + "if the modem has moved") session = None if session is None: session = Series4Session(address, serial, self._s4_factory, clock=self._clock) diff --git a/tests/test_device_access.py b/tests/test_device_access.py index 1e1c760..1a1de7a 100644 --- a/tests/test_device_access.py +++ b/tests/test_device_access.py @@ -79,12 +79,23 @@ def test_a_dropped_line_is_called_up_again_quietly(): assert st.series == 4 and second.opened == 1 -def test_a_different_serial_at_the_same_address_hangs_up_the_old_line(): +def test_a_different_serial_at_a_held_address_is_refused_without_io(): first, second = FakeMicromateClient(), FakeMicromateClient(serial="UM12947") da = access(s4=[first, second]) da.status(ADDR, "UM20147") + with pytest.raises(WrongUnit, match="hangup"): + da.status(ADDR, "UM12947") + assert first.closed == 0 and second.opened == 0 + assert da.held_serial(ADDR) == "UM20147" + + +def test_a_different_serial_replaces_a_session_that_is_no_longer_open(): + first, second = FakeMicromateClient(), FakeMicromateClient(serial="UM12947") + da = access(s4=[first, second]) + da.status(ADDR, "UM20147") + first.close() da.status(ADDR, "UM12947") - assert first.closed == 1 and second.opened == 1 + assert second.opened == 1 def test_a_busy_unit_answers_423_without_a_second_connection():