Skip to content

Commit 9409cba

Browse files
committed
Add a fixture to monitor asyncio.sleep() calls
We monitor only the calls to sleep inside the wall clock time module, so we can assert those calls independently from what happens in the tests themselves, for example. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent cabf859 commit 9409cba

File tree

1 file changed

+15
-1
lines changed
  • tests/timeseries/_resampling/wall_clock_timer

1 file changed

+15
-1
lines changed

tests/timeseries/_resampling/wall_clock_timer/conftest.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33

44
"""Fixtures for wall clock timer tests."""
55

6+
import asyncio
67
from collections.abc import Iterator
78
from datetime import datetime
8-
from unittest.mock import MagicMock, patch
9+
from unittest.mock import AsyncMock, MagicMock, patch
910

1011
import pytest
1112
from frequenz.core.datetime import UNIX_EPOCH
@@ -19,3 +20,16 @@ def datetime_mock() -> Iterator[MagicMock]:
1920
dt_mock.now.return_value = UNIX_EPOCH
2021
with patch(dt_symbol, new=dt_mock):
2122
yield dt_mock
23+
24+
25+
@pytest.fixture
26+
def asyncio_sleep_mock() -> Iterator[AsyncMock]:
27+
"""Mock asyncio.sleep in the target module for all tests."""
28+
asyncio_symbol = "frequenz.sdk.timeseries._resampling._wall_clock_timer.asyncio"
29+
mock = AsyncMock(
30+
name="asyncio_sleep_mock", wraps=asyncio.sleep, spec_set=asyncio.sleep
31+
)
32+
asyncio_mock = AsyncMock(name="asyncio_mock", wraps=asyncio, spec_set=asyncio)
33+
asyncio_mock.sleep = mock
34+
with patch(asyncio_symbol, new=asyncio_mock):
35+
yield mock

0 commit comments

Comments
 (0)