Fix slow aestransport and cli tests (#816)

This commit is contained in:
Steven B 2024-03-11 10:17:12 +00:00 committed by GitHub
parent 3495bd83df
commit 7507837734
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 16 additions and 10 deletions

View File

@ -38,7 +38,6 @@ _LOGGER = logging.getLogger(__name__)
ONE_DAY_SECONDS = 86400
SESSION_EXPIRE_BUFFER_SECONDS = 60 * 20
BACKOFF_SECONDS_AFTER_LOGIN_ERROR = 1
def _sha1(payload: bytes) -> str:
@ -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,
@ -213,7 +213,7 @@ class AesTransport(BaseTransport):
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(

View File

@ -1,6 +1,6 @@
import warnings
from typing import Dict
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch
import pytest
@ -48,8 +48,8 @@ def dummy_protocol():
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():

View File

@ -148,4 +148,5 @@ def filter_fixtures(
print(f"# {desc}")
for value in filtered:
print(f"\t{value.name}")
filtered.sort()
return filtered

View File

@ -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

View File

@ -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)
@ -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