|
10 | 10 | import httpx2 |
11 | 11 | import pytest |
12 | 12 |
|
13 | | -from httpware import AsyncClient, NotFoundError, ServiceUnavailableError |
14 | | -from httpware.errors import RetryBudgetExhaustedError |
| 13 | +from httpware import AsyncClient, NotFoundError, ServiceUnavailableError, TransportError |
| 14 | +from httpware.errors import NetworkError, RetryBudgetExhaustedError |
15 | 15 | from httpware.middleware.resilience.budget import RetryBudget |
16 | 16 | from httpware.middleware.resilience.retry import ( |
17 | 17 | DEFAULT_IDEMPOTENT_METHODS, |
@@ -155,3 +155,82 @@ async def test_budget_exhausted_raises_retry_budget_exhausted_error() -> None: |
155 | 155 | assert handler.calls == 1 |
156 | 156 | assert info.value.attempts == 1 |
157 | 157 | assert sleeper.calls == [] |
| 158 | + |
| 159 | + |
| 160 | +async def test_retries_on_network_error() -> None: |
| 161 | + sleeper = _SleepRecorder() |
| 162 | + call_count = {"n": 0} |
| 163 | + |
| 164 | + def handler(request: httpx2.Request) -> httpx2.Response: |
| 165 | + call_count["n"] += 1 |
| 166 | + if call_count["n"] < 2: # noqa: PLR2004 — "2" is intentional literal in test assertion |
| 167 | + msg = "transient" |
| 168 | + raise httpx2.ConnectError(msg) |
| 169 | + return httpx2.Response(HTTPStatus.OK, request=request) |
| 170 | + |
| 171 | + client = _client(handler, retry=Retry(_sleep=sleeper, base_delay=0.01, max_delay=0.02)) |
| 172 | + response = await client.get("https://example.test/x") |
| 173 | + assert response.status_code == HTTPStatus.OK |
| 174 | + assert call_count["n"] == 2 # noqa: PLR2004 — "2" is intentional literal in test assertion |
| 175 | + assert len(sleeper.calls) == 1 |
| 176 | + |
| 177 | + |
| 178 | +async def test_retries_on_httpware_timeout_error() -> None: |
| 179 | + sleeper = _SleepRecorder() |
| 180 | + call_count = {"n": 0} |
| 181 | + |
| 182 | + def handler(request: httpx2.Request) -> httpx2.Response: |
| 183 | + call_count["n"] += 1 |
| 184 | + if call_count["n"] < 2: # noqa: PLR2004 — "2" is intentional literal in test assertion |
| 185 | + msg = "read timeout" |
| 186 | + raise httpx2.ReadTimeout(msg) |
| 187 | + return httpx2.Response(HTTPStatus.OK, request=request) |
| 188 | + |
| 189 | + client = _client(handler, retry=Retry(_sleep=sleeper, base_delay=0.01, max_delay=0.02)) |
| 190 | + response = await client.get("https://example.test/x") |
| 191 | + assert response.status_code == HTTPStatus.OK |
| 192 | + assert call_count["n"] == 2 # noqa: PLR2004 — "2" is intentional literal in test assertion |
| 193 | + |
| 194 | + |
| 195 | +async def test_does_not_retry_on_bare_transport_error_like_invalid_url() -> None: |
| 196 | + sleeper = _SleepRecorder() |
| 197 | + |
| 198 | + def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001 |
| 199 | + msg = "bad url" |
| 200 | + raise httpx2.InvalidURL(msg) |
| 201 | + |
| 202 | + client = _client(handler, retry=Retry(_sleep=sleeper)) |
| 203 | + with pytest.raises(TransportError) as info: |
| 204 | + await client.get("https://example.test/x") |
| 205 | + assert not isinstance(info.value, NetworkError) |
| 206 | + assert sleeper.calls == [] |
| 207 | + |
| 208 | + |
| 209 | +async def test_network_error_exhaustion_reraises_with_note() -> None: |
| 210 | + sleeper = _SleepRecorder() |
| 211 | + |
| 212 | + def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001 |
| 213 | + msg = "never works" |
| 214 | + raise httpx2.ConnectError(msg) |
| 215 | + |
| 216 | + client = _client(handler, retry=Retry(_sleep=sleeper, max_attempts=2, base_delay=0.01, max_delay=0.02)) |
| 217 | + with pytest.raises(NetworkError) as info: |
| 218 | + await client.get("https://example.test/x") |
| 219 | + notes = getattr(info.value, "__notes__", []) |
| 220 | + assert any("gave up after 2 attempts" in note for note in notes) |
| 221 | + |
| 222 | + |
| 223 | +async def test_does_not_retry_network_error_on_non_idempotent_method() -> None: |
| 224 | + sleeper = _SleepRecorder() |
| 225 | + call_count = {"n": 0} |
| 226 | + |
| 227 | + def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001 |
| 228 | + call_count["n"] += 1 |
| 229 | + msg = "transient" |
| 230 | + raise httpx2.ConnectError(msg) |
| 231 | + |
| 232 | + client = _client(handler, retry=Retry(_sleep=sleeper)) |
| 233 | + with pytest.raises(NetworkError): |
| 234 | + await client.post("https://example.test/x", json={"x": 1}) |
| 235 | + assert call_count["n"] == 1 |
| 236 | + assert sleeper.calls == [] |
0 commit comments