Skip to content

Commit 0c2501d

Browse files
Use MockMicrogridClient instead of a MockMicrogridServer in LogicalMeter tests (#180)
Closes #177
2 parents 13455af + cc32ec0 commit 0c2501d

File tree

5 files changed

+402
-234
lines changed

5 files changed

+402
-234
lines changed

src/frequenz/sdk/timeseries/logical_meter/_formula_engine.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,20 @@ async def _synchronize_metric_timestamps(
9191
name = metric.get_name()
9292
if result is None:
9393
raise RuntimeError(f"Stream closed for component: {name}")
94-
metrics_by_ts[result.timestamp] = name
94+
metrics_by_ts.setdefault(result.timestamp, []).append(name)
9595
latest_ts = max(metrics_by_ts)
9696

9797
# fetch the metrics with non-latest timestamps again until we have the values
9898
# for the same ts for all metrics.
99-
for metric_ts, name in metrics_by_ts.items():
99+
for metric_ts, names in metrics_by_ts.items():
100100
if metric_ts == latest_ts:
101101
continue
102-
fetcher = self._metric_fetchers[name]
103102
while metric_ts < latest_ts:
104-
next_val = await fetcher.fetch_next()
105-
assert next_val is not None
106-
metric_ts = next_val.timestamp
103+
for name in names:
104+
fetcher = self._metric_fetchers[name]
105+
next_val = await fetcher.fetch_next()
106+
assert next_val is not None
107+
metric_ts = next_val.timestamp
107108
if metric_ts > latest_ts:
108109
raise RuntimeError(
109110
"Unable to synchronize resampled metric timestamps, "

tests/actor/test_power_distributing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async def test_constructor(self, mocker: MockerFixture) -> None:
8484
"""Test if gets all necessary data."""
8585
components, connections = self.component_graph()
8686
mock_microgrid = MockMicrogridClient(components, connections)
87-
await mock_microgrid.initialize(mocker)
87+
mock_microgrid.initialize(mocker)
8888

8989
channel = Bidirectional[Request, Result]("user1", "power_distributor")
9090
distributor = PowerDistributingActor({"user1": channel.service_handle})
@@ -101,7 +101,7 @@ async def init_mock_microgrid(self, mocker: MockerFixture) -> MockMicrogridClien
101101
"""
102102
components, connections = self.component_graph()
103103
microgrid = MockMicrogridClient(components, connections)
104-
await microgrid.initialize(mocker)
104+
microgrid.initialize(mocker)
105105

106106
graph = microgrid.component_graph
107107
for battery in graph.components(component_category={ComponentCategory.BATTERY}):

0 commit comments

Comments
 (0)