Skip to content

Commit c0f28da

Browse files
authored
chore: update pytest-asyncio and fix clean shutdown (#434)
* chore: update pytest-asyncio and fix clean shutdown * chore: add back generator exception handling * chore: fix lint errors
1 parent f076a51 commit c0f28da

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

poetry.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ requires = ["poetry-core==1.8.0"]
3939
build-backend = "poetry.core.masonry.api"
4040

4141
[tool.poetry.group.dev.dependencies]
42-
pytest-asyncio = "*"
42+
pytest-asyncio = ">=1.1.0"
4343
pytest = "*"
4444
pre-commit = ">=3.5,<5.0"
4545
mypy = "*"

roborock/mqtt/roborock_session.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ async def _run_task(self, start_future: asyncio.Future[None] | None) -> None:
125125
except Exception as err:
126126
# This error is thrown when the MQTT loop is cancelled
127127
# and the generator is not stopped.
128-
if "generator didn't stop" in str(err):
128+
if "generator didn't stop" in str(err) or "generator didn't yield" in str(err):
129129
_LOGGER.debug("MQTT loop was cancelled")
130130
return
131131
if start_future:
132132
_LOGGER.error("Uncaught error starting MQTT session: %s", err)
133133
start_future.set_exception(err)
134134
return
135-
_LOGGER.error("Uncaught error during MQTT session: %s", err)
135+
_LOGGER.exception("Uncaught error during MQTT session: %s", err)
136136

137137
self._healthy = False
138138
_LOGGER.info("MQTT session disconnected, retrying in %s seconds", self._backoff.total_seconds())
@@ -180,7 +180,7 @@ async def _process_message_loop(self, client: aiomqtt.Client) -> None:
180180
except asyncio.CancelledError:
181181
raise
182182
except Exception as e:
183-
_LOGGER.error("Uncaught exception in subscriber callback: %s", e)
183+
_LOGGER.exception("Uncaught exception in subscriber callback: %s", e)
184184

185185
async def subscribe(self, topic: str, callback: Callable[[bytes], None]) -> Callable[[], None]:
186186
"""Subscribe to messages on the specified topic and invoke the callback for new messages.

tests/mqtt/test_roborock_session.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Tests for the MQTT session module."""
22

33
import asyncio
4-
from collections.abc import Callable, Generator
4+
from collections.abc import AsyncGenerator, Callable
55
from queue import Queue
66
from typing import Any
77
from unittest.mock import AsyncMock, Mock, patch
@@ -32,14 +32,16 @@ def mqtt_server_fixture(mock_create_connection: None, mock_select: None) -> None
3232

3333

3434
@pytest.fixture(autouse=True)
35-
def mock_client_fixture(event_loop: asyncio.AbstractEventLoop) -> Generator[None, None, None]:
35+
async def mock_client_fixture() -> AsyncGenerator[None, None]:
3636
"""Fixture to patch the MQTT underlying sync client.
3737
3838
The tests use fake sockets, so this ensures that the async mqtt client does not
3939
attempt to listen on them directly. We instead just poll the socket for
4040
data ourselves.
4141
"""
4242

43+
event_loop = asyncio.get_running_loop()
44+
4345
orig_class = mqtt.Client
4446

4547
async def poll_sockets(client: mqtt.Client) -> None:
@@ -226,3 +228,4 @@ async def test_subscribe_failure() -> None:
226228
await session.subscribe("topic-1", subscriber1.append)
227229

228230
assert not subscriber1.messages
231+
await session.close()

0 commit comments

Comments
 (0)