Files
DECNET/tests/realism/test_circuit_breaker.py
anti f2b3393669 chore: relicense to AGPL-3.0-or-later and add SPDX headers
Replaces LICENSE (GPLv3 -> AGPLv3) and prepends
`SPDX-License-Identifier: AGPL-3.0-or-later` to every source file
across decnet/, decnet_web/, tests/, scripts/, and tools/.

Rationale: closes the GPLv3 ASP loophole so any party operating a
modified DECNET as a network service must offer their modified
source. Personal copyright (Samuel Paschuan) + inbound=outbound
contributions make a future unilateral relicense infeasible.

- LICENSE: full AGPL-3.0 text (gnu.org/licenses/agpl-3.0.txt)
- COPYRIGHT: project copyright notice
- tools/add_spdx_headers.py: idempotent header injector
  (shebang- and PEP 263-aware)

Touches 1565 source files (.py, .ts, .tsx, .js, .jsx, .css, .sh).
No behavior change; comments only.
2026-05-22 21:04:16 -04:00

83 lines
2.4 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""LLMCircuitBreaker — process-local sliding-window breaker."""
from __future__ import annotations
from decnet.realism.llm.circuit import LLMCircuitBreaker
def test_starts_closed_and_allows_calls() -> None:
breaker = LLMCircuitBreaker()
assert breaker.state == "closed"
assert breaker.allow_call() is True
def test_trips_open_after_threshold_failures() -> None:
clock_value = [0.0]
breaker = LLMCircuitBreaker(
failure_threshold=3, cooldown_seconds=60.0,
clock=lambda: clock_value[0],
)
breaker.record_failure()
assert breaker.state == "closed"
breaker.record_failure()
assert breaker.state == "closed"
breaker.record_failure()
assert breaker.state == "open"
assert breaker.allow_call() is False
def test_success_resets_consecutive_failure_count() -> None:
breaker = LLMCircuitBreaker(failure_threshold=3)
breaker.record_failure()
breaker.record_failure()
breaker.record_success()
breaker.record_failure()
breaker.record_failure()
assert breaker.state == "closed" # only 2 since the success
def test_half_open_after_cooldown() -> None:
clock_value = [0.0]
breaker = LLMCircuitBreaker(
failure_threshold=2, cooldown_seconds=10.0,
clock=lambda: clock_value[0],
)
breaker.record_failure()
breaker.record_failure()
assert breaker.state == "open"
assert breaker.allow_call() is False
clock_value[0] = 11.0
assert breaker.allow_call() is True
assert breaker.state == "half_open"
def test_half_open_failure_re_opens() -> None:
clock_value = [0.0]
breaker = LLMCircuitBreaker(
failure_threshold=2, cooldown_seconds=5.0,
clock=lambda: clock_value[0],
)
breaker.record_failure()
breaker.record_failure()
clock_value[0] = 6.0
breaker.allow_call()
assert breaker.state == "half_open"
breaker.record_failure()
assert breaker.state == "open"
def test_half_open_success_closes() -> None:
clock_value = [0.0]
breaker = LLMCircuitBreaker(
failure_threshold=2, cooldown_seconds=5.0,
clock=lambda: clock_value[0],
)
breaker.record_failure()
breaker.record_failure()
clock_value[0] = 6.0
breaker.allow_call()
breaker.record_success()
assert breaker.state == "closed"
assert breaker.allow_call() is True