|
26 | 26 | Sink, |
27 | 27 | Source, |
28 | 28 | SourceProperties, |
| 29 | + WindowSide, |
29 | 30 | ) |
30 | 31 | from frequenz.sdk.timeseries._resampling._exceptions import ( |
31 | 32 | ResamplingError, |
@@ -1504,6 +1505,177 @@ async def test_resampling_all_zeros( |
1504 | 1505 | assert _get_buffer_len(resampler, source_receiver) == 3 |
1505 | 1506 |
|
1506 | 1507 |
|
| 1508 | +@pytest.mark.parametrize("closed", [WindowSide.RIGHT, WindowSide.LEFT]) |
| 1509 | +async def test_resampler_closed_option( |
| 1510 | + closed: WindowSide, |
| 1511 | + fake_time: time_machine.Coordinates, |
| 1512 | + source_chan: Broadcast[Sample[Quantity]], |
| 1513 | +) -> None: |
| 1514 | + """Test the `closed` option in ResamplerConfig.""" |
| 1515 | + timestamp = datetime.now(timezone.utc) |
| 1516 | + |
| 1517 | + resampling_period_s = 2 |
| 1518 | + expected_resampled_value = 42.0 |
| 1519 | + |
| 1520 | + resampling_fun_mock = MagicMock( |
| 1521 | + spec=ResamplingFunction, return_value=expected_resampled_value |
| 1522 | + ) |
| 1523 | + config = ResamplerConfig( |
| 1524 | + resampling_period=timedelta(seconds=resampling_period_s), |
| 1525 | + max_data_age_in_periods=1.0, |
| 1526 | + resampling_function=resampling_fun_mock, |
| 1527 | + closed=closed, |
| 1528 | + ) |
| 1529 | + resampler = Resampler(config) |
| 1530 | + |
| 1531 | + source_receiver = source_chan.new_receiver() |
| 1532 | + source_sender = source_chan.new_sender() |
| 1533 | + |
| 1534 | + sink_mock = AsyncMock(spec=Sink, return_value=True) |
| 1535 | + |
| 1536 | + resampler.add_timeseries("test", source_receiver, sink_mock) |
| 1537 | + source_props = resampler.get_source_properties(source_receiver) |
| 1538 | + |
| 1539 | + # Test timeline |
| 1540 | + # |
| 1541 | + # t(s) 0 1 2 2.5 3 4 |
| 1542 | + # |----------|----------R----|-----|----------R-----> (no more samples) |
| 1543 | + # value 5.0 10.0 15.0 1.0 4.0 5.0 |
| 1544 | + # |
| 1545 | + # R = resampling is done |
| 1546 | + |
| 1547 | + # Send a few samples and run a resample tick, advancing the fake time by one period |
| 1548 | + sample1 = Sample(timestamp, value=Quantity(5.0)) |
| 1549 | + sample2 = Sample(timestamp + timedelta(seconds=1), value=Quantity(10.0)) |
| 1550 | + sample3 = Sample(timestamp + timedelta(seconds=2), value=Quantity(15.0)) |
| 1551 | + await source_sender.send(sample1) |
| 1552 | + await source_sender.send(sample2) |
| 1553 | + await source_sender.send(sample3) |
| 1554 | + |
| 1555 | + await _advance_time(fake_time, resampling_period_s) |
| 1556 | + await resampler.resample(one_shot=True) |
| 1557 | + |
| 1558 | + assert datetime.now(timezone.utc).timestamp() == 2 |
| 1559 | + sink_mock.assert_called_once_with( |
| 1560 | + Sample( |
| 1561 | + timestamp + timedelta(seconds=resampling_period_s), |
| 1562 | + Quantity(expected_resampled_value), |
| 1563 | + ) |
| 1564 | + ) |
| 1565 | + # Assert the behavior based on the `closed` option |
| 1566 | + if closed == WindowSide.RIGHT: |
| 1567 | + resampling_fun_mock.assert_called_once_with( |
| 1568 | + a_sequence(as_float_tuple(sample2), as_float_tuple(sample3)), |
| 1569 | + config, |
| 1570 | + source_props, |
| 1571 | + ) |
| 1572 | + elif closed == WindowSide.LEFT: |
| 1573 | + resampling_fun_mock.assert_called_once_with( |
| 1574 | + a_sequence(as_float_tuple(sample1), as_float_tuple(sample2)), |
| 1575 | + config, |
| 1576 | + source_props, |
| 1577 | + ) |
| 1578 | + assert source_props == SourceProperties( |
| 1579 | + sampling_start=timestamp, received_samples=3, sampling_period=None |
| 1580 | + ) |
| 1581 | + assert _get_buffer_len(resampler, source_receiver) == config.initial_buffer_len |
| 1582 | + sink_mock.reset_mock() |
| 1583 | + resampling_fun_mock.reset_mock() |
| 1584 | + |
| 1585 | + # Additional samples at 2.5, 3, and 4 seconds |
| 1586 | + sample4 = Sample(timestamp + timedelta(seconds=2.5), value=Quantity(1.0)) |
| 1587 | + sample5 = Sample(timestamp + timedelta(seconds=3), value=Quantity(4.0)) |
| 1588 | + sample6 = Sample(timestamp + timedelta(seconds=4), value=Quantity(5.0)) |
| 1589 | + await source_sender.send(sample4) |
| 1590 | + await source_sender.send(sample5) |
| 1591 | + await source_sender.send(sample6) |
| 1592 | + |
| 1593 | + # Advance time to 4 seconds and resample again |
| 1594 | + await _advance_time(fake_time, resampling_period_s * 2) |
| 1595 | + await resampler.resample(one_shot=True) |
| 1596 | + |
| 1597 | + sink_mock.assert_called_once_with( |
| 1598 | + Sample( |
| 1599 | + timestamp + timedelta(seconds=resampling_period_s * 2), |
| 1600 | + Quantity(expected_resampled_value), |
| 1601 | + ) |
| 1602 | + ) |
| 1603 | + if closed == WindowSide.RIGHT: |
| 1604 | + resampling_fun_mock.assert_called_once_with( |
| 1605 | + a_sequence( |
| 1606 | + as_float_tuple(sample4), |
| 1607 | + as_float_tuple(sample5), |
| 1608 | + as_float_tuple(sample6), |
| 1609 | + ), |
| 1610 | + config, |
| 1611 | + source_props, |
| 1612 | + ) |
| 1613 | + elif closed == WindowSide.LEFT: |
| 1614 | + resampling_fun_mock.assert_called_once_with( |
| 1615 | + a_sequence( |
| 1616 | + as_float_tuple(sample3), |
| 1617 | + as_float_tuple(sample4), |
| 1618 | + as_float_tuple(sample5), |
| 1619 | + ), |
| 1620 | + config, |
| 1621 | + source_props, |
| 1622 | + ) |
| 1623 | + assert source_props == SourceProperties( |
| 1624 | + sampling_start=timestamp, received_samples=6, sampling_period=None |
| 1625 | + ) |
| 1626 | + assert _get_buffer_len(resampler, source_receiver) == config.initial_buffer_len |
| 1627 | + |
| 1628 | + |
| 1629 | +@pytest.mark.parametrize("label", [WindowSide.LEFT, WindowSide.RIGHT]) |
| 1630 | +async def test_resampler_label_option( |
| 1631 | + label: WindowSide, |
| 1632 | + fake_time: time_machine.Coordinates, |
| 1633 | + source_chan: Broadcast[Sample[Quantity]], |
| 1634 | +) -> None: |
| 1635 | + """Test the `label` option in ResamplerConfig.""" |
| 1636 | + timestamp = datetime.now(timezone.utc) |
| 1637 | + |
| 1638 | + resampling_period_s = 2 |
| 1639 | + expected_resampled_value = 42.0 |
| 1640 | + |
| 1641 | + resampling_fun_mock = MagicMock( |
| 1642 | + spec=ResamplingFunction, return_value=expected_resampled_value |
| 1643 | + ) |
| 1644 | + config = ResamplerConfig( |
| 1645 | + resampling_period=timedelta(seconds=resampling_period_s), |
| 1646 | + max_data_age_in_periods=1.0, |
| 1647 | + resampling_function=resampling_fun_mock, |
| 1648 | + label=label, |
| 1649 | + ) |
| 1650 | + resampler = Resampler(config) |
| 1651 | + |
| 1652 | + source_receiver = source_chan.new_receiver() |
| 1653 | + source_sender = source_chan.new_sender() |
| 1654 | + |
| 1655 | + sink_mock = AsyncMock(spec=Sink, return_value=True) |
| 1656 | + |
| 1657 | + resampler.add_timeseries("test", source_receiver, sink_mock) |
| 1658 | + |
| 1659 | + # Send samples and resample |
| 1660 | + sample1 = Sample(timestamp, value=Quantity(5.0)) |
| 1661 | + sample2 = Sample(timestamp + timedelta(seconds=1), value=Quantity(10.0)) |
| 1662 | + await source_sender.send(sample1) |
| 1663 | + await source_sender.send(sample2) |
| 1664 | + |
| 1665 | + await _advance_time(fake_time, resampling_period_s) |
| 1666 | + await resampler.resample(one_shot=True) |
| 1667 | + |
| 1668 | + # Assert the timestamp of the resampled sample |
| 1669 | + expected_timestamp = ( |
| 1670 | + timestamp |
| 1671 | + if label == WindowSide.LEFT |
| 1672 | + else timestamp + timedelta(seconds=resampling_period_s) |
| 1673 | + ) |
| 1674 | + sink_mock.assert_called_once_with( |
| 1675 | + Sample(expected_timestamp, Quantity(expected_resampled_value)) |
| 1676 | + ) |
| 1677 | + |
| 1678 | + |
1507 | 1679 | def _get_buffer_len(resampler: Resampler, source_receiver: Source) -> int: |
1508 | 1680 | # pylint: disable-next=protected-access |
1509 | 1681 | blen = resampler._resamplers[source_receiver]._helper._buffer.maxlen |
|
0 commit comments