Skip to content

Commit 08ee2b4

Browse files
Fix flaky LogicalMeter test_soc (#235)
In MockMicrogrid it is hardcoded that iverter with id > 100 send first 5 messages and then stop sending. Tests assumed we have control over when the components starts sending data and in what order. But that is not true in async. Also inverter messages were not synchronized with rest of the data.
2 parents 613c180 + b681463 commit 08ee2b4

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

tests/timeseries/mock_microgrid.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def __init__(self, grid_side_meter: bool, sample_rate_s: float = 0.01):
7575
self.battery_ids: list[int] = []
7676
self.evc_ids: list[int] = []
7777
self.meter_ids: list[int] = [4]
78+
self.bat_inv_map: dict[int, int] = {}
7879

7980
self.evc_component_states: dict[int, EVChargerComponentState] = {}
8081
self.evc_cable_states: dict[int, EVChargerCableState] = {}
@@ -179,6 +180,7 @@ def add_batteries(self, count: int) -> None:
179180
self.meter_ids.append(meter_id)
180181
self.battery_inverter_ids.append(inv_id)
181182
self.battery_ids.append(bat_id)
183+
self.bat_inv_map[bat_id] = inv_id
182184

183185
self._components.add(
184186
Component(

tests/timeseries/test_logical_meter.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
)
2121
from .mock_microgrid import MockMicrogrid
2222

23+
# pylint: disable=too-many-locals
24+
2325

2426
class TestLogicalMeter:
2527
"""Tests for the logical meter."""
@@ -202,33 +204,48 @@ async def test_soc(self, mocker: MockerFixture) -> None:
202204

203205
soc_recv = await logical_meter._soc() # pylint: disable=protected-access
204206

205-
bat_receivers = [
206-
await get_resampled_stream(
207+
bat_receivers = {
208+
bat_id: await get_resampled_stream(
207209
logical_meter,
208210
channel_registry,
209211
request_chan.new_sender(),
210212
bat_id,
211213
ComponentMetricId.SOC,
212214
)
213215
for bat_id in mockgrid.battery_ids
214-
]
216+
}
217+
218+
bat_inv_map = mockgrid.bat_inv_map
219+
inv_receivers = {
220+
inverter_id: await get_resampled_stream(
221+
logical_meter,
222+
channel_registry,
223+
request_chan.new_sender(),
224+
inverter_id,
225+
ComponentMetricId.ACTIVE_POWER,
226+
)
227+
for inverter_id in mockgrid.bat_inv_map.values()
228+
}
215229

216-
await synchronize_receivers([soc_recv, *bat_receivers])
230+
await synchronize_receivers(
231+
[soc_recv, *bat_receivers.values(), *inv_receivers.values()]
232+
)
217233

218-
for ctr in range(10):
234+
for _ in range(10):
219235
bat_vals = []
220-
for recv in bat_receivers:
221-
val = await recv.receive()
236+
for bat_id, bat_recv in bat_receivers.items():
237+
inv_id = bat_inv_map[bat_id]
238+
inv_recv = inv_receivers[inv_id]
239+
inv_msg = await inv_recv.receive()
240+
# We won't use batteries where adjacent inverter is not sending active
241+
# power.
242+
if inv_msg.value is None:
243+
continue
244+
245+
val = await bat_recv.receive()
222246
assert val is not None and val.value is not None
223247
bat_vals.append(val.value)
224248

225-
assert len(bat_vals) == 3
226-
# After 5 values, the inverter with component_id > 100 stops sending
227-
# data. And the values from the last battery goes out of the calculation.
228-
# So we drop it from out control value as well.
229-
if ctr >= 5:
230-
bat_vals = bat_vals[:2]
231-
232249
soc_sample = await soc_recv.receive()
233250
assert soc_sample is not None and soc_sample.value is not None
234251

0 commit comments

Comments
 (0)