Skip to content

Commit

Permalink
Fix slow aestransport and cli tests (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdb9696 committed Mar 11, 2024
1 parent 3495bd8 commit 7507837
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions kasa/aestransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

ONE_DAY_SECONDS = 86400
SESSION_EXPIRE_BUFFER_SECONDS = 60 * 20
BACKOFF_SECONDS_AFTER_LOGIN_ERROR = 1


def _sha1(payload: bytes) -> str:
Expand Down Expand Up @@ -72,6 +71,7 @@ class AesTransport(BaseTransport):
}
CONTENT_LENGTH = "Content-Length"
KEY_PAIR_CONTENT_LENGTH = 314
BACKOFF_SECONDS_AFTER_LOGIN_ERROR = 1

def __init__(
self,
Expand Down Expand Up @@ -213,7 +213,7 @@ async def perform_login(self):
self._default_credentials = get_default_credentials(
DEFAULT_CREDENTIALS["TAPO"]
)
await asyncio.sleep(BACKOFF_SECONDS_AFTER_LOGIN_ERROR)
await asyncio.sleep(self.BACKOFF_SECONDS_AFTER_LOGIN_ERROR)
await self.perform_handshake()
await self.try_login(self._get_login_params(self._default_credentials))
_LOGGER.debug(
Expand Down
6 changes: 3 additions & 3 deletions kasa/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import warnings
from typing import Dict
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch

import pytest

Expand Down Expand Up @@ -48,8 +48,8 @@ async def reset(self) -> None:

transport = DummyTransport(config=DeviceConfig(host="127.0.0.123"))
protocol = SmartProtocol(transport=transport)

return protocol
with patch.object(protocol, "BACKOFF_SECONDS_AFTER_TIMEOUT", 0):
yield protocol


def pytest_configure():
Expand Down
1 change: 1 addition & 0 deletions kasa/tests/fixtureinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,5 @@ def _device_type_match(fixture_data: FixtureInfo, device_type):
print(f"# {desc}")
for value in filtered:
print(f"\t{value.name}")
filtered.sort()
return filtered
1 change: 1 addition & 0 deletions kasa/tests/test_aestransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ async def test_login_errors(mocker, inner_error_codes, expectation, call_count):
transport._state = TransportState.LOGIN_REQUIRED
transport._session_expire_at = time.time() + 86400
transport._encryption_session = mock_aes_device.encryption_session
mocker.patch.object(transport, "BACKOFF_SECONDS_AFTER_LOGIN_ERROR", 0)

assert transport._token_url is None

Expand Down
14 changes: 9 additions & 5 deletions kasa/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,15 @@ async def test_command_with_child(dev, mocker):
runner = CliRunner()
update_mock = mocker.patch.object(dev, "update")

dummy_child = mocker.create_autospec(IotDevice)
query_mock = mocker.patch.object(
dummy_child, "_query_helper", return_value={"dummy": "response"}
)
# create_autospec for device slows tests way too much, so we use a dummy here
class DummyDevice(dev.__class__):
def __init__(self):
super().__init__("127.0.0.1")

async def _query_helper(*_, **__):
return {"dummy": "response"}

dummy_child = DummyDevice()

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

update_mock.assert_called()
query_mock.assert_called()
assert '{"dummy": "response"}' in res.output
assert res.exit_code == 0

Expand Down

0 comments on commit 7507837

Please sign in to comment.