Skip to content

Commit 00775ae

Browse files
lesnik512claude
andcommitted
test(circuit-breaker): cover custom failure_status_codes; clean counters on open
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 3e30e6b commit 00775ae

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎src/httpware/middleware/resilience/circuit_breaker.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ def on_failure(self, role: str, request: httpx2.Request) -> None:
152152
if self._consecutive_failures >= self._failure_threshold:
153153
self._open(request, failures=self._consecutive_failures)
154154
elif self._state is _CircuitState.HALF_OPEN:
155-
self._consecutive_successes = 0
156-
self._open(request, failures=1)
155+
self._open(request, failures=1) # 1 = the single probe failure that re-opened the circuit
157156

158157
def release_probe(self, role: str) -> None:
159158
"""Release the probe slot without recording success or failure (non-counted exc)."""
@@ -163,6 +162,8 @@ def release_probe(self, role: str) -> None:
163162
def _open(self, request: httpx2.Request, *, failures: int) -> None:
164163
self._state = _CircuitState.OPEN
165164
self._opened_at = self._now()
165+
self._consecutive_failures = 0
166+
self._consecutive_successes = 0
166167
self._emit(
167168
request,
168169
"circuit.opened",

‎tests/test_circuit_breaker.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
NetworkError,
2121
NotFoundError,
2222
RateLimitedError,
23+
ServiceUnavailableError,
2324
)
2425
from httpware.middleware.resilience.circuit_breaker import AsyncCircuitBreaker
2526

@@ -166,6 +167,38 @@ def _raise(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
166167
await client.get("https://example.test/x")
167168

168169

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+
169202
async def test_non_counted_exception_propagates_without_state_change() -> None:
170203
"""A ValueError from inner middleware is neither success nor failure; state unchanged."""
171204

0 commit comments

Comments
 (0)