Skip to content

Commit 80db721

Browse files
Update some tests to use time machine (#798)
Updates some data pipeline and Battery Pool tests.
2 parents d32719f + 0a0c44b commit 80db721

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

tests/microgrid/test_datapipeline.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import async_solipsism
1111
import pytest
12+
import time_machine
1213
from pytest_mock import MockerFixture
1314

1415
from frequenz.sdk.microgrid._data_pipeline import _DataPipeline
@@ -27,7 +28,10 @@ def event_loop() -> Iterator[async_solipsism.EventLoop]:
2728
loop.close()
2829

2930

30-
async def test_actors_started(mocker: MockerFixture) -> None:
31+
# loop time is advanced but not the system time
32+
async def test_actors_started(
33+
fake_time: time_machine.Coordinates, mocker: MockerFixture
34+
) -> None:
3135
"""Test that the datasourcing, resampling and power distributing actors are started."""
3236
datapipeline = _DataPipeline(
3337
resampler_config=ResamplerConfig(resampling_period=timedelta(seconds=1))
@@ -44,6 +48,7 @@ async def test_actors_started(mocker: MockerFixture) -> None:
4448
assert datapipeline._data_sourcing_actor is not None
4549
assert datapipeline._data_sourcing_actor.actor is not None
4650
await asyncio.sleep(1)
51+
fake_time.shift(timedelta(seconds=1))
4752
assert datapipeline._data_sourcing_actor.actor.is_running
4853

4954
assert datapipeline._resampling_actor is not None

tests/timeseries/_battery_pool/test_battery_pool.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import async_solipsism
1919
import pytest
20+
import time_machine
2021
from frequenz.channels import Receiver, Sender
2122
from pytest_mock import MockerFixture
2223

@@ -296,22 +297,28 @@ async def run_scenarios(
296297
raise err
297298

298299

299-
async def test_all_batteries_capacity(setup_all_batteries: SetupArgs) -> None:
300+
async def test_all_batteries_capacity(
301+
fake_time: time_machine.Coordinates, setup_all_batteries: SetupArgs
302+
) -> None:
300303
"""Test capacity metric for battery pool with all components in the microgrid.
301304
302305
Args:
306+
fake_time: The time machine fake time argument.
303307
setup_all_batteries: Fixture that creates needed microgrid tools.
304308
"""
305-
await run_capacity_test(setup_all_batteries)
309+
await run_capacity_test(fake_time, setup_all_batteries)
306310

307311

308-
async def test_battery_pool_capacity(setup_batteries_pool: SetupArgs) -> None:
312+
async def test_battery_pool_capacity(
313+
fake_time: time_machine.Coordinates, setup_batteries_pool: SetupArgs
314+
) -> None:
309315
"""Test capacity metric for battery pool with subset of components in the microgrid.
310316
311317
Args:
318+
fake_time: The time machine fake time argument.
312319
setup_batteries_pool: Fixture that creates needed microgrid tools.
313320
"""
314-
await run_capacity_test(setup_batteries_pool)
321+
await run_capacity_test(fake_time, setup_batteries_pool)
315322

316323

317324
async def test_all_batteries_soc(setup_all_batteries: SetupArgs) -> None:
@@ -499,10 +506,13 @@ async def test_battery_pool_power(mocker: MockerFixture) -> None:
499506
await mockgrid.cleanup()
500507

501508

502-
async def run_capacity_test(setup_args: SetupArgs) -> None:
509+
async def run_capacity_test( # pylint: disable=too-many-locals
510+
fake_time: time_machine.Coordinates, setup_args: SetupArgs
511+
) -> None:
503512
"""Test if capacity metric is working as expected.
504513
505514
Args:
515+
fake_time: The time machine fake time argument.
506516
setup_args: Needed sdk tools and tools for mocking microgrid.
507517
"""
508518
battery_pool = setup_args.battery_pool
@@ -666,20 +676,30 @@ async def run_capacity_test(setup_args: SetupArgs) -> None:
666676
# One battery stopped sending data.
667677
await streamer.stop_streaming(batteries_in_pool[1])
668678
await asyncio.sleep(MAX_BATTERY_DATA_AGE_SEC + 0.2)
679+
fake_time.shift(MAX_BATTERY_DATA_AGE_SEC + 0.2)
669680
msg = await asyncio.wait_for(capacity_receiver.receive(), timeout=waiting_time_sec)
670-
compare_messages(msg, Sample(now, Energy.from_watt_hours(21.0)), 0.2)
681+
# the msg time difference shouldn't be bigger then the shifted time + 0.2 sec tolerance
682+
compare_messages(
683+
msg, Sample(now, Energy.from_watt_hours(21.0)), MAX_BATTERY_DATA_AGE_SEC + 0.4
684+
)
671685

672686
# All batteries stopped sending data.
673687
await streamer.stop_streaming(batteries_in_pool[0])
674688
await asyncio.sleep(MAX_BATTERY_DATA_AGE_SEC + 0.2)
689+
fake_time.shift(MAX_BATTERY_DATA_AGE_SEC + 0.2)
675690
msg = await asyncio.wait_for(capacity_receiver.receive(), timeout=waiting_time_sec)
676691
assert isinstance(msg, Sample) and msg.value is None
677692

678693
# One battery started sending data.
679694
latest_data = streamer.get_current_component_data(batteries_in_pool[0])
680695
streamer.start_streaming(latest_data, sampling_rate=0.1)
681696
msg = await asyncio.wait_for(capacity_receiver.receive(), timeout=waiting_time_sec)
682-
compare_messages(msg, Sample(now, Energy.from_watt_hours(21.0)), 0.2)
697+
# the msg time difference shouldn't be bigger then the shifted time + 0.2 sec tolerance
698+
compare_messages(
699+
msg,
700+
Sample(datetime.now(tz=timezone.utc), Energy.from_watt_hours(21.0)),
701+
MAX_BATTERY_DATA_AGE_SEC + 0.4,
702+
)
683703

684704

685705
async def run_soc_test(setup_args: SetupArgs) -> None:

0 commit comments

Comments
 (0)