Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,4 @@

## Bug Fixes

<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
* `Timer`: Fix bug that was causing calls to `reset()` to not reset the timer, if the timer was already being awaited.
7 changes: 6 additions & 1 deletion src/frequenz/channels/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,10 +696,15 @@ async def ready(self) -> bool: # noqa: DOC502

now = self._now()
time_to_next_tick = self._next_tick_time - now

# If we didn't reach the tick yet, sleep until we do.
if time_to_next_tick > 0:
# We need to do this in a loop also reacting to the reset event, as the timer
# could be reset while we are sleeping, in which case we need to recalculated
# the time to the next tick and try again.
while time_to_next_tick > 0:
await asyncio.sleep(time_to_next_tick / 1_000_000)
now = self._now()
time_to_next_tick = self._next_tick_time - now

# If a stop was explicitly requested during the sleep, we bail out.
if self._stopped:
Expand Down
34 changes: 34 additions & 0 deletions tests/test_timer_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# License: MIT
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH

"""Integration tests for the timer."""


import asyncio
from datetime import timedelta

import async_solipsism
import pytest

from frequenz.channels.timer import Timer


@pytest.mark.integration
async def test_timer_timeout_reset(
event_loop: async_solipsism.EventLoop, # pylint: disable=redefined-outer-name
) -> None:
"""Test that the receiving is properly adjusted after a reset."""

async def timer_wait(timer: Timer) -> None:
await timer.receive()

async with asyncio.timeout(2.0):
async with asyncio.TaskGroup() as task_group:
timer = Timer.timeout(timedelta(seconds=1.0))
start_time = event_loop.time()
task_group.create_task(timer_wait(timer))
await asyncio.sleep(0.5)
timer.reset()

run_time = event_loop.time() - start_time
assert run_time >= 1.5