Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion roborock/devices/transport/local_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ async def connect(self) -> None:
loop = get_running_loop()
protocol = _LocalProtocol(self._data_received, self._connection_lost)
try:
self._transport, self._protocol = await loop.create_connection(lambda: protocol, self._host, _PORT)
async with asyncio.timeout(_TIMEOUT):
self._transport, self._protocol = await loop.create_connection(lambda: protocol, self._host, _PORT)
self._is_connected = True
except OSError as e:
raise RoborockConnectionException(f"Failed to connect to {self._host}:{_PORT}") from e
Expand Down
17 changes: 17 additions & 0 deletions tests/devices/transport/test_local_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ async def test_connection_failure(local_channel: LocalChannel, mock_loop: Mock)
assert local_channel._is_connected is False


async def test_connection_timeout(local_channel: LocalChannel, mock_loop: Mock) -> None:
"""Test that a connection attempt to an unreachable host times out."""

async def never_connect(*args: object) -> None:
await asyncio.Event().wait()

mock_loop.create_connection.side_effect = never_connect

with (
patch("roborock.devices.transport.local_channel._TIMEOUT", 0.01),
pytest.raises(RoborockConnectionException, match="Failed to connect to 192.168.1.100:58867"),
):
await local_channel.connect()

assert not local_channel.is_connected


async def test_close_connection(local_channel: LocalChannel, mock_loop: Mock, mock_transport: Mock) -> None:
"""Test closing the connection."""
await local_channel.connect()
Expand Down
Loading