Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert circuit_breaker.recovery_ratio == 0
assert circuit_breaker.state == States.PASSING
ctx.failed = True
for _ in range(10):
await asyncio.sleep(0.05)
with pytest.raises(RuntimeError):
circuit_breaker.call(ctx)
for _ in range(10):
try:
circuit_breaker.call(ctx)
except (RuntimeError, CircuitBroken):
pass
assert circuit_breaker.state == States.BROKEN
def _on_recover(self, counter):
current_time = time.monotonic()
condition = (random() + 1) < (
2 ** ((current_time - self._recovery_at) / self._recovery_time)
)
if condition:
yield from self._on_passing(counter)
else:
raise CircuitBroken()
def _on_broken(self):
raise CircuitBroken()