|
| 1 | +"""Unit tests for RetryBudget token-bucket math. |
| 2 | +
|
| 3 | +Tests inject a deterministic `_now` callable rather than monkeypatching `time.monotonic`, |
| 4 | +so they cannot be perturbed by other tests sharing the same module. |
| 5 | +""" |
| 6 | + |
| 7 | +import time |
| 8 | + |
| 9 | +from httpware.middleware.resilience.budget import RetryBudget |
| 10 | + |
| 11 | + |
| 12 | +class _Clock: |
| 13 | + """Mutable clock for deterministic tests. Pass `clock.now` as `_now`.""" |
| 14 | + |
| 15 | + def __init__(self, start: float = 0.0) -> None: |
| 16 | + self._t = start |
| 17 | + |
| 18 | + def now(self) -> float: |
| 19 | + return self._t |
| 20 | + |
| 21 | + def advance(self, seconds: float) -> None: |
| 22 | + self._t += seconds |
| 23 | + |
| 24 | + |
| 25 | +def test_defaults_match_spec() -> None: |
| 26 | + budget = RetryBudget() |
| 27 | + assert budget._ttl == 10.0 # noqa: SLF001, PLR2004 |
| 28 | + assert budget._min_retries_per_sec == 10.0 # noqa: SLF001, PLR2004 |
| 29 | + assert budget._percent_can_retry == 0.2 # noqa: SLF001, PLR2004 |
| 30 | + |
| 31 | + |
| 32 | +def test_floor_permits_min_retries_per_sec_times_ttl_with_zero_deposits() -> None: |
| 33 | + # floor = min_retries_per_sec * ttl = 10 * 10 = 100 permitted withdrawals |
| 34 | + clock = _Clock() |
| 35 | + budget = RetryBudget(ttl=10.0, min_retries_per_sec=10.0, percent_can_retry=0.0, _now=clock.now) |
| 36 | + permitted = sum(1 for _ in range(101) if budget.try_withdraw()) |
| 37 | + assert permitted == 100 # noqa: PLR2004 |
| 38 | + |
| 39 | + |
| 40 | +def test_percent_can_retry_ceiling_with_deposits() -> None: |
| 41 | + # 1000 deposits * 0.2 = 200 retries permitted (plus floor 100 = 300 total) |
| 42 | + clock = _Clock() |
| 43 | + budget = RetryBudget(ttl=10.0, min_retries_per_sec=10.0, percent_can_retry=0.2, _now=clock.now) |
| 44 | + for _ in range(1000): |
| 45 | + budget.deposit() |
| 46 | + permitted = sum(1 for _ in range(500) if budget.try_withdraw()) |
| 47 | + assert permitted == 300 # noqa: PLR2004 |
| 48 | + |
| 49 | + |
| 50 | +def test_ttl_expiry_purges_old_deposits() -> None: |
| 51 | + clock = _Clock() |
| 52 | + budget = RetryBudget(ttl=1.0, min_retries_per_sec=0.0, percent_can_retry=0.5, _now=clock.now) |
| 53 | + for _ in range(10): |
| 54 | + budget.deposit() |
| 55 | + # 10 deposits * 0.5 = 5 retries available immediately |
| 56 | + assert budget.try_withdraw() is True |
| 57 | + # Advance past TTL; deposits expire |
| 58 | + clock.advance(2.0) |
| 59 | + # With min_retries_per_sec=0 and no live deposits, no retries permitted |
| 60 | + assert budget.try_withdraw() is False |
| 61 | + |
| 62 | + |
| 63 | +def test_try_withdraw_returns_false_when_exhausted() -> None: |
| 64 | + clock = _Clock() |
| 65 | + budget = RetryBudget(ttl=10.0, min_retries_per_sec=1.0, percent_can_retry=0.0, _now=clock.now) |
| 66 | + # floor = 1 * 10 = 10 retries |
| 67 | + for _ in range(10): |
| 68 | + assert budget.try_withdraw() is True |
| 69 | + assert budget.try_withdraw() is False |
| 70 | + |
| 71 | + |
| 72 | +def test_deposit_after_exhaustion_does_not_immediately_unblock() -> None: |
| 73 | + """A single deposit at 20% percent_can_retry contributes 0.2 → floor (int truncation) → 0 new retries.""" |
| 74 | + clock = _Clock() |
| 75 | + budget = RetryBudget(ttl=10.0, min_retries_per_sec=1.0, percent_can_retry=0.2, _now=clock.now) |
| 76 | + # exhaust the floor (10) |
| 77 | + for _ in range(10): |
| 78 | + budget.try_withdraw() |
| 79 | + assert budget.try_withdraw() is False |
| 80 | + # one deposit: 1 * 0.2 = 0.2 → int() → 0 |
| 81 | + budget.deposit() |
| 82 | + assert budget.try_withdraw() is False |
| 83 | + # 5 more deposits: 6 * 0.2 = 1.2 → int() → 1 new retry permitted |
| 84 | + for _ in range(5): |
| 85 | + budget.deposit() |
| 86 | + assert budget.try_withdraw() is True |
| 87 | + assert budget.try_withdraw() is False |
| 88 | + |
| 89 | + |
| 90 | +def test_withdrawn_also_expires_after_ttl() -> None: |
| 91 | + """After TTL passes, prior withdrawals no longer count against the budget.""" |
| 92 | + clock = _Clock() |
| 93 | + budget = RetryBudget(ttl=1.0, min_retries_per_sec=10.0, percent_can_retry=0.0, _now=clock.now) |
| 94 | + for _ in range(10): |
| 95 | + budget.try_withdraw() |
| 96 | + assert budget.try_withdraw() is False |
| 97 | + clock.advance(2.0) |
| 98 | + assert budget.try_withdraw() is True |
| 99 | + |
| 100 | + |
| 101 | +def test_default_now_is_time_monotonic() -> None: |
| 102 | + """When _now is not passed, the budget uses time.monotonic by default.""" |
| 103 | + budget = RetryBudget() |
| 104 | + assert budget._now is time.monotonic # noqa: SLF001 |
0 commit comments