fix(controller): fix retry_once for socket_put and socket_put_and_receive

This commit is contained in:
2026-06-25 17:32:14 +02:00
committed by Christian Appel
co-authored by Christian Appel
parent b93f6d6445
commit be32dceba0
2 changed files with 24 additions and 5 deletions
+8 -4
View File
@@ -135,10 +135,14 @@ class Controller(OphydObject):
Override this method in the derived class if necessary, especially if the response
needs to be parsed differently.
"""
self.socket_put(val)
if remove_trailing_chars:
return self._remove_trailing_characters(self.sock.receive().decode())
return self.socket_get()
try:
self.socket_put(val)
if remove_trailing_chars:
return self._remove_trailing_characters(self.sock.receive().decode())
return self.socket_get()
except Exception as exc:
logger.error(f"Error in socket_put_and_receive: {exc}")
raise ControllerCommunicationError from exc
def _remove_trailing_characters(self, var) -> str:
if len(var) > 1:
+16 -1
View File
@@ -5,7 +5,7 @@ import pytest
from bec_server.device_server.tests.utils import DMMock
from ophyd_devices.tests.utils import SocketMock
from ophyd_devices.utils.controller import Controller
from ophyd_devices.utils.controller import Controller, ControllerCommunicationError
from ophyd_devices.utils.socket import SocketIO, SocketSignal
@@ -211,3 +211,18 @@ def test_socket_signal_put(signal):
assert callback_read_buffer[0][signal.name]["value"] == "new_value"
assert callback_read_buffer[1][signal.name]["value"] == "new_value"
assert callback_value_buffer == [("new_value", "value2"), ("new_value", "new_value")]
def test_socket_put_and_receive_raises_controller_communication_error(controller):
"""Test that socket_put_and_receive raises ControllerCommunicationError on socket errors."""
controller.sock.buffer_recv = [b"\xbfhello", b"ok"]
# First receive will raise a UnicodeDecodeError, which should be caught and re-raised as ControllerCommunicationError
# second one will be successful
val = controller.socket_put_and_receive("test")
assert val == "ok"
# Second test: simulate a socket error that cannot be decoded
controller.sock.buffer_recv = [b"\xbfhello", b"\xbfworld"]
with pytest.raises(ControllerCommunicationError):
controller.socket_put_and_receive("test")