Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion tests/microgrid/test_datapipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import async_solipsism
import pytest
import time_machine
from pytest_mock import MockerFixture

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


async def test_actors_started(mocker: MockerFixture) -> None:
# loop time is advanced but not the system time
async def test_actors_started(
fake_time: time_machine.Coordinates, mocker: MockerFixture
) -> None:
"""Test that the datasourcing, resampling and power distributing actors are started."""
datapipeline = _DataPipeline(
resampler_config=ResamplerConfig(resampling_period=timedelta(seconds=1))
Expand All @@ -44,6 +48,7 @@ async def test_actors_started(mocker: MockerFixture) -> None:
assert datapipeline._data_sourcing_actor is not None
assert datapipeline._data_sourcing_actor.actor is not None
await asyncio.sleep(1)
fake_time.shift(timedelta(seconds=1))
assert datapipeline._data_sourcing_actor.actor.is_running

assert datapipeline._resampling_actor is not None
Expand Down
34 changes: 27 additions & 7 deletions tests/timeseries/_battery_pool/test_battery_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import async_solipsism
import pytest
import time_machine
from frequenz.channels import Receiver, Sender
from pytest_mock import MockerFixture

Expand Down Expand Up @@ -296,22 +297,28 @@ async def run_scenarios(
raise err


async def test_all_batteries_capacity(setup_all_batteries: SetupArgs) -> None:
async def test_all_batteries_capacity(
fake_time: time_machine.Coordinates, setup_all_batteries: SetupArgs
) -> None:
"""Test capacity metric for battery pool with all components in the microgrid.

Args:
fake_time: The time machine fake time argument.
setup_all_batteries: Fixture that creates needed microgrid tools.
"""
await run_capacity_test(setup_all_batteries)
await run_capacity_test(fake_time, setup_all_batteries)


async def test_battery_pool_capacity(setup_batteries_pool: SetupArgs) -> None:
async def test_battery_pool_capacity(
fake_time: time_machine.Coordinates, setup_batteries_pool: SetupArgs
) -> None:
"""Test capacity metric for battery pool with subset of components in the microgrid.

Args:
fake_time: The time machine fake time argument.
setup_batteries_pool: Fixture that creates needed microgrid tools.
"""
await run_capacity_test(setup_batteries_pool)
await run_capacity_test(fake_time, setup_batteries_pool)


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


async def run_capacity_test(setup_args: SetupArgs) -> None:
async def run_capacity_test( # pylint: disable=too-many-locals
fake_time: time_machine.Coordinates, setup_args: SetupArgs
) -> None:
"""Test if capacity metric is working as expected.

Args:
fake_time: The time machine fake time argument.
setup_args: Needed sdk tools and tools for mocking microgrid.
"""
battery_pool = setup_args.battery_pool
Expand Down Expand Up @@ -666,20 +676,30 @@ async def run_capacity_test(setup_args: SetupArgs) -> None:
# One battery stopped sending data.
await streamer.stop_streaming(batteries_in_pool[1])
await asyncio.sleep(MAX_BATTERY_DATA_AGE_SEC + 0.2)
fake_time.shift(MAX_BATTERY_DATA_AGE_SEC + 0.2)
msg = await asyncio.wait_for(capacity_receiver.receive(), timeout=waiting_time_sec)
compare_messages(msg, Sample(now, Energy.from_watt_hours(21.0)), 0.2)
# the msg time difference shouldn't be bigger then the shifted time + 0.2 sec tolerance
compare_messages(
msg, Sample(now, Energy.from_watt_hours(21.0)), MAX_BATTERY_DATA_AGE_SEC + 0.4
)

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

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


async def run_soc_test(setup_args: SetupArgs) -> None:
Expand Down