Skip to content
Draft
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
9 changes: 6 additions & 3 deletions linode_api4/polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ def __init__(self, timeout_seconds=120):
self._start_time = datetime.datetime.now()
self._timeout_seconds = timeout_seconds

def start(self, start_time=datetime.datetime.now()):
def start(self, start_time=None):
"""
Sets the timeout start time to the current time.

:param start_time: The moment when the context started.
:param start_time: The moment when the context started. If omitted,
use the current time when this method is called.
:type start_time: datetime
"""
self._start_time = start_time
self._start_time = (
datetime.datetime.now() if start_time is None else start_time
)

def extend(self, seconds: int):
"""
Expand Down
37 changes: 37 additions & 0 deletions test/unit/polling_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import datetime
from unittest import TestCase
from unittest.mock import patch

from linode_api4.polling import TimeoutContext


class TimeoutContextTest(TestCase):
def test_start_restarts_an_expired_timeout(self):
started_at = datetime.datetime(2026, 1, 1)
with patch(
"linode_api4.polling.datetime.datetime", wraps=datetime.datetime
) as clock:
clock.now.return_value = started_at
context = TimeoutContext(timeout_seconds=10)
context.start(started_at)

for elapsed in (30, 60):
clock.now.return_value = started_at + datetime.timedelta(
seconds=elapsed
)
self.assertTrue(context.expired)
context.start()
self.assertEqual(context.seconds_remaining, 10)
self.assertTrue(context.valid)

def test_start_preserves_explicit_start_time(self):
started_at = datetime.datetime(2026, 1, 1)
now = started_at + datetime.timedelta(seconds=7)
with patch(
"linode_api4.polling.datetime.datetime", wraps=datetime.datetime
) as clock:
clock.now.return_value = now
context = TimeoutContext(timeout_seconds=10)
context.start(started_at)
self.assertEqual(context.seconds_remaining, 3)
self.assertTrue(context.valid)