diff --git a/wizard-work/beam/test_token_bucket.py b/wizard-work/beam/test_token_bucket.py index 1612d8e..3e54748 100644 --- a/wizard-work/beam/test_token_bucket.py +++ b/wizard-work/beam/test_token_bucket.py @@ -21,12 +21,20 @@ def test_capacity_cap(): def test_acquire_blocks_and_returns(): b = TokenBucket(rate=20.0, capacity=1.0) - b.try_acquire(1.0) # drain + # 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 # had to wait ~0.05s for 1 token at 20/s + assert dt >= 0.04 # 1 token at 20/s = 0.05s withheld, measured from the origin def test_n_exceeds_capacity_raises():