1414from pytest_mock import MockerFixture
1515
1616from frequenz .sdk import microgrid , timeseries
17+ from frequenz .sdk ._internal ._channels import LatestValueCache
1718from frequenz .sdk .actor import ResamplerConfig , power_distributing
1819from frequenz .sdk .actor .power_distributing import ComponentPoolStatus
1920from frequenz .sdk .actor .power_distributing ._component_pool_status_tracker import (
@@ -161,6 +162,7 @@ def _assert_report( # pylint: disable=too-many-arguments
161162 power : float | None ,
162163 lower : float ,
163164 upper : float ,
165+ dist_result : power_distributing .Result | None = None ,
164166 expected_result_pred : (
165167 typing .Callable [[power_distributing .Result ], bool ] | None
166168 ) = None ,
@@ -172,8 +174,8 @@ def _assert_report( # pylint: disable=too-many-arguments
172174 assert report .bounds .lower == Power .from_watts (lower )
173175 assert report .bounds .upper == Power .from_watts (upper )
174176 if expected_result_pred is not None :
175- assert report . distribution_result is not None
176- assert expected_result_pred (report . distribution_result )
177+ assert dist_result is not None
178+ assert expected_result_pred (dist_result )
177179
178180 async def test_case_1 (
179181 self ,
@@ -196,6 +198,9 @@ async def test_case_1(
196198 battery_pool = microgrid .new_battery_pool (priority = 5 )
197199
198200 bounds_rx = battery_pool .power_status .new_receiver ()
201+ latest_dist_result = LatestValueCache (
202+ battery_pool .power_distribution_results .new_receiver ()
203+ )
199204
200205 self ._assert_report (
201206 await bounds_rx .receive (), power = None , lower = - 4000.0 , upper = 4000.0
@@ -217,6 +222,7 @@ async def test_case_1(
217222 power = 1000.0 ,
218223 lower = - 4000.0 ,
219224 upper = 4000.0 ,
225+ dist_result = latest_dist_result .get (),
220226 expected_result_pred = lambda result : isinstance (
221227 result , power_distributing .Success
222228 ),
@@ -236,6 +242,7 @@ async def side_effect(inv_id: int, _: float) -> None:
236242 power = 100.0 ,
237243 lower = - 4000.0 ,
238244 upper = 4000.0 ,
245+ dist_result = latest_dist_result .get (),
239246 expected_result_pred = lambda result : isinstance (
240247 result , power_distributing .Success
241248 ),
@@ -250,6 +257,7 @@ async def side_effect(inv_id: int, _: float) -> None:
250257 power = 100.0 ,
251258 lower = - 4000.0 ,
252259 upper = 4000.0 ,
260+ dist_result = latest_dist_result .get (),
253261 expected_result_pred = lambda result : isinstance (
254262 result , power_distributing .PartialFailure
255263 )
@@ -267,6 +275,7 @@ async def side_effect(inv_id: int, _: float) -> None:
267275 power = 100.0 ,
268276 lower = - 4000.0 ,
269277 upper = 4000.0 ,
278+ dist_result = latest_dist_result .get (),
270279 expected_result_pred = lambda result : isinstance (
271280 result , power_distributing .Success
272281 ),
@@ -294,6 +303,9 @@ async def test_case_2(self, mocks: Mocks, mocker: MockerFixture) -> None:
294303 priority = 5 , component_ids = set (mocks .microgrid .battery_ids [2 :])
295304 )
296305 bounds_2_rx = battery_pool_2 .power_status .new_receiver ()
306+ latest_dist_result_2 = LatestValueCache (
307+ battery_pool_2 .power_distribution_results .new_receiver ()
308+ )
297309
298310 self ._assert_report (
299311 await bounds_1_rx .receive (), power = None , lower = - 2000.0 , upper = 2000.0
@@ -313,8 +325,9 @@ async def test_case_2(self, mocks: Mocks, mocker: MockerFixture) -> None:
313325 set_power .reset_mock ()
314326
315327 await battery_pool_2 .propose_power (Power .from_watts (1000.0 ))
328+
316329 bounds = await bounds_2_rx .receive ()
317- if bounds . distribution_result is None :
330+ if not latest_dist_result_2 . has_value () :
318331 bounds = await bounds_2_rx .receive ()
319332 self ._assert_report (bounds , power = 1000.0 , lower = - 2000.0 , upper = 2000.0 )
320333 assert set_power .call_count == 2
@@ -341,6 +354,9 @@ async def test_case_3(self, mocks: Mocks, mocker: MockerFixture) -> None:
341354 bounds_1_rx = battery_pool_1 .power_status .new_receiver ()
342355 battery_pool_2 = microgrid .new_battery_pool (priority = 1 )
343356 bounds_2_rx = battery_pool_2 .power_status .new_receiver ()
357+ latest_dist_result_2 = LatestValueCache (
358+ battery_pool_2 .power_distribution_results .new_receiver ()
359+ )
344360
345361 self ._assert_report (
346362 await bounds_1_rx .receive (), power = None , lower = - 4000.0 , upper = 4000.0
@@ -374,7 +390,7 @@ async def test_case_3(self, mocks: Mocks, mocker: MockerFixture) -> None:
374390 await bounds_1_rx .receive (), power = 0.0 , lower = - 4000.0 , upper = 4000.0
375391 )
376392 bounds = await bounds_2_rx .receive ()
377- if bounds . distribution_result is None :
393+ if not latest_dist_result_2 . has_value () :
378394 bounds = await bounds_2_rx .receive ()
379395 self ._assert_report (bounds , power = 0.0 , lower = - 1000.0 , upper = 0.0 )
380396
@@ -398,6 +414,9 @@ async def test_case_4(self, mocks: Mocks, mocker: MockerFixture) -> None:
398414
399415 battery_pool = microgrid .new_battery_pool (priority = 5 )
400416 bounds_rx = battery_pool .power_status .new_receiver ()
417+ latest_dist_result = LatestValueCache (
418+ battery_pool .power_distribution_results .new_receiver ()
419+ )
401420
402421 self ._assert_report (
403422 await bounds_rx .receive (), power = None , lower = - 4000.0 , upper = 4000.0
@@ -418,6 +437,7 @@ async def test_case_4(self, mocks: Mocks, mocker: MockerFixture) -> None:
418437 power = 1000.0 ,
419438 lower = - 4000.0 ,
420439 upper = 4000.0 ,
440+ dist_result = latest_dist_result .get (),
421441 expected_result_pred = lambda result : isinstance (
422442 result , power_distributing .Success
423443 ),
@@ -442,6 +462,7 @@ async def test_case_4(self, mocks: Mocks, mocker: MockerFixture) -> None:
442462 power = 400.0 ,
443463 lower = - 4000.0 ,
444464 upper = 4000.0 ,
465+ dist_result = latest_dist_result .get (),
445466 expected_result_pred = lambda result : isinstance (
446467 result , power_distributing .Success
447468 ),
@@ -464,6 +485,7 @@ async def test_case_4(self, mocks: Mocks, mocker: MockerFixture) -> None:
464485 power = 0.0 ,
465486 lower = - 4000.0 ,
466487 upper = 4000.0 ,
488+ dist_result = latest_dist_result .get (),
467489 expected_result_pred = lambda result : isinstance (
468490 result , power_distributing .Success
469491 ),
@@ -488,12 +510,13 @@ async def test_case_4(self, mocks: Mocks, mocker: MockerFixture) -> None:
488510 power = - 400.0 ,
489511 lower = - 4000.0 ,
490512 upper = 4000.0 ,
513+ dist_result = latest_dist_result .get (),
491514 expected_result_pred = lambda result : isinstance (
492515 result , power_distributing .Success
493516 ),
494517 )
495518
496- async def test_case_5 ( # pylint: disable=too-many-statements
519+ async def test_case_5 ( # pylint: disable=too-many-statements,too-many-locals
497520 self ,
498521 mocks : Mocks ,
499522 mocker : MockerFixture ,
@@ -525,6 +548,10 @@ async def test_case_5( # pylint: disable=too-many-statements
525548 battery_pool_1 = microgrid .new_battery_pool (priority = 1 )
526549 bounds_1_rx = battery_pool_1 .power_status .new_receiver ()
527550
551+ latest_dist_result_4 = LatestValueCache (
552+ battery_pool_4 .power_distribution_results .new_receiver ()
553+ )
554+
528555 self ._assert_report (
529556 await bounds_4_rx .receive (), power = None , lower = - 4000.0 , upper = 4000.0
530557 )
@@ -587,12 +614,13 @@ async def test_case_5( # pylint: disable=too-many-statements
587614 await bounds_1_rx .receive ()
588615 await bounds_2_rx .receive ()
589616 await bounds_3_rx .receive ()
590- bounds = await bounds_4_rx .receive ()
591- if bounds .distribution_result is None or not isinstance (
592- bounds .distribution_result , power_distributing .Success
617+ await bounds_4_rx .receive ()
618+ dist_result = latest_dist_result_4 .get ()
619+ if dist_result is None or not isinstance (
620+ dist_result , power_distributing .Success
593621 ):
594622 continue
595- if bounds . distribution_result .succeeded_power == Power .from_watts (720.0 ):
623+ if dist_result .succeeded_power == Power .from_watts (720.0 ):
596624 break
597625
598626 assert set_power .call_count == 4
@@ -626,12 +654,13 @@ async def test_case_5( # pylint: disable=too-many-statements
626654 await bounds_1_rx .receive ()
627655 await bounds_2_rx .receive ()
628656 await bounds_3_rx .receive ()
629- bounds = await bounds_4_rx .receive ()
630- if bounds .distribution_result is None or not isinstance (
631- bounds .distribution_result , power_distributing .Success
657+ await bounds_4_rx .receive ()
658+ dist_result = latest_dist_result_4 .get ()
659+ if dist_result is None or not isinstance (
660+ dist_result , power_distributing .Success
632661 ):
633662 continue
634- if bounds . distribution_result .succeeded_power == Power .from_watts (- 280.0 ):
663+ if dist_result .succeeded_power == Power .from_watts (- 280.0 ):
635664 break
636665
637666 assert set_power .call_count == 4
0 commit comments