Skip to content

Commit 7507837

Browse files
authored
Fix slow aestransport and cli tests (python-kasa#816)
1 parent 3495bd8 commit 7507837

File tree

5 files changed

+16
-10
lines changed

5 files changed

+16
-10
lines changed

kasa/aestransport.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838

3939
ONE_DAY_SECONDS = 86400
4040
SESSION_EXPIRE_BUFFER_SECONDS = 60 * 20
41-
BACKOFF_SECONDS_AFTER_LOGIN_ERROR = 1
4241

4342

4443
def _sha1(payload: bytes) -> str:
@@ -72,6 +71,7 @@ class AesTransport(BaseTransport):
7271
}
7372
CONTENT_LENGTH = "Content-Length"
7473
KEY_PAIR_CONTENT_LENGTH = 314
74+
BACKOFF_SECONDS_AFTER_LOGIN_ERROR = 1
7575

7676
def __init__(
7777
self,
@@ -213,7 +213,7 @@ async def perform_login(self):
213213
self._default_credentials = get_default_credentials(
214214
DEFAULT_CREDENTIALS["TAPO"]
215215
)
216-
await asyncio.sleep(BACKOFF_SECONDS_AFTER_LOGIN_ERROR)
216+
await asyncio.sleep(self.BACKOFF_SECONDS_AFTER_LOGIN_ERROR)
217217
await self.perform_handshake()
218218
await self.try_login(self._get_login_params(self._default_credentials))
219219
_LOGGER.debug(

kasa/tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import warnings
22
from typing import Dict
3-
from unittest.mock import MagicMock
3+
from unittest.mock import MagicMock, patch
44

55
import pytest
66

@@ -48,8 +48,8 @@ async def reset(self) -> None:
4848

4949
transport = DummyTransport(config=DeviceConfig(host="127.0.0.123"))
5050
protocol = SmartProtocol(transport=transport)
51-
52-
return protocol
51+
with patch.object(protocol, "BACKOFF_SECONDS_AFTER_TIMEOUT", 0):
52+
yield protocol
5353

5454

5555
def pytest_configure():

kasa/tests/fixtureinfo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,5 @@ def _device_type_match(fixture_data: FixtureInfo, device_type):
148148
print(f"# {desc}")
149149
for value in filtered:
150150
print(f"\t{value.name}")
151+
filtered.sort()
151152
return filtered

kasa/tests/test_aestransport.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ async def test_login_errors(mocker, inner_error_codes, expectation, call_count):
135135
transport._state = TransportState.LOGIN_REQUIRED
136136
transport._session_expire_at = time.time() + 86400
137137
transport._encryption_session = mock_aes_device.encryption_session
138+
mocker.patch.object(transport, "BACKOFF_SECONDS_AFTER_LOGIN_ERROR", 0)
138139

139140
assert transport._token_url is None
140141

kasa/tests/test_cli.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,15 @@ async def test_command_with_child(dev, mocker):
149149
runner = CliRunner()
150150
update_mock = mocker.patch.object(dev, "update")
151151

152-
dummy_child = mocker.create_autospec(IotDevice)
153-
query_mock = mocker.patch.object(
154-
dummy_child, "_query_helper", return_value={"dummy": "response"}
155-
)
152+
# create_autospec for device slows tests way too much, so we use a dummy here
153+
class DummyDevice(dev.__class__):
154+
def __init__(self):
155+
super().__init__("127.0.0.1")
156+
157+
async def _query_helper(*_, **__):
158+
return {"dummy": "response"}
159+
160+
dummy_child = DummyDevice()
156161

157162
mocker.patch.object(dev, "_children", {"XYZ": dummy_child})
158163
mocker.patch.object(dev, "get_child_device", return_value=dummy_child)
@@ -165,7 +170,6 @@ async def test_command_with_child(dev, mocker):
165170
)
166171

167172
update_mock.assert_called()
168-
query_mock.assert_called()
169173
assert '{"dummy": "response"}' in res.output
170174
assert res.exit_code == 0
171175

0 commit comments

Comments
 (0)