|
17 | 17 |
|
18 | 18 | import async_solipsism |
19 | 19 | import pytest |
| 20 | +import time_machine |
20 | 21 | from frequenz.channels import Receiver, Sender |
21 | 22 | from pytest_mock import MockerFixture |
22 | 23 |
|
@@ -296,22 +297,28 @@ async def run_scenarios( |
296 | 297 | raise err |
297 | 298 |
|
298 | 299 |
|
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: |
300 | 303 | """Test capacity metric for battery pool with all components in the microgrid. |
301 | 304 |
|
302 | 305 | Args: |
| 306 | + fake_time: The time machine fake time argument. |
303 | 307 | setup_all_batteries: Fixture that creates needed microgrid tools. |
304 | 308 | """ |
305 | | - await run_capacity_test(setup_all_batteries) |
| 309 | + await run_capacity_test(fake_time, setup_all_batteries) |
306 | 310 |
|
307 | 311 |
|
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: |
309 | 315 | """Test capacity metric for battery pool with subset of components in the microgrid. |
310 | 316 |
|
311 | 317 | Args: |
| 318 | + fake_time: The time machine fake time argument. |
312 | 319 | setup_batteries_pool: Fixture that creates needed microgrid tools. |
313 | 320 | """ |
314 | | - await run_capacity_test(setup_batteries_pool) |
| 321 | + await run_capacity_test(fake_time, setup_batteries_pool) |
315 | 322 |
|
316 | 323 |
|
317 | 324 | async def test_all_batteries_soc(setup_all_batteries: SetupArgs) -> None: |
@@ -499,10 +506,13 @@ async def test_battery_pool_power(mocker: MockerFixture) -> None: |
499 | 506 | await mockgrid.cleanup() |
500 | 507 |
|
501 | 508 |
|
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: |
503 | 512 | """Test if capacity metric is working as expected. |
504 | 513 |
|
505 | 514 | Args: |
| 515 | + fake_time: The time machine fake time argument. |
506 | 516 | setup_args: Needed sdk tools and tools for mocking microgrid. |
507 | 517 | """ |
508 | 518 | battery_pool = setup_args.battery_pool |
@@ -666,20 +676,30 @@ async def run_capacity_test(setup_args: SetupArgs) -> None: |
666 | 676 | # One battery stopped sending data. |
667 | 677 | await streamer.stop_streaming(batteries_in_pool[1]) |
668 | 678 | await asyncio.sleep(MAX_BATTERY_DATA_AGE_SEC + 0.2) |
| 679 | + fake_time.shift(MAX_BATTERY_DATA_AGE_SEC + 0.2) |
669 | 680 | 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 | + ) |
671 | 685 |
|
672 | 686 | # All batteries stopped sending data. |
673 | 687 | await streamer.stop_streaming(batteries_in_pool[0]) |
674 | 688 | await asyncio.sleep(MAX_BATTERY_DATA_AGE_SEC + 0.2) |
| 689 | + fake_time.shift(MAX_BATTERY_DATA_AGE_SEC + 0.2) |
675 | 690 | msg = await asyncio.wait_for(capacity_receiver.receive(), timeout=waiting_time_sec) |
676 | 691 | assert isinstance(msg, Sample) and msg.value is None |
677 | 692 |
|
678 | 693 | # One battery started sending data. |
679 | 694 | latest_data = streamer.get_current_component_data(batteries_in_pool[0]) |
680 | 695 | streamer.start_streaming(latest_data, sampling_rate=0.1) |
681 | 696 | 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 | + ) |
683 | 703 |
|
684 | 704 |
|
685 | 705 | async def run_soc_test(setup_args: SetupArgs) -> None: |
|
0 commit comments