Skip to content

Commit 012b610

Browse files
committed
Test wall clock timer in resampled moving window tests
These tests are very tricky and there seems to be some inconsistency between the monotonic and wall clock, the wall clock timer have less samples in the buffer at the end. This is probably some border condition because of the way we fake time. We also had to tune how to do wall clock time shifting to make sure the shift occurs before the timer wakes up. Finally we adjusted sample sending so it always sends an odd number of samples to make sure the resample is triggered at the points we expect it, otherwise again we could end up in border condition that were not the same for both timers. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 0e13568 commit 012b610

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

tests/timeseries/test_moving_window.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from frequenz.sdk.timeseries import (
2020
MovingWindow,
2121
ResamplerConfig,
22+
ResamplerConfig2,
2223
Sample,
2324
)
2425

@@ -51,8 +52,12 @@ async def push_logical_meter_data(
5152
Sample(timestamp, Quantity(float(i)) if i is not None else None)
5253
)
5354
if fake_time is not None:
54-
await asyncio.sleep(1.0)
55-
fake_time.shift(1)
55+
# For the wall clock timer we need to make sure that the wall clock is
56+
# adjusted just before the timer wakes up from sleeping, and then we need to
57+
# make sure this function returns *after* the timer has woken up.
58+
await asyncio.sleep(0.999)
59+
fake_time.shift(1.0)
60+
await asyncio.sleep(0.001)
5661

5762
await asyncio.sleep(0.0)
5863

@@ -452,12 +457,13 @@ async def test_wait_for_samples() -> None:
452457
assert task.done()
453458

454459

460+
@pytest.mark.parametrize("config_class", [ResamplerConfig, ResamplerConfig2])
455461
async def test_wait_for_samples_with_resampling(
456-
fake_time: time_machine.Coordinates,
462+
config_class: type[ResamplerConfig], fake_time: time_machine.Coordinates
457463
) -> None:
458464
"""Test waiting for samples in a moving window with resampling."""
459465
window, sender = init_moving_window(
460-
timedelta(seconds=20), ResamplerConfig(resampling_period=timedelta(seconds=2))
466+
timedelta(seconds=20), config_class(resampling_period=timedelta(seconds=2))
461467
)
462468
async with window:
463469
task = asyncio.create_task(window.wait_for_samples(3))
@@ -469,7 +475,7 @@ async def test_wait_for_samples_with_resampling(
469475
task = asyncio.create_task(window.wait_for_samples(10))
470476
await push_logical_meter_data(
471477
sender,
472-
range(0, 11),
478+
range(0, 10),
473479
fake_time=fake_time,
474480
start_ts=UNIX_EPOCH + timedelta(seconds=7),
475481
)
@@ -478,9 +484,9 @@ async def test_wait_for_samples_with_resampling(
478484

479485
await push_logical_meter_data(
480486
sender,
481-
range(0, 5),
487+
range(0, 6),
482488
fake_time=fake_time,
483-
start_ts=UNIX_EPOCH + timedelta(seconds=18),
489+
start_ts=UNIX_EPOCH + timedelta(seconds=17),
484490
)
485491
assert window.count_covered() == 10
486492
assert not task.done()
@@ -514,7 +520,11 @@ async def test_wait_for_samples_with_resampling(
514520
start_ts=UNIX_EPOCH + timedelta(seconds=39),
515521
)
516522
assert window.count_covered() == 10
517-
assert window.count_valid() == 7
523+
# There is also an inconsistency here between the monotonic and wall clock,
524+
# the wall clock timer have less samples in the buffer at the end. This is
525+
# probably some border condition because of the way we fake time
526+
is_wall_clock = config_class is ResamplerConfig2
527+
assert window.count_valid() == (5 if is_wall_clock else 7)
518528
assert task.done()
519529

520530

0 commit comments

Comments
 (0)