-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
133 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Utilities for testing MQTT.""" | ||
import asyncio | ||
from collections.abc import Callable | ||
import datetime | ||
import json | ||
from unittest.mock import MagicMock, Mock | ||
|
||
from aiomqtt import Client | ||
|
||
from deebot_client.event_bus import EventBus | ||
from deebot_client.models import DeviceInfo | ||
from deebot_client.mqtt_client import MqttClient, SubscriberInfo | ||
|
||
|
||
async def verify_subscribe( | ||
test_client: Client, device_info: DeviceInfo, mock: Mock, *, expected_called: bool | ||
) -> None: | ||
command = "test" | ||
data = json.dumps({"test": str(datetime.datetime.now())}).encode("utf-8") | ||
topic = f"iot/atr/{command}/{device_info.did}/{device_info.get_class}/{device_info.resource}/j" | ||
await test_client.publish(topic, data) | ||
|
||
await asyncio.sleep(0.1) | ||
if expected_called: | ||
mock.assert_called_with(command, data) | ||
else: | ||
mock.assert_not_called() | ||
|
||
mock.reset_mock() | ||
|
||
|
||
async def subscribe( | ||
mqtt_client: MqttClient, device_info: DeviceInfo | ||
) -> tuple[Mock, Mock, Callable[[], None]]: | ||
events = Mock(spec=EventBus) | ||
callback = MagicMock() | ||
unsubscribe = await mqtt_client.subscribe( | ||
SubscriberInfo(device_info, events, callback) | ||
) | ||
await asyncio.sleep(0.1) | ||
return (events, callback, unsubscribe) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import asyncio | ||
from collections.abc import Generator | ||
import logging | ||
|
||
from aiomqtt import Client | ||
import pytest | ||
|
||
from deebot_client.models import DeviceInfo | ||
from deebot_client.mqtt_client import MqttClient, MqttConfiguration | ||
|
||
from .fixtures.mqtt_server import MqttServer | ||
from .mqtt_util import subscribe, verify_subscribe | ||
|
||
_WAITING_AFTER_RESTART = 30 | ||
|
||
|
||
@pytest.fixture | ||
def mqtt_server() -> Generator[MqttServer, None, None]: | ||
server = MqttServer() | ||
server.config.options["ports"] = {"1883/tcp": 54321} | ||
server.run() | ||
yield server | ||
server.stop() | ||
|
||
|
||
@pytest.mark.timeout(_WAITING_AFTER_RESTART + 10) | ||
async def test_client_reconnect_on_broker_error( | ||
mqtt_client: MqttClient, | ||
mqtt_server: MqttServer, | ||
device_info: DeviceInfo, | ||
mqtt_config: MqttConfiguration, | ||
caplog: pytest.LogCaptureFixture, | ||
) -> None: | ||
(_, callback, _) = await subscribe(mqtt_client, device_info) | ||
async with Client( | ||
hostname=mqtt_config.hostname, | ||
port=mqtt_config.port, | ||
identifier="Test-helper", | ||
tls_context=mqtt_config.ssl_context, | ||
) as client: | ||
# test client cannot be used as we restart the broker in this test | ||
await verify_subscribe(client, device_info, callback, expected_called=True) | ||
|
||
caplog.clear() | ||
mqtt_server.stop() | ||
await asyncio.sleep(0.1) | ||
|
||
assert ( | ||
"deebot_client.mqtt_client", | ||
logging.WARNING, | ||
"Connection lost; Reconnecting in 5 seconds ...", | ||
) in caplog.record_tuples | ||
caplog.clear() | ||
|
||
mqtt_server.run() | ||
|
||
expected_log_tuple = ( | ||
"deebot_client.mqtt_client", | ||
logging.DEBUG, | ||
"All mqtt tasks created", | ||
) | ||
for i in range(_WAITING_AFTER_RESTART): | ||
print(f"Wait for success reconnect... {i}/{_WAITING_AFTER_RESTART}") | ||
if expected_log_tuple in caplog.record_tuples: | ||
async with Client( | ||
hostname=mqtt_config.hostname, | ||
port=mqtt_config.port, | ||
identifier="Test-helper", | ||
tls_context=mqtt_config.ssl_context, | ||
) as client: | ||
# test client cannot be used as we restart the broker in this test | ||
await verify_subscribe( | ||
client, device_info, callback, expected_called=True | ||
) | ||
return | ||
|
||
await asyncio.sleep(1) | ||
|
||
pytest.fail("Reconnect failed") |