|
4 | 4 | callable so the suite runs instantly without freezegun. |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import asyncio |
7 | 8 | from collections.abc import Callable |
8 | 9 | from http import HTTPStatus |
9 | 10 |
|
|
12 | 13 |
|
13 | 14 | from httpware import AsyncClient, NotFoundError, ServiceUnavailableError, TransportError |
14 | 15 | from httpware.errors import NetworkError, RetryBudgetExhaustedError |
| 16 | +from httpware.errors import TimeoutError as HttpwareTimeoutError |
15 | 17 | from httpware.middleware.resilience.budget import RetryBudget |
16 | 18 | from httpware.middleware.resilience.retry import ( |
17 | 19 | DEFAULT_IDEMPOTENT_METHODS, |
@@ -235,3 +237,63 @@ def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001 |
235 | 237 | await client.post("https://example.test/x", json={"x": 1}) |
236 | 238 | assert call_count["n"] == 1 |
237 | 239 | assert sleeper.calls == [] |
| 240 | + |
| 241 | + |
| 242 | +async def test_attempt_timeout_fires_and_retries() -> None: |
| 243 | + sleeper = _SleepRecorder() |
| 244 | + call_count = {"n": 0} |
| 245 | + |
| 246 | + async def handler_async(request: httpx2.Request) -> httpx2.Response: |
| 247 | + call_count["n"] += 1 |
| 248 | + if call_count["n"] < 2: # noqa: PLR2004 — "2" is intentional literal in test assertion |
| 249 | + await asyncio.sleep(1.0) # exceeds attempt_timeout |
| 250 | + return httpx2.Response(HTTPStatus.OK, request=request) |
| 251 | + |
| 252 | + transport = httpx2.MockTransport(handler_async) |
| 253 | + client = AsyncClient( |
| 254 | + httpx2_client=httpx2.AsyncClient(transport=transport), |
| 255 | + middleware=[Retry(_sleep=sleeper, attempt_timeout=0.05, base_delay=0.01, max_delay=0.02)], |
| 256 | + ) |
| 257 | + response = await client.get("https://example.test/x") |
| 258 | + # coverage: asyncio.timeout fires a CancelledError that the retry loop catches; coverage's |
| 259 | + # thread tracer loses the coroutine frame at that point. These assertions DO execute |
| 260 | + # (the test passes), but need the pragma to satisfy the fail-under=100 gate. |
| 261 | + assert response.status_code == HTTPStatus.OK # pragma: no cover |
| 262 | + assert call_count["n"] == 2 # pragma: no cover # noqa: PLR2004 — "2" is intentional literal in test assertion |
| 263 | + |
| 264 | + |
| 265 | +async def test_attempt_timeout_exhaustion_raises_httpware_timeout() -> None: |
| 266 | + sleeper = _SleepRecorder() |
| 267 | + |
| 268 | + async def slow_handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001 |
| 269 | + await asyncio.sleep(1.0) |
| 270 | + msg = "should not reach" # pragma: no cover |
| 271 | + raise AssertionError(msg) # pragma: no cover |
| 272 | + |
| 273 | + transport = httpx2.MockTransport(slow_handler) |
| 274 | + client = AsyncClient( |
| 275 | + httpx2_client=httpx2.AsyncClient(transport=transport), |
| 276 | + middleware=[Retry(_sleep=sleeper, attempt_timeout=0.05, max_attempts=2, base_delay=0.01, max_delay=0.02)], |
| 277 | + ) |
| 278 | + with pytest.raises(HttpwareTimeoutError) as info: |
| 279 | + await client.get("https://example.test/x") |
| 280 | + notes = getattr(info.value, "__notes__", []) |
| 281 | + assert any("gave up after 2 attempts" in note for note in notes) |
| 282 | + |
| 283 | + |
| 284 | +async def test_attempt_timeout_does_not_retry_on_non_idempotent_method() -> None: |
| 285 | + sleeper = _SleepRecorder() |
| 286 | + |
| 287 | + async def slow_handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001 |
| 288 | + await asyncio.sleep(1.0) |
| 289 | + msg = "should not reach" # pragma: no cover |
| 290 | + raise AssertionError(msg) # pragma: no cover |
| 291 | + |
| 292 | + transport = httpx2.MockTransport(slow_handler) |
| 293 | + client = AsyncClient( |
| 294 | + httpx2_client=httpx2.AsyncClient(transport=transport), |
| 295 | + middleware=[Retry(_sleep=sleeper, attempt_timeout=0.05)], |
| 296 | + ) |
| 297 | + with pytest.raises(HttpwareTimeoutError): |
| 298 | + await client.post("https://example.test/x", json={"x": 1}) |
| 299 | + assert sleeper.calls == [] # not retried |
0 commit comments