8000 Add autouse fixture to patch asyncio.sleep (#1131) · python-kasa/python-kasa@db686e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit db686e1

Browse files
authored
Add autouse fixture to patch asyncio.sleep (#1131)
1 parent 038b699 commit db686e1

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

kasa/tests/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import asyncio
34
import warnings
45
from unittest.mock import MagicMock, patch
56

@@ -96,6 +97,18 @@ def pytest_collection_modifyitems(config, items):
9697
item.add_marker(requires_dummy)
9798

9899

100+
@pytest.fixture(autouse=True, scope="session")
101+
def asyncio_sleep_fixture(): # noqa: PT004
102+
"""Patch sleep to prevent tests actually waiting."""
103+
orig_asyncio_sleep = asyncio.sleep
104+
105+
async def _asyncio_sleep(*_, **__):
106+
await orig_asyncio_sleep(0)
107+
108+
with patch("asyncio.sleep", side_effect=_asyncio_sleep):
109+
yield
110+
111+
99112
# allow mocks to be awaited
100113
# https://stackoverflow.com/questions/51394411/python-object-magicmock-cant-be-used-in-await-expression/51399767#51399767
101114

kasa/tests/test_klapprotocol.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ async def test_protocol_retries_via_client_session(
6666
):
6767
host = "127.0.0.1"
6868
conn = mocker.patch.object(aiohttp.ClientSession, "post", side_effect=error)
69-
mocker.patch.object(protocol_class, "BACKOFF_SECONDS_AFTER_TIMEOUT", 0)
70-
mocker.patch("asyncio.sleep")
7169

7270
config = DeviceConfig(host)
7371
with pytest.raises(KasaException):
@@ -140,7 +138,6 @@ async def test_protocol_retry_recoverable_error(
140138
"post",
141139
side_effect=aiohttp.ClientOSError("foo"),
142140
)
143-
mocker.patch("asyncio.sleep")
144141

145142
config = DeviceConfig(host)
146143
with pytest.raises(KasaException):

0 commit comments

Comments
 (0)
0