Skip to content

Commit f1fa336

Browse files
committed
Move receive_timeout to tests.utils package, so it can be reused
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 506e83e commit f1fa336

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

tests/actor/power_distributing/_component_status/test_battery_status.py

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# License: MIT
22
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH
3+
34
"""Tests for BatteryStatusTracker."""
45

56
# pylint: disable=too-many-lines
@@ -38,6 +39,7 @@
3839
from tests.timeseries.mock_microgrid import MockMicrogrid
3940

4041
from ....utils.component_data_wrapper import BatteryDataWrapper, InverterDataWrapper
42+
from ....utils.receive_timeout import Timeout, receive_timeout
4143

4244

4345
def battery_data( # pylint: disable=too-many-arguments
@@ -123,26 +125,6 @@ class Message(Generic[T]):
123125
INVERTER_ID = 8
124126

125127

126-
class _Timeout:
127-
"""Sentinel for timeout."""
128-
129-
130-
async def recv_timeout(recv: Receiver[T], timeout: float = 0.1) -> T | type[_Timeout]:
131-
"""Receive message from receiver with timeout.
132-
133-
Args:
134-
recv: Receiver to receive message from.
135-
timeout: Timeout in seconds.
136-
137-
Returns:
138-
Received message or _Timeout if timeout is reached.
139-
"""
140-
try:
141-
return await asyncio.wait_for(recv.receive(), timeout=timeout)
142-
except asyncio.TimeoutError:
143-
return _Timeout
144-
145-
146128
# pylint: disable=protected-access, unused-argument
147129
class TestBatteryStatus:
148130
"""Tests BatteryStatusTracker."""
@@ -952,7 +934,7 @@ async def test_critical_error(
952934
# --- battery warning error (keeps working) ---
953935
await self._send_healthy_inverter(mock_microgrid)
954936
await self._send_warning_error_battery(mock_microgrid)
955-
assert await recv_timeout(status_receiver, timeout=0.1) is _Timeout
937+
assert await receive_timeout(status_receiver, timeout=0.1) is Timeout
956938

957939
await self._send_healthy_battery(mock_microgrid)
958940
await self._send_healthy_inverter(mock_microgrid)
@@ -971,7 +953,7 @@ async def test_critical_error(
971953
# --- inverter warning error (keeps working) ---
972954
await self._send_healthy_battery(mock_microgrid)
973955
await self._send_warning_error_inverter(mock_microgrid)
974-
assert await recv_timeout(status_receiver, timeout=0.1) is _Timeout
956+
assert await receive_timeout(status_receiver, timeout=0.1) is Timeout
975957

976958
await self._send_healthy_battery(mock_microgrid)
977959
await self._send_healthy_inverter(mock_microgrid)
@@ -1023,11 +1005,11 @@ async def test_stale_data(
10231005
# --- stale battery data ---
10241006
await self._send_healthy_inverter(mock_microgrid)
10251007
await self._send_healthy_battery(mock_microgrid, timestamp)
1026-
assert await recv_timeout(status_receiver) is _Timeout
1008+
assert await receive_timeout(status_receiver) is Timeout
10271009

10281010
await self._send_healthy_inverter(mock_microgrid)
10291011
await self._send_healthy_battery(mock_microgrid, timestamp)
1030-
assert await recv_timeout(status_receiver, 0.3) == ComponentStatus(
1012+
assert await receive_timeout(status_receiver, 0.3) == ComponentStatus(
10311013
BATTERY_ID, ComponentStatusEnum.NOT_WORKING
10321014
)
10331015

@@ -1039,11 +1021,11 @@ async def test_stale_data(
10391021
# --- stale inverter data ---
10401022
await self._send_healthy_battery(mock_microgrid)
10411023
await self._send_healthy_inverter(mock_microgrid, timestamp)
1042-
assert await recv_timeout(status_receiver) is _Timeout
1024+
assert await receive_timeout(status_receiver) is Timeout
10431025

10441026
await self._send_healthy_battery(mock_microgrid)
10451027
await self._send_healthy_inverter(mock_microgrid, timestamp)
1046-
assert await recv_timeout(status_receiver, 0.3) == ComponentStatus(
1028+
assert await receive_timeout(status_receiver, 0.3) == ComponentStatus(
10471029
BATTERY_ID, ComponentStatusEnum.NOT_WORKING
10481030
)
10491031

tests/utils/receive_timeout.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# License: MIT
2+
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Utility for receiving messages with timeout."""
5+
6+
import asyncio
7+
from typing import TypeVar
8+
9+
from frequenz.channels import Receiver
10+
11+
T = TypeVar("T")
12+
13+
14+
class Timeout:
15+
"""Sentinel for timeout."""
16+
17+
18+
async def receive_timeout(recv: Receiver[T], timeout: float = 0.1) -> T | type[Timeout]:
19+
"""Receive message from receiver with timeout.
20+
21+
Args:
22+
recv: Receiver to receive message from.
23+
timeout: Timeout in seconds.
24+
25+
Returns:
26+
Received message or Timeout if timeout is reached.
27+
"""
28+
try:
29+
return await asyncio.wait_for(recv.receive(), timeout=timeout)
30+
except asyncio.TimeoutError:
31+
return Timeout

0 commit comments

Comments
 (0)