8000 Use freezegun for testing aes http client delays by sdb9696 · Pull Request #954 · python-kasa/python-kasa · GitHub
[go: up one dir, main page]

Skip to content

Use freezegun for testing aes http client delays #954

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

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions kasa/tests/test_aestransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import pytest
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding as asymmetric_padding
from freezegun.api import FrozenDateTimeFactory
from yarl import URL

from ..aestransport import AesEncyptionSession, AesTransport, TransportState
Expand Down Expand Up @@ -287,29 +288,40 @@ async def test_port_override():


@pytest.mark.parametrize(
"request_delay, should_error, should_succeed",
[(0, False, True), (0.125, True, True), (0.3, True, True), (0.7, True, False)],
ids=["No error", "Error then succeed", "Two errors then succeed", "No succeed"],
"device_delay_required, should_error, should_succeed",
[
pytest.param(0, False, True, id="No error"),
pytest.param(0.125, True, True, id="Error then succeed"),
pytest.param(0.3, True, True, id="Two errors then succeed"),
pytest.param(0.7, True, False, id="No succeed"),
],
)
async def test_device_closes_connection(
mocker, request_delay, should_error, should_succeed
mocker,
freezer: FrozenDateTimeFactory,
device_delay_required,
should_error,
should_succeed,
):
"""Test the delay logic in http client to deal with devices that close connections after each request.

Currently only the P100 on older firmware.
"""
host = "127.0.0.1"

# Speed up the test by dividing all times by a factor. Doesn't seem to work on windows
# but leaving here as a TODO to manipulate system time for testing.
speed_up_factor = 1
default_delay = HttpClient.WAIT_BETWEEN_REQUESTS_ON_OSERROR / speed_up_factor
request_delay = request_delay / speed_up_factor
default_delay = HttpClient.WAIT_BETWEEN_REQUESTS_ON_OSERROR

mock_aes_device = MockAesDevice(
host, 200, 0, 0, sequential_request_delay=request_delay
host, 200, 0, 0, sequential_request_delay=device_delay_required
)
mocker.patch.object(aiohttp.ClientSession, "post", side_effect=mock_aes_device.post)

async def _asyncio_sleep_mock(delay, result=None):
freezer.tick(delay)
return result

mocker.patch("asyncio.sleep", side_effect=_asyncio_sleep_mock)

config = DeviceConfig(host, credentials=Credentials("foo", "bar"))
transport = AesTransport(config=config)
transport._http_client.WAIT_BETWEEN_REQUESTS_ON_OSERROR = default_delay
Expand All @@ -332,7 +344,7 @@ async def test_device_closes_connection(
# If the device errors without a delay then it should error immedately ( + 1)
# and then the number of times the default delay passes within the request delay window
expected_error_count = (
0 if not should_error else int(request_delay / default_delay) + 1
0 if not should_error else int(device_delay_required / default_delay) + 1
)
for _ in range(3):
try:
Expand Down
56 changes: 55 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ codecov = "*"
xdoctest = "*"
coverage = {version = "*", extras = ["toml"]}
pytest-timeout = "^2"
pytest-freezer = "^0.4"

[tool.poetry.extras]
docs = ["sphinx", "sphinx_rtd_theme", "sphinxcontrib-programoutput", "myst-parser", "docutils"]
Expand Down
Loading
0