diff --git a/kasa/tests/conftest.py b/kasa/tests/conftest.py index e709a58f..9cefd7ab 100644 --- a/kasa/tests/conftest.py +++ b/kasa/tests/conftest.py @@ -1,5 +1,6 @@ from __future__ import annotations +import asyncio import warnings from unittest.mock import MagicMock, patch @@ -96,6 +97,18 @@ def pytest_collection_modifyitems(config, items): 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 # https://stackoverflow.com/questions/51394411/python-object-magicmock-cant-be-used-in-await-expression/51399767#51399767 diff --git a/kasa/tests/test_klapprotocol.py b/kasa/tests/test_klapprotocol.py index 4862235a..ce370b5b 100644 --- a/kasa/tests/test_klapprotocol.py +++ b/kasa/tests/test_klapprotocol.py @@ -66,8 +66,6 @@ async def test_protocol_retries_via_client_session( ): host = "127.0.0.1" 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) with pytest.raises(KasaException): @@ -140,7 +138,6 @@ async def test_protocol_retry_recoverable_error( "post", side_effect=aiohttp.ClientOSError("foo"), ) - mocker.patch("asyncio.sleep") config = DeviceConfig(host) with pytest.raises(KasaException):