diff --git a/meshtastic/tcp_interface.py b/meshtastic/tcp_interface.py index 670335c..ee17e97 100644 --- a/meshtastic/tcp_interface.py +++ b/meshtastic/tcp_interface.py @@ -105,6 +105,7 @@ class TCPInterface(StreamInterface): except OSError as e: logger.error(f"Socket send error, reconnecting: {e}") self._reconnect() + raise def _readBytes(self, length) -> Optional[bytes]: """Read an array of bytes from our stream""" diff --git a/meshtastic/tests/test_tcp_interface.py b/meshtastic/tests/test_tcp_interface.py index 60bee57..5857a7c 100644 --- a/meshtastic/tests/test_tcp_interface.py +++ b/meshtastic/tests/test_tcp_interface.py @@ -86,9 +86,9 @@ def test_TCPInterface_reconnect(): iface = TCPInterface(hostname="localhost", noProto=True) old_socket = iface.socket assert old_socket is not None - + iface._reconnect() - + assert old_socket.close.called # We expect socket class to be instantiated at least twice (init + reconnect) assert mock_socket.call_count >= 2 @@ -96,13 +96,14 @@ def test_TCPInterface_reconnect(): @pytest.mark.unit def test_TCPInterface_writeBytes_reconnects(): - """Test that _writeBytes calls _reconnect on OSError""" + """Test that _writeBytes reconnects and re-raises on OSError.""" with patch("socket.socket"): iface = TCPInterface(hostname="localhost", noProto=True) iface.socket.sendall.side_effect = OSError("Broken pipe") - - with patch.object(iface, '_reconnect') as mock_reconnect: - iface._writeBytes(b"some data") + + with patch.object(iface, "_reconnect") as mock_reconnect: + with pytest.raises(OSError, match="Broken pipe"): + iface._writeBytes(b"some data") mock_reconnect.assert_called_once() @@ -113,7 +114,7 @@ def test_TCPInterface_readBytes_reconnects(): iface = TCPInterface(hostname="localhost", noProto=True) # Mock the socket instance on the interface iface.socket.recv.return_value = b'' - + with patch.object(iface, '_reconnect') as mock_reconnect: iface._readBytes(10) mock_reconnect.assert_called_once()