Skip to content

Commit 0571c51

Browse files
lesnik512claude
andcommitted
test(resilience): Hypothesis property tests for Retry
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent efe1881 commit 0571c51

1 file changed

Lines changed: 171 additions & 0 deletions

File tree

‎tests/test_retry_props.py‎

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
"""Hypothesis property tests for Retry.
2+
3+
Properties verified:
4+
1. Total attempts never exceed max_attempts.
5+
2. Total sleep time never exceeds max_attempts * max_delay.
6+
3. Non-retryable statuses (NOT in retry_status_codes) cause exactly one attempt.
7+
4. Non-idempotent methods (NOT in retry_methods) cause exactly one attempt,
8+
regardless of response status.
9+
"""
10+
11+
from http import HTTPStatus
12+
13+
import httpx2
14+
from hypothesis import given, settings
15+
from hypothesis import strategies as st
16+
17+
from httpware import AsyncClient
18+
from httpware.middleware.resilience.budget import RetryBudget
19+
from httpware.middleware.resilience.retry import (
20+
DEFAULT_IDEMPOTENT_METHODS,
21+
DEFAULT_RETRY_STATUS_CODES,
22+
Retry,
23+
)
24+
25+
26+
class _SleepRecorder:
27+
def __init__(self) -> None:
28+
self.calls: list[float] = []
29+
30+
async def __call__(self, delay: float) -> None:
31+
self.calls.append(delay)
32+
33+
34+
def _always_status(status: int) -> httpx2.MockTransport:
35+
return httpx2.MockTransport(lambda req: httpx2.Response(status, request=req))
36+
37+
38+
_RETRYABLE_STATUS_STRATEGY = st.sampled_from(sorted(DEFAULT_RETRY_STATUS_CODES))
39+
_NON_RETRYABLE_STATUS_STRATEGY = st.sampled_from(
40+
[
41+
HTTPStatus.BAD_REQUEST,
42+
HTTPStatus.UNAUTHORIZED,
43+
HTTPStatus.NOT_FOUND,
44+
HTTPStatus.CONFLICT,
45+
]
46+
)
47+
_IDEMPOTENT_METHODS = st.sampled_from(sorted(DEFAULT_IDEMPOTENT_METHODS))
48+
_NON_IDEMPOTENT_METHODS = st.sampled_from(["POST", "PATCH"])
49+
50+
51+
@given(
52+
max_attempts=st.integers(min_value=1, max_value=5),
53+
status=_RETRYABLE_STATUS_STRATEGY,
54+
method=_IDEMPOTENT_METHODS,
55+
)
56+
@settings(max_examples=50, deadline=None)
57+
async def test_total_attempts_never_exceeds_max_attempts(
58+
max_attempts: int,
59+
status: int,
60+
method: str,
61+
) -> None:
62+
sleeper = _SleepRecorder()
63+
call_count = {"n": 0}
64+
65+
def handler(request: httpx2.Request) -> httpx2.Response:
66+
call_count["n"] += 1
67+
return httpx2.Response(status, request=request)
68+
69+
transport = httpx2.MockTransport(handler)
70+
client = AsyncClient(
71+
httpx2_client=httpx2.AsyncClient(transport=transport),
72+
middleware=[
73+
Retry(
74+
_sleep=sleeper,
75+
max_attempts=max_attempts,
76+
base_delay=0.001,
77+
max_delay=0.002,
78+
budget=RetryBudget(ttl=60.0, min_retries_per_sec=1000.0),
79+
)
80+
],
81+
)
82+
try: # noqa: SIM105 — contextlib.suppress can't be used in async Hypothesis tests
83+
await client.request(method, "https://example.test/x")
84+
except Exception: # noqa: BLE001, S110 — we only care about call count
85+
pass
86+
assert call_count["n"] <= max_attempts
87+
88+
89+
@given(
90+
max_attempts=st.integers(min_value=1, max_value=5),
91+
base_delay=st.floats(min_value=0.001, max_value=0.01),
92+
max_delay=st.floats(min_value=0.001, max_value=0.05),
93+
)
94+
@settings(max_examples=30, deadline=None)
95+
async def test_total_sleep_never_exceeds_max_attempts_times_max_delay(
96+
max_attempts: int,
97+
base_delay: float,
98+
max_delay: float,
99+
) -> None:
100+
sleeper = _SleepRecorder()
101+
transport = _always_status(HTTPStatus.SERVICE_UNAVAILABLE)
102+
client = AsyncClient(
103+
httpx2_client=httpx2.AsyncClient(transport=transport),
104+
middleware=[
105+
Retry(
106+
_sleep=sleeper,
107+
max_attempts=max_attempts,
108+
base_delay=base_delay,
109+
max_delay=max_delay,
110+
budget=RetryBudget(ttl=60.0, min_retries_per_sec=1000.0),
111+
)
112+
],
113+
)
114+
try: # noqa: SIM105 — contextlib.suppress can't be used in async Hypothesis tests
115+
await client.get("https://example.test/x")
116+
except Exception: # noqa: BLE001, S110
117+
pass
118+
total = sum(sleeper.calls)
119+
assert total <= max_attempts * max_delay + 1e-9
120+
121+
122+
@given(
123+
status=_NON_RETRYABLE_STATUS_STRATEGY,
124+
method=_IDEMPOTENT_METHODS,
125+
)
126+
@settings(max_examples=30, deadline=None)
127+
async def test_non_retryable_status_causes_one_attempt(status: int, method: str) -> None:
128+
sleeper = _SleepRecorder()
129+
call_count = {"n": 0}
130+
131+
def handler(request: httpx2.Request) -> httpx2.Response:
132+
call_count["n"] += 1
133+
return httpx2.Response(status, request=request)
134+
135+
transport = httpx2.MockTransport(handler)
136+
client = AsyncClient(
137+
httpx2_client=httpx2.AsyncClient(transport=transport),
138+
middleware=[Retry(_sleep=sleeper, max_attempts=3, base_delay=0.001, max_delay=0.002)],
139+
)
140+
try: # noqa: SIM105 — contextlib.suppress can't be used in async Hypothesis tests
141+
await client.request(method, "https://example.test/x")
142+
except Exception: # noqa: BLE001, S110
143+
pass
144+
assert call_count["n"] == 1
145+
assert sleeper.calls == []
146+
147+
148+
@given(
149+
status=_RETRYABLE_STATUS_STRATEGY,
150+
method=_NON_IDEMPOTENT_METHODS,
151+
)
152+
@settings(max_examples=30, deadline=None)
153+
async def test_non_idempotent_method_causes_one_attempt(status: int, method: str) -> None:
154+
sleeper = _SleepRecorder()
155+
call_count = {"n": 0}
156+
157+
def handler(request: httpx2.Request) -> httpx2.Response:
158+
call_count["n"] += 1
159+
return httpx2.Response(status, request=request)
160+
161+
transport = httpx2.MockTransport(handler)
162+
client = AsyncClient(
163+
httpx2_client=httpx2.AsyncClient(transport=transport),
164+
middleware=[Retry(_sleep=sleeper, max_attempts=3, base_delay=0.001, max_delay=0.002)],
165+
)
166+
try: # noqa: SIM105 — contextlib.suppress can't be used in async Hypothesis tests
167+
await client.request(method, "https://example.test/x")
168+
except Exception: # noqa: BLE001, S110
169+
pass
170+
assert call_count["n"] == 1
171+
assert sleeper.calls == []

0 commit comments

Comments
 (0)