-
Notifications
You must be signed in to change notification settings - Fork 57
Fix V1Channel reconnection by falling back to cache when MQTT fails #604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,89 @@ | ||
| import datetime | ||
| from unittest.mock import AsyncMock, Mock | ||
|
|
||
| import pytest | ||
|
|
||
| from roborock.data import NetworkInfo | ||
| from roborock.devices.cache import CacheData, InMemoryCache | ||
| from roborock.devices.local_channel import LocalSession | ||
| from roborock.devices.v1_channel import NETWORK_INFO_REFRESH_INTERVAL, V1Channel | ||
| from roborock.exceptions import RoborockException | ||
| from roborock.protocols.v1_protocol import SecurityData | ||
|
|
||
| from ..conftest import FakeChannel | ||
|
|
||
| TEST_DEVICE_UID = "abc123" | ||
| TEST_SECURITY_DATA = SecurityData(endpoint="test_endpoint", nonce=b"test_nonce") | ||
| TEST_IP = "192.168.1.100" | ||
|
|
||
|
|
||
| @pytest.fixture(name="mock_mqtt_channel") | ||
| async def setup_mock_mqtt_channel() -> FakeChannel: | ||
| """Mock MQTT channel for testing.""" | ||
| channel = FakeChannel() | ||
| await channel.connect() | ||
| # Mock send_command to fail | ||
| channel.send_command = AsyncMock(side_effect=RoborockException("MQTT Failed")) | ||
| return channel | ||
|
|
||
|
|
||
| @pytest.fixture(name="mock_local_channel") | ||
| async def setup_mock_local_channel() -> FakeChannel: | ||
| """Mock Local channel for testing.""" | ||
| channel = FakeChannel() | ||
| return channel | ||
|
|
||
|
|
||
| @pytest.fixture(name="mock_local_session") | ||
| def setup_mock_local_session(mock_local_channel: Mock) -> Mock: | ||
| """Mock Local session factory for testing.""" | ||
| mock_session = Mock(spec=LocalSession) | ||
| mock_session.return_value = mock_local_channel | ||
| return mock_session | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_v1_channel_reconnect_with_stale_cache_and_mqtt_down( | ||
| mock_mqtt_channel: FakeChannel, | ||
| mock_local_session: Mock, | ||
| mock_local_channel: FakeChannel, | ||
| ): | ||
| """ | ||
| Test that when cache is stale (> 12h) and MQTT is down, the system | ||
| falls back to the stale cache instead of failing indefinitely. | ||
| """ | ||
| # 1. Setup stale cache | ||
| cache = InMemoryCache() | ||
| cache_data = CacheData() | ||
| stale_network_info = NetworkInfo(ip=TEST_IP, ssid="ssid", bssid="bssid") | ||
| cache_data.network_info[TEST_DEVICE_UID] = stale_network_info | ||
| await cache.set(cache_data) | ||
|
|
||
| v1_channel = V1Channel( | ||
| device_uid=TEST_DEVICE_UID, | ||
| security_data=TEST_SECURITY_DATA, | ||
| mqtt_channel=mock_mqtt_channel, | ||
| local_session=mock_local_session, | ||
| cache=cache, | ||
| ) | ||
|
|
||
| # Manually set the last refresh to be old to simulate stale cache | ||
| # Break long line | ||
| last_refresh = datetime.datetime.now(datetime.UTC) - (NETWORK_INFO_REFRESH_INTERVAL + datetime.timedelta(hours=1)) | ||
| v1_channel._last_network_info_refresh = last_refresh | ||
|
|
||
| # 2. Mock MQTT RPC channel to fail | ||
| # V1Channel creates _mqtt_rpc_channel in __init__. We need to mock its send_command. | ||
| v1_channel._mqtt_rpc_channel.send_command = AsyncMock(side_effect=RoborockException("MQTT Network Info Failed")) | ||
|
|
||
| # 3. Attempt local connection. | ||
| # Because cache is stale, use_cache will be False. | ||
| # Because MQTT fails, it will trigger fallback to cache. | ||
|
|
||
| # We call _local_connect(use_cache=False) which is what happens in the loop | ||
| # when _should_use_cache returns False (due to stale cache) | ||
| await v1_channel._local_connect(use_cache=False) | ||
|
|
||
| # 4. Assert that we tried to connect to the local IP from the cache | ||
| mock_local_session.assert_called_once_with(TEST_IP) | ||
| mock_local_channel.connect.assert_called_once() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this equivalent to what we had before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, for the
use_cache=Truepath it is equivalent. However, I extractedcached_infointo a variable so it can be reused in theexceptblock below (lines 193-199) to support the fallback mechanism without fetching from the cache again.