mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-01-08 22:07:06 +00:00
Fix slow aestransport and cli tests (#816)
This commit is contained in:
parent
3495bd83df
commit
7507837734
@ -38,7 +38,6 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
ONE_DAY_SECONDS = 86400
|
ONE_DAY_SECONDS = 86400
|
||||||
SESSION_EXPIRE_BUFFER_SECONDS = 60 * 20
|
SESSION_EXPIRE_BUFFER_SECONDS = 60 * 20
|
||||||
BACKOFF_SECONDS_AFTER_LOGIN_ERROR = 1
|
|
||||||
|
|
||||||
|
|
||||||
def _sha1(payload: bytes) -> str:
|
def _sha1(payload: bytes) -> str:
|
||||||
@ -72,6 +71,7 @@ class AesTransport(BaseTransport):
|
|||||||
}
|
}
|
||||||
CONTENT_LENGTH = "Content-Length"
|
CONTENT_LENGTH = "Content-Length"
|
||||||
KEY_PAIR_CONTENT_LENGTH = 314
|
KEY_PAIR_CONTENT_LENGTH = 314
|
||||||
|
BACKOFF_SECONDS_AFTER_LOGIN_ERROR = 1
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -213,7 +213,7 @@ class AesTransport(BaseTransport):
|
|||||||
self._default_credentials = get_default_credentials(
|
self._default_credentials = get_default_credentials(
|
||||||
DEFAULT_CREDENTIALS["TAPO"]
|
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.perform_handshake()
|
||||||
await self.try_login(self._get_login_params(self._default_credentials))
|
await self.try_login(self._get_login_params(self._default_credentials))
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import warnings
|
import warnings
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@ -48,8 +48,8 @@ def dummy_protocol():
|
|||||||
|
|
||||||
transport = DummyTransport(config=DeviceConfig(host="127.0.0.123"))
|
transport = DummyTransport(config=DeviceConfig(host="127.0.0.123"))
|
||||||
protocol = SmartProtocol(transport=transport)
|
protocol = SmartProtocol(transport=transport)
|
||||||
|
with patch.object(protocol, "BACKOFF_SECONDS_AFTER_TIMEOUT", 0):
|
||||||
return protocol
|
yield protocol
|
||||||
|
|
||||||
|
|
||||||
def pytest_configure():
|
def pytest_configure():
|
||||||
|
@ -148,4 +148,5 @@ def filter_fixtures(
|
|||||||
print(f"# {desc}")
|
print(f"# {desc}")
|
||||||
for value in filtered:
|
for value in filtered:
|
||||||
print(f"\t{value.name}")
|
print(f"\t{value.name}")
|
||||||
|
filtered.sort()
|
||||||
return filtered
|
return filtered
|
||||||
|
@ -135,6 +135,7 @@ async def test_login_errors(mocker, inner_error_codes, expectation, call_count):
|
|||||||
transport._state = TransportState.LOGIN_REQUIRED
|
transport._state = TransportState.LOGIN_REQUIRED
|
||||||
transport._session_expire_at = time.time() + 86400
|
transport._session_expire_at = time.time() + 86400
|
||||||
transport._encryption_session = mock_aes_device.encryption_session
|
transport._encryption_session = mock_aes_device.encryption_session
|
||||||
|
mocker.patch.object(transport, "BACKOFF_SECONDS_AFTER_LOGIN_ERROR", 0)
|
||||||
|
|
||||||
assert transport._token_url is None
|
assert transport._token_url is None
|
||||||
|
|
||||||
|
@ -149,10 +149,15 @@ async def test_command_with_child(dev, mocker):
|
|||||||
runner = CliRunner()
|
runner = CliRunner()
|
||||||
update_mock = mocker.patch.object(dev, "update")
|
update_mock = mocker.patch.object(dev, "update")
|
||||||
|
|
||||||
dummy_child = mocker.create_autospec(IotDevice)
|
# create_autospec for device slows tests way too much, so we use a dummy here
|
||||||
query_mock = mocker.patch.object(
|
class DummyDevice(dev.__class__):
|
||||||
dummy_child, "_query_helper", return_value={"dummy": "response"}
|
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, "_children", {"XYZ": dummy_child})
|
||||||
mocker.patch.object(dev, "get_child_device", return_value=dummy_child)
|
mocker.patch.object(dev, "get_child_device", return_value=dummy_child)
|
||||||
@ -165,7 +170,6 @@ async def test_command_with_child(dev, mocker):
|
|||||||
)
|
)
|
||||||
|
|
||||||
update_mock.assert_called()
|
update_mock.assert_called()
|
||||||
query_mock.assert_called()
|
|
||||||
assert '{"dummy": "response"}' in res.output
|
assert '{"dummy": "response"}' in res.output
|
||||||
assert res.exit_code == 0
|
assert res.exit_code == 0
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user