|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2023 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Basic tests for the DataPipeline.""" |
| 5 | + |
| 6 | +import asyncio |
| 7 | +from datetime import timedelta |
| 8 | +from typing import Iterator |
| 9 | + |
| 10 | +import async_solipsism |
| 11 | +import pytest |
| 12 | +from pytest_mock import MockerFixture |
| 13 | + |
| 14 | +from frequenz.sdk.microgrid._data_pipeline import _DataPipeline |
| 15 | +from frequenz.sdk.microgrid.client import Connection |
| 16 | +from frequenz.sdk.microgrid.component import Component, ComponentCategory, InverterType |
| 17 | +from frequenz.sdk.timeseries._resampling import ResamplerConfig |
| 18 | + |
| 19 | +from ..utils.mock_microgrid_client import MockMicrogridClient |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def event_loop() -> Iterator[async_solipsism.EventLoop]: |
| 24 | + """Replace the loop with one that doesn't interact with the outside world.""" |
| 25 | + loop = async_solipsism.EventLoop() |
| 26 | + asyncio.set_event_loop(loop) # Set the loop as default |
| 27 | + yield loop |
| 28 | + loop.close() |
| 29 | + |
| 30 | + |
| 31 | +async def test_actors_started(mocker: MockerFixture) -> None: |
| 32 | + """Test that the datasourcing, resampling and power distributing actors are started.""" |
| 33 | + |
| 34 | + datapipeline = _DataPipeline( |
| 35 | + resampler_config=ResamplerConfig(resampling_period=timedelta(seconds=1)) |
| 36 | + ) |
| 37 | + await asyncio.sleep(1) |
| 38 | + |
| 39 | + # pylint: disable=protected-access |
| 40 | + assert datapipeline._data_sourcing_actor is None |
| 41 | + assert datapipeline._resampling_actor is None |
| 42 | + assert datapipeline._power_distributing_actor is None |
| 43 | + |
| 44 | + datapipeline.logical_meter() |
| 45 | + |
| 46 | + assert datapipeline._data_sourcing_actor is not None |
| 47 | + assert datapipeline._data_sourcing_actor.actor is not None |
| 48 | + await asyncio.sleep(1) |
| 49 | + assert datapipeline._data_sourcing_actor.actor.is_running |
| 50 | + |
| 51 | + assert datapipeline._resampling_actor is not None |
| 52 | + assert datapipeline._resampling_actor.actor is not None |
| 53 | + assert datapipeline._resampling_actor.actor.is_running |
| 54 | + |
| 55 | + assert datapipeline._power_distributing_actor is None |
| 56 | + |
| 57 | + mock_client = MockMicrogridClient( |
| 58 | + set( |
| 59 | + [ |
| 60 | + Component(1, ComponentCategory.GRID), |
| 61 | + Component(4, ComponentCategory.INVERTER, InverterType.BATTERY), |
| 62 | + Component(15, ComponentCategory.BATTERY), |
| 63 | + ] |
| 64 | + ), |
| 65 | + connections=set([Connection(1, 4), Connection(4, 15)]), |
| 66 | + ) |
| 67 | + mock_client.initialize(mocker) |
| 68 | + |
| 69 | + datapipeline.battery_pool() |
| 70 | + |
| 71 | + assert datapipeline._power_distributing_actor is not None |
| 72 | + await asyncio.sleep(1) |
| 73 | + assert datapipeline._power_distributing_actor.is_running |
| 74 | + |
| 75 | + await datapipeline._stop() |
0 commit comments