88import dataclasses
99import logging
1010from dataclasses import dataclass , is_dataclass , replace
11- from datetime import datetime , timedelta , timezone
11+ from datetime import datetime , timezone
1212from typing import Any , AsyncIterator , Generic , Iterator , TypeVar
1313
1414import async_solipsism
1515import pytest
16- from frequenz .channels import Broadcast , Receiver , Sender
16+ from frequenz .channels import Receiver , Sender
1717from pytest_mock import MockerFixture
1818
19+ from frequenz .sdk import microgrid
1920from frequenz .sdk ._internal ._constants import (
2021 MAX_BATTERY_DATA_AGE_SEC ,
2122 WAIT_FOR_COMPONENT_DATA_SEC ,
2223)
24+ from frequenz .sdk .actor import ResamplerConfig
2325from frequenz .sdk .actor .power_distributing import BatteryStatus
2426from frequenz .sdk .microgrid .component import ComponentCategory
2527from frequenz .sdk .timeseries .battery_pool import (
4042 create_component_graph_structure ,
4143)
4244from ...utils .mock_microgrid_client import MockMicrogridClient
43- from ...utils .sdk_interface import SdkInterface
4445
4546_logger = logging .getLogger (__name__ )
4647
@@ -130,27 +131,37 @@ async def setup_all_batteries(mocker: MockerFixture) -> AsyncIterator[SetupArgs]
130131 mock_microgrid = create_mock_microgrid (
131132 mocker , ComponentGraphConfig (batteries_num = 2 )
132133 )
133- sdk = SdkInterface (resampling_period_s = 1 )
134+ min_update_interval : float = 0.2
135+ # pylint: disable=protected-access
136+ microgrid ._data_pipeline ._DATA_PIPELINE = None
137+ microgrid ._data_pipeline .initialize (ResamplerConfig (min_update_interval ))
134138 streamer = MockComponentDataStreamer (mock_microgrid )
135139
136140 # We don't use status channel from the sdk interface to limit
137141 # the scope of this tests. This tests should cover BatteryPool only.
138142 # We use our own battery status channel, where we easily control set of working
139143 # batteries.
140- battery_status_channel = Broadcast [BatteryStatus ]("bat_status" , resend_latest = True )
141- sender_channel = battery_status_channel .new_sender ()
142- min_update_interval : float = 0.2
143- battery_pool = BatteryPool (
144- batteries_status_receiver = battery_status_channel .new_receiver (maxsize = 1 ),
145- min_update_interval = timedelta (seconds = min_update_interval ),
146- )
144+ battery_pool = microgrid .battery_pool ()
145+
146+ assert microgrid ._data_pipeline ._DATA_PIPELINE is not None
147147
148148 args = SetupArgs (
149- battery_pool , min_update_interval , mock_microgrid , streamer , sender_channel
149+ battery_pool ,
150+ min_update_interval ,
151+ mock_microgrid ,
152+ streamer ,
153+ microgrid ._data_pipeline ._DATA_PIPELINE ._battery_status_channel .new_sender (),
150154 )
151155
152156 yield args
153- await asyncio .gather (* [sdk .stop (), battery_pool .stop (), streamer .stop ()])
157+ await asyncio .gather (
158+ * [
159+ microgrid ._data_pipeline ._DATA_PIPELINE ._stop (),
160+ battery_pool .stop (),
161+ streamer .stop (),
162+ ]
163+ )
164+ # pylint: enable=protected-access
154165
155166
156167@pytest .fixture
@@ -170,29 +181,39 @@ async def setup_batteries_pool(mocker: MockerFixture) -> AsyncIterator[SetupArgs
170181 mocker , ComponentGraphConfig (batteries_num = 4 )
171182 )
172183 streamer = MockComponentDataStreamer (mock_microgrid )
173- sdk = SdkInterface (resampling_period_s = 1 )
184+ min_update_interval : float = 0.2
185+ # pylint: disable=protected-access
186+ microgrid ._data_pipeline ._DATA_PIPELINE = None
187+ microgrid ._data_pipeline .initialize (ResamplerConfig (min_update_interval ))
174188
175189 # We don't use status channel from the sdk interface to limit
176190 # the scope of this tests. This tests should cover BatteryPool only.
177191 # We use our own battery status channel, where we easily control set of working
178192 # batteries.
179- battery_status_channel = Broadcast [BatteryStatus ]("bat_status" , resend_latest = True )
180- sender_channel = battery_status_channel .new_sender ()
181- min_update_interval : float = 0.2
182193 all_batteries = list (get_components (mock_microgrid , ComponentCategory .BATTERY ))
183194
184- battery_pool = BatteryPool (
185- batteries_id = set (all_batteries [:2 ]),
186- batteries_status_receiver = battery_status_channel .new_receiver (maxsize = 1 ),
187- min_update_interval = timedelta (seconds = min_update_interval ),
188- )
195+ battery_pool = microgrid .battery_pool (set (all_batteries [:2 ]))
196+
197+ assert microgrid ._data_pipeline ._DATA_PIPELINE is not None
189198
190199 args = SetupArgs (
191- battery_pool , min_update_interval , mock_microgrid , streamer , sender_channel
200+ battery_pool ,
201+ min_update_interval ,
202+ mock_microgrid ,
203+ streamer ,
204+ microgrid ._data_pipeline ._DATA_PIPELINE ._battery_status_channel .new_sender (),
192205 )
193206
194207 yield args
195- await asyncio .gather (* [sdk .stop (), battery_pool .stop (), streamer .stop ()])
208+
209+ await asyncio .gather (
210+ * [
211+ microgrid ._data_pipeline ._DATA_PIPELINE ._stop (),
212+ battery_pool .stop (),
213+ streamer .stop (),
214+ ]
215+ )
216+ # pylint: enable=protected-access
196217
197218
198219T = TypeVar ("T" )
0 commit comments