Skip to content

Commit 254c367

Browse files
committed
Update tests to support latest time-machine
And migrate to latest time-machine version v2.16.0 This new version of `time-machine` mocks the monotonic clock of asyncio. This was causing the `test_battery_status.py::TestBatteryStatus::test_async_battery_status` test to fail, because `channels.Timer`s weren't working properly. To fix this, this commit switches this test file to use an `async_solipsism` event loop instead, which `time-machine` doesn't affect. But introducing the `async_solipsism` event loop caused the `TestBatteryStatusRecovery::test_stale_data` test to fail, because it needed the clock time to make progress, which wasn't happening because `sleep` was returning immediately. To fix this, this commit also updates that test to use `time-machine` and moves the time forward manually, so the test can pass. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 286ad18 commit 254c367

File tree

2 files changed

+52
-34
lines changed

2 files changed

+52
-34
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ dev-pytest = [
8484
"frequenz-repo-config[extra-lint-examples] == 0.13.3",
8585
"pytest-mock == 3.14.0",
8686
"pytest-asyncio == 0.26.0",
87-
"time-machine == 2.12.0",
87+
"time-machine == 2.16.0",
8888
"async-solipsism == 0.7",
8989
"hypothesis == 6.131.9",
9090
]

tests/microgrid/power_distributing/_component_status/test_battery_status.py

+51-33
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from datetime import datetime, timedelta, timezone
1313
from typing import Generic, TypeVar
1414

15+
import async_solipsism
1516
import pytest
1617
from frequenz.channels import Broadcast, Receiver
1718
from frequenz.client.microgrid import (
@@ -42,6 +43,12 @@
4243
from ....utils.receive_timeout import Timeout, receive_timeout
4344

4445

46+
@pytest.fixture
47+
def event_loop_policy() -> async_solipsism.EventLoopPolicy:
48+
"""Event loop policy."""
49+
return async_solipsism.EventLoopPolicy()
50+
51+
4552
def battery_data( # pylint: disable=too-many-arguments,too-many-positional-arguments
4653
component_id: ComponentId,
4754
timestamp: datetime | None = None,
@@ -995,40 +1002,51 @@ async def test_stale_data(
9951002
setup_tracker: tuple[MockMicrogrid, Receiver[ComponentStatus]],
9961003
) -> None:
9971004
"""Test recovery after stale data."""
998-
mock_microgrid, status_receiver = setup_tracker
999-
1000-
timestamp = datetime.now(timezone.utc)
1001-
await self._send_healthy_battery(mock_microgrid, timestamp)
1002-
await self._send_healthy_inverter(mock_microgrid)
1003-
assert (await status_receiver.receive()).value is ComponentStatusEnum.WORKING
1004-
1005-
# --- stale battery data ---
1006-
await self._send_healthy_inverter(mock_microgrid)
1007-
await self._send_healthy_battery(mock_microgrid, timestamp)
1008-
assert await receive_timeout(status_receiver) is Timeout
1009-
1010-
await self._send_healthy_inverter(mock_microgrid)
1011-
await self._send_healthy_battery(mock_microgrid, timestamp)
1012-
assert await receive_timeout(status_receiver, 0.3) == ComponentStatus(
1013-
BATTERY_ID, ComponentStatusEnum.NOT_WORKING
1014-
)
1005+
import time_machine # pylint: disable=import-outside-toplevel
10151006

1016-
timestamp = datetime.now(timezone.utc)
1017-
await self._send_healthy_battery(mock_microgrid, timestamp)
1018-
await self._send_healthy_inverter(mock_microgrid, timestamp)
1019-
assert (await status_receiver.receive()).value is ComponentStatusEnum.WORKING
1007+
with time_machine.travel("2022-01-01 00:00 UTC", tick=False) as time:
1008+
mock_microgrid, status_receiver = setup_tracker
10201009

1021-
# --- stale inverter data ---
1022-
await self._send_healthy_battery(mock_microgrid)
1023-
await self._send_healthy_inverter(mock_microgrid, timestamp)
1024-
assert await receive_timeout(status_receiver) is Timeout
1010+
timestamp = datetime.now(timezone.utc)
1011+
await self._send_healthy_battery(mock_microgrid, timestamp)
1012+
await self._send_healthy_inverter(mock_microgrid)
1013+
assert (
1014+
await status_receiver.receive()
1015+
).value is ComponentStatusEnum.WORKING
1016+
1017+
# --- stale battery data ---
1018+
await self._send_healthy_inverter(mock_microgrid)
1019+
await self._send_healthy_battery(mock_microgrid, timestamp)
1020+
assert await receive_timeout(status_receiver) is Timeout
1021+
1022+
await self._send_healthy_inverter(mock_microgrid)
1023+
await self._send_healthy_battery(mock_microgrid, timestamp)
1024+
time.shift(0.3)
1025+
assert await receive_timeout(status_receiver, 0.3) == ComponentStatus(
1026+
BATTERY_ID, ComponentStatusEnum.NOT_WORKING
1027+
)
10251028

1026-
await self._send_healthy_battery(mock_microgrid)
1027-
await self._send_healthy_inverter(mock_microgrid, timestamp)
1028-
assert await receive_timeout(status_receiver, 0.3) == ComponentStatus(
1029-
BATTERY_ID, ComponentStatusEnum.NOT_WORKING
1030-
)
1029+
timestamp = datetime.now(timezone.utc)
1030+
await self._send_healthy_battery(mock_microgrid, timestamp)
1031+
await self._send_healthy_inverter(mock_microgrid, timestamp)
1032+
assert (
1033+
await status_receiver.receive()
1034+
).value is ComponentStatusEnum.WORKING
1035+
1036+
# --- stale inverter data ---
1037+
await self._send_healthy_battery(mock_microgrid)
1038+
await self._send_healthy_inverter(mock_microgrid, timestamp)
1039+
assert await receive_timeout(status_receiver) is Timeout
1040+
1041+
await self._send_healthy_battery(mock_microgrid)
1042+
await self._send_healthy_inverter(mock_microgrid, timestamp)
1043+
time.shift(0.3)
1044+
assert await receive_timeout(status_receiver, 0.3) == ComponentStatus(
1045+
BATTERY_ID, ComponentStatusEnum.NOT_WORKING
1046+
)
10311047

1032-
await self._send_healthy_battery(mock_microgrid)
1033-
await self._send_healthy_inverter(mock_microgrid)
1034-
assert (await status_receiver.receive()).value is ComponentStatusEnum.WORKING
1048+
await self._send_healthy_battery(mock_microgrid)
1049+
await self._send_healthy_inverter(mock_microgrid)
1050+
assert (
1051+
await status_receiver.receive()
1052+
).value is ComponentStatusEnum.WORKING

0 commit comments

Comments
 (0)