mirror of
https://github.com/python-kasa/python-kasa.git
synced 2024-12-22 19:23:34 +00:00
Add autouse fixture to patch asyncio.sleep (#1131)
This commit is contained in:
parent
038b6993ca
commit
db686e191a
@ -1,5 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
import warnings
|
import warnings
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
@ -96,6 +97,18 @@ def pytest_collection_modifyitems(config, items):
|
|||||||
item.add_marker(requires_dummy)
|
item.add_marker(requires_dummy)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True, scope="session")
|
||||||
|
def asyncio_sleep_fixture(): # noqa: PT004
|
||||||
|
"""Patch sleep to prevent tests actually waiting."""
|
||||||
|
orig_asyncio_sleep = asyncio.sleep
|
||||||
|
|
||||||
|
async def _asyncio_sleep(*_, **__):
|
||||||
|
await orig_asyncio_sleep(0)
|
||||||
|
|
||||||
|
with patch("asyncio.sleep", side_effect=_asyncio_sleep):
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
# allow mocks to be awaited
|
# allow mocks to be awaited
|
||||||
# https://stackoverflow.com/questions/51394411/python-object-magicmock-cant-be-used-in-await-expression/51399767#51399767
|
# https://stackoverflow.com/questions/51394411/python-object-magicmock-cant-be-used-in-await-expression/51399767#51399767
|
||||||
|
|
||||||
|
@ -66,8 +66,6 @@ async def test_protocol_retries_via_client_session(
|
|||||||
):
|
):
|
||||||
host = "127.0.0.1"
|
host = "127.0.0.1"
|
||||||
conn = mocker.patch.object(aiohttp.ClientSession, "post", side_effect=error)
|
conn = mocker.patch.object(aiohttp.ClientSession, "post", side_effect=error)
|
||||||
mocker.patch.object(protocol_class, "BACKOFF_SECONDS_AFTER_TIMEOUT", 0)
|
|
||||||
mocker.patch("asyncio.sleep")
|
|
||||||
|
|
||||||
config = DeviceConfig(host)
|
config = DeviceConfig(host)
|
||||||
with pytest.raises(KasaException):
|
with pytest.raises(KasaException):
|
||||||
@ -140,7 +138,6 @@ async def test_protocol_retry_recoverable_error(
|
|||||||
"post",
|
"post",
|
||||||
side_effect=aiohttp.ClientOSError("foo"),
|
side_effect=aiohttp.ClientOSError("foo"),
|
||||||
)
|
)
|
||||||
mocker.patch("asyncio.sleep")
|
|
||||||
|
|
||||||
config = DeviceConfig(host)
|
config = DeviceConfig(host)
|
||||||
with pytest.raises(KasaException):
|
with pytest.raises(KasaException):
|
||||||
|
Loading…
Reference in New Issue
Block a user