Skip to content

Commit

Permalink
Improve test framework.
Browse files Browse the repository at this point in the history
  • Loading branch information
BenediktBurger committed Mar 13, 2024
1 parent e0a6852 commit c51d882
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
9 changes: 7 additions & 2 deletions pyleco/utils/pipe_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,13 @@ def _send_pipe_message(self, typ: PipeCommands, *content: bytes) -> None:
try:
self.socket.send_multipart((typ, *content))
except zmq.ZMQError as exc:
raise ConnectionRefusedError(f"Connection to the handler refused with '{exc}', "
"probably the handler stopped.")
if exc.errno == 128:
raise ConnectionRefusedError(
"Connection to the handler refused as the socket is closed. "
"Probably the handler stopped."
)
else:
raise

def send_message(self, message: Message) -> None:
if not message.sender:
Expand Down
34 changes: 31 additions & 3 deletions tests/utils/test_pipe_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
from pyleco.core.message import Message
from pyleco.test import FakeContext

from pyleco.utils.pipe_handler import LockedMessageBuffer, PipeHandler, CommunicatorPipe
from pyleco.utils.pipe_handler import LockedMessageBuffer, PipeHandler, CommunicatorPipe,\
PipeCommands

cid = b"conversation_id;" # conversation_id
header = b"".join((cid, b"mid", b"\x00"))
Expand Down Expand Up @@ -124,9 +125,36 @@ def test_length_of_buffer(message_buffer: LockedMessageBuffer, length: int):
assert len(message_buffer) == length


# Test CommunicatorPipe
class Test_CommunicatorPipe_send_pipe:
def test_send_pipe_message(self, communicator: CommunicatorPipe):
communicator.socket.send_multipart = MagicMock() # type: ignore[method-assign]
communicator._send_pipe_message(PipeCommands.LOCAL_COMMAND, b"abc")
# assert
communicator.socket.send_multipart.assert_called_once_with(
(PipeCommands.LOCAL_COMMAND, b"abc")
)

def test_raise_ConnectionError_on_zmq_error(self, communicator: CommunicatorPipe):
communicator.socket.send_multipart = MagicMock( # type: ignore[method-assign]
side_effect=zmq.ZMQError(128, "not a socket")
)
# act
with pytest.raises(ConnectionRefusedError):
communicator._send_pipe_message(PipeCommands.LOCAL_COMMAND, b"c")

def test_reraise_on_other_zmq_error(self, communicator: CommunicatorPipe):
communicator.socket.send_multipart = MagicMock( # type: ignore[method-assign]
side_effect=zmq.ZMQError(3, "different")
)
# act
with pytest.raises(zmq.ZMQError, match="different"):
communicator._send_pipe_message(PipeCommands.LOCAL_COMMAND, b"c")


# Test PipeHandler
@pytest.fixture
def pipe_handler():
def pipe_handler() -> PipeHandler:
"""With fake contexts, that is with a broken pipe."""
pipe_handler = PipeHandler(name="handler", context=FakeContext()) # type: ignore
return pipe_handler
Expand All @@ -144,7 +172,7 @@ def pipe_handler_pipe():


@pytest.fixture
def communicator(pipe_handler_pipe: PipeHandler):
def communicator(pipe_handler_pipe: PipeHandler) -> CommunicatorPipe:
"""Communicator of `pipe_handler_pipe`."""
return pipe_handler_pipe.get_communicator()

Expand Down

0 comments on commit c51d882

Please sign in to comment.