|
20 | 20 | NetworkError, |
21 | 21 | NotFoundError, |
22 | 22 | RateLimitedError, |
| 23 | + ServiceUnavailableError, |
23 | 24 | ) |
24 | 25 | from httpware.middleware.resilience.circuit_breaker import AsyncCircuitBreaker |
25 | 26 |
|
@@ -166,6 +167,38 @@ def _raise(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001 |
166 | 167 | await client.get("https://example.test/x") |
167 | 168 |
|
168 | 169 |
|
| 170 | +async def test_custom_failure_status_codes_trips_on_member() -> None: |
| 171 | + """A status code in a custom failure set trips the breaker.""" |
| 172 | + handler = _StatusSequence([503, 503]) |
| 173 | + breaker = AsyncCircuitBreaker( |
| 174 | + failure_threshold=2, |
| 175 | + failure_status_codes=frozenset({503}), |
| 176 | + _now=_Clock(), |
| 177 | + ) |
| 178 | + async with _client(handler, breaker=breaker) as client: |
| 179 | + for _ in range(2): |
| 180 | + with pytest.raises(ServiceUnavailableError): |
| 181 | + await client.get("https://example.test/x") |
| 182 | + with pytest.raises(CircuitOpenError): |
| 183 | + await client.get("https://example.test/x") |
| 184 | + assert handler.calls == 2 # noqa: PLR2004 |
| 185 | + |
| 186 | + |
| 187 | +async def test_custom_failure_status_codes_excludes_other_5xx() -> None: |
| 188 | + """With a custom set of {503}, a 500 response is NOT a failure — it counts as success.""" |
| 189 | + handler = _StatusSequence([500, 500, 500, 500]) |
| 190 | + breaker = AsyncCircuitBreaker( |
| 191 | + failure_threshold=2, |
| 192 | + failure_status_codes=frozenset({503}), |
| 193 | + _now=_Clock(), |
| 194 | + ) |
| 195 | + async with _client(handler, breaker=breaker) as client: |
| 196 | + for _ in range(4): |
| 197 | + with pytest.raises(InternalServerError): |
| 198 | + await client.get("https://example.test/x") |
| 199 | + assert handler.calls == 4 # noqa: PLR2004 # 500 not in custom set -> never opened |
| 200 | + |
| 201 | + |
169 | 202 | async def test_non_counted_exception_propagates_without_state_change() -> None: |
170 | 203 | """A ValueError from inner middleware is neither success nor failure; state unchanged.""" |
171 | 204 |
|
|
0 commit comments