Skip to content

Commit c59b301

Browse files
lesnik512claude
andcommitted
test(circuit-breaker): property test — OPEN never forwards pre-timeout
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent f10f934 commit c59b301

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""Property test: while OPEN and before reset_timeout, the breaker never forwards.
2+
3+
Drives the AsyncCircuitBreaker directly with a stub `next` that records calls.
4+
Hypothesis generates random advance/outcome sequences. Time is injected via a clock.
5+
"""
6+
7+
import httpx2
8+
import pytest
9+
from hypothesis import given, settings
10+
from hypothesis import strategies as st
11+
12+
from httpware import CircuitOpenError, InternalServerError
13+
from httpware.middleware.resilience.circuit_breaker import AsyncCircuitBreaker
14+
15+
16+
class _Clock:
17+
def __init__(self) -> None:
18+
self.t = 0.0
19+
20+
def __call__(self) -> float:
21+
return self.t
22+
23+
24+
def _request() -> httpx2.Request:
25+
return httpx2.Request("GET", "https://example.test/x")
26+
27+
28+
@given(
29+
failure_threshold=st.integers(min_value=1, max_value=5),
30+
reset_timeout=st.floats(min_value=1.0, max_value=100.0),
31+
advances=st.lists(st.floats(min_value=0.0, max_value=0.5), min_size=1, max_size=20),
32+
)
33+
@settings(max_examples=50, deadline=None)
34+
async def test_open_circuit_never_forwards_before_reset_timeout(
35+
failure_threshold: int,
36+
reset_timeout: float,
37+
advances: list[float],
38+
) -> None:
39+
clock = _Clock()
40+
breaker = AsyncCircuitBreaker(
41+
failure_threshold=failure_threshold,
42+
reset_timeout=reset_timeout,
43+
_now=clock,
44+
)
45+
forwarded = 0
46+
47+
async def _ok(request: httpx2.Request) -> httpx2.Response:
48+
nonlocal forwarded
49+
forwarded += 1 # pragma: no cover — invariant: OPEN never forwards, so this never runs
50+
return httpx2.Response(200, request=request) # pragma: no cover
51+
52+
async def _five_hundred(request: httpx2.Request) -> httpx2.Response:
53+
raise InternalServerError(httpx2.Response(500, request=request))
54+
55+
# Open the circuit: failure_threshold consecutive 500s.
56+
for _ in range(failure_threshold):
57+
with pytest.raises(InternalServerError):
58+
await breaker(_request(), _five_hundred)
59+
60+
# Now OPEN. Advance the clock in small steps that stay strictly below reset_timeout.
61+
calls_before = forwarded
62+
total = 0.0
63+
for step in advances:
64+
if total + step >= reset_timeout:
65+
break
66+
total += step
67+
clock.t = total
68+
with pytest.raises(CircuitOpenError):
69+
await breaker(_request(), _ok)
70+
71+
assert forwarded == calls_before # `next` (_ok) was never called while OPEN pre-timeout

0 commit comments

Comments
 (0)