|
12 | 12 | from datetime import datetime, timedelta, timezone |
13 | 13 | from typing import Generic, TypeVar |
14 | 14 |
|
| 15 | +import async_solipsism |
15 | 16 | import pytest |
16 | 17 | from frequenz.channels import Broadcast, Receiver |
17 | 18 | from frequenz.client.microgrid import ( |
|
42 | 43 | from ....utils.receive_timeout import Timeout, receive_timeout |
43 | 44 |
|
44 | 45 |
|
| 46 | +@pytest.fixture |
| 47 | +def event_loop_policy() -> async_solipsism.EventLoopPolicy: |
| 48 | + """Event loop policy.""" |
| 49 | + return async_solipsism.EventLoopPolicy() |
| 50 | + |
| 51 | + |
45 | 52 | def battery_data( # pylint: disable=too-many-arguments,too-many-positional-arguments |
46 | 53 | component_id: ComponentId, |
47 | 54 | timestamp: datetime | None = None, |
@@ -995,40 +1002,51 @@ async def test_stale_data( |
995 | 1002 | setup_tracker: tuple[MockMicrogrid, Receiver[ComponentStatus]], |
996 | 1003 | ) -> None: |
997 | 1004 | """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 |
1015 | 1006 |
|
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 |
1020 | 1009 |
|
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 | + ) |
1025 | 1028 |
|
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 | + ) |
1031 | 1047 |
|
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