Files
troupe-standup/wizard-work/beam/test_token_bucket.py
T
ledger 960469db33 beam: token_bucket blocking cell measures from the refill origin
WHY: test_acquire_blocks_and_returns took t0 after the drain, so the cell timed
only the part of the 0.05 s refill that happened after the scheduler returned to
it. Observed red in a loaded full-suite run at 7c36185:
'assert 0.03909331373870373 >= 0.04' -- 10.9 ms of untimed prefix. Deterministic
repro: pre-fix cell + 11 ms injected gap -> 0.03897/0.03912, rc 1.

FIX: t0 now precedes try_acquire(1.0), which is where the bucket sets _last, so
dt is >= 0.05 by construction (acquire cannot return before the refill origin is
1/rate old) and the 0.04 floor has ~10 ms of margin instead of 1 ms.

MEASURED: repo cell 9 passed; pre control rc 1 dt 0.03897-0.03912; post control
rc 0; same-gap in-process dt pre 0.039119 / post 0.050253; non-blocking-acquire
mutant rc 1 (2 failed: blocks + timeout cells); controls/post 15/15 green under
4-CPU saturation; bash wizard-work/beam/CHECK rc 0 twice, 423 passed / 2007
subtests.

CHECK: python3 /root/.hermes/wizard-outputs/volt/2026-10-28-q122/verify.py
2026-10-28 14:14:46 +00:00

117 lines
3.4 KiB
Python

import threading
import time
from token_bucket import TokenBucket
def test_basic_acquire_and_refill():
b = TokenBucket(rate=10.0, capacity=5.0)
assert b.try_acquire(5.0) is True
assert b.try_acquire(1.0) is False
# refill: wait ~0.15s -> 1.5 tokens
time.sleep(0.15)
assert b.try_acquire(1.0) is True
def test_capacity_cap():
b = TokenBucket(rate=100.0, capacity=3.0)
time.sleep(0.1)
assert b.tokens() <= 3.0 + 1e-9
def test_acquire_blocks_and_returns():
b = TokenBucket(rate=20.0, capacity=1.0)
# t0 must precede the drain: try_acquire() debits the last token and sets
# the refill origin (self._last) inside the call, so a t0 taken after it
# silently subtracts whatever the scheduler spent in between. Observed at
# 7c36185 as `assert 0.03909331373870373 >= 0.04` in a loaded full-suite
# run: ~10.9 ms of scheduler gap, i.e. 0.05 s of withheld tokens minus a
# prefix the cell never measured. dt from before the drain is >= 0.05 by
# construction (acquire cannot return until the refill origin is 0.05 s
# old), so the 0.04 floor has margin instead of sitting 1 ms from the edge.
t0 = time.monotonic()
b.try_acquire(1.0) # drain; sets the refill origin
ok = b.acquire(1.0, timeout=0.2)
dt = time.monotonic() - t0
assert ok is True
assert dt >= 0.04 # 1 token at 20/s = 0.05s withheld, measured from the origin
def test_n_exceeds_capacity_raises():
b = TokenBucket(rate=1.0, capacity=1.0)
try:
b.acquire(2.0, timeout=0.01)
raised = False
except ValueError:
raised = True
assert raised
try:
b.try_acquire(2.0)
raised2 = False
except ValueError:
raised2 = True
assert raised2
def test_invalid_params():
for kwargs in ({"rate": 0, "capacity": 1}, {"rate": 1, "capacity": 0}, {"rate": -1, "capacity": 1}):
try:
TokenBucket(**kwargs)
assert False
except ValueError:
pass
try:
TokenBucket(rate=1, capacity=1).try_acquire(0)
assert False
except ValueError:
pass
def test_concurrent_no_overconsume():
b = TokenBucket(rate=1e-9, capacity=10.0) # effectively no refill
successes = {"n": 0}
lock = threading.Lock()
def worker():
if b.try_acquire(1.0):
with lock:
successes["n"] += 1
threads = [threading.Thread(target=worker) for _ in range(50)]
for t in threads:
t.start()
for t in threads:
t.join()
assert successes["n"] == 10
def test_acquire_timeout_false():
b = TokenBucket(rate=1.0, capacity=1.0)
b.try_acquire(1.0) # drain
# request 1 token but timeout too short for refill (need 1s)
t0 = time.monotonic()
ok = b.acquire(1.0, timeout=0.05)
dt = time.monotonic() - t0
assert ok is False
assert dt < 0.2
def test_reset():
b = TokenBucket(rate=1.0, capacity=2.0)
b.try_acquire(2.0)
assert b.try_acquire(1.0) is False
b.reset()
assert b.try_acquire(2.0) is True
def test_partial_never_consumes():
b = TokenBucket(rate=1.0, capacity=2.0)
b.try_acquire(2.0) # drain
# 1 token needed, only 0 available -> no consume
assert b.try_acquire(1.0) is False
# refill 0.5s -> 0.5 tokens, still < 1
time.sleep(0.5)
assert b.try_acquire(1.0) is False
# tokens should be ~0.5, not negative
assert b.tokens() >= 0.4