Skip to content

Commit 0932405

Browse files
fix: test_respects_max_delay now actually verifies delay capping
Previously only checked len(timestamps)==3 without verifying the delay was capped. Now mocks asyncio.sleep and asserts the sleep argument is capped at max_delay. Uses jitter=0 for determinism. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 506c066 commit 0932405

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

tests/test_retry.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Tests for retry utilities."""
22

3+
import asyncio
4+
35
import pytest
46

57
from dqliteclient.retry import retry_with_backoff
@@ -71,20 +73,33 @@ async def raise_type_error() -> str:
7173
assert call_count == 1 # Should not retry
7274

7375
async def test_respects_max_delay(self) -> None:
74-
import time
76+
"""Verify that the backoff delay is capped at max_delay."""
77+
from unittest.mock import AsyncMock, patch
7578

7679
call_count = 0
77-
timestamps: list[float] = []
80+
sleep_args: list[float] = []
7881

79-
async def track_time() -> str:
82+
async def fail_twice() -> str:
8083
nonlocal call_count
81-
timestamps.append(time.monotonic())
8284
call_count += 1
8385
if call_count < 3:
8486
raise ValueError("not yet")
8587
return "ok"
8688

87-
await retry_with_backoff(track_time, max_attempts=3, base_delay=0.01, max_delay=0.02)
89+
original_sleep = asyncio.sleep
90+
91+
async def mock_sleep(delay: float) -> None:
92+
sleep_args.append(delay)
93+
await original_sleep(0) # Yield control without actual delay
94+
95+
with patch("dqliteclient.retry.asyncio.sleep", side_effect=mock_sleep):
96+
await retry_with_backoff(
97+
fail_twice, max_attempts=3, base_delay=0.1, max_delay=0.05, jitter=0
98+
)
8899

89-
# Second retry delay should be capped at max_delay
90-
assert len(timestamps) == 3
100+
assert call_count == 3
101+
assert len(sleep_args) == 2 # Two retries = two sleeps
102+
# First delay: 0.1 * 2^0 = 0.1, capped to 0.05
103+
assert sleep_args[0] == pytest.approx(0.05)
104+
# Second delay: 0.1 * 2^1 = 0.2, capped to 0.05
105+
assert sleep_args[1] == pytest.approx(0.05)

0 commit comments

Comments
 (0)