mirror of
https://github.com/meshtastic/python.git
synced 2026-06-02 12:45:00 -04:00
Re-establish the OSError being raised to match former behavior, but still reconnect. TBD if this is quite the right approach.
This commit is contained in:
@@ -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"""
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user