test(rate-limit): tolerate the timer on Windows, and restore two bounds

The last Windows failure was float noise on an upper bound: a 0.15s cooldown
window reports 0.15000000000000568 remaining, so the assertion missed by 6e-15.
The upper bound now carries an epsilon.

The two `elapsed >= 0.14` cooldown assertions had been weakened to `>= 0.09` by
a substring replace while relaxing the pacing bound -- that would have let a
missing wait pass. They are back at 0.12 (0.15s minus Windows timer rounding),
which still fails if the cooldown is not awaited.
This commit is contained in:
Will Miao
2026-10-12 11:51:48 +08:00
parent d25f170ffe
commit 946809f47c
@@ -84,12 +84,15 @@ async def test_register_rate_limit_arms_cooldown_and_waits(monkeypatch):
) )
coordinator.register_rate_limit("example.com", retry_after=0.15) coordinator.register_rate_limit("example.com", retry_after=0.15)
assert coordinator.in_cooldown("example.com") assert coordinator.in_cooldown("example.com")
assert 0.1 < coordinator.remaining_seconds("example.com") <= 0.15 remaining = coordinator.remaining_seconds("example.com")
# The upper bound only says "barely any time has passed": allow the timer's
# float noise (Windows reports 0.15000000000000568 for a 0.15s window).
assert 0.1 < remaining <= 0.15 + 1e-6
start = time.monotonic() start = time.monotonic()
await coordinator.wait_for_slot("example.com") await coordinator.wait_for_slot("example.com")
elapsed = time.monotonic() - start elapsed = time.monotonic() - start
assert elapsed >= 0.09 # Windows timer granularity rounds 0.1s down4 assert elapsed >= 0.12 # 0.15s cooldown, minus Windows timer rounding
assert not coordinator.in_cooldown("example.com") assert not coordinator.in_cooldown("example.com")
@@ -255,7 +258,7 @@ async def test_make_request_paces_consecutive_calls(monkeypatch):
await downloader.make_request("GET", "https://api.example.com/a") await downloader.make_request("GET", "https://api.example.com/a")
await downloader.make_request("GET", "https://api.example.com/b") await downloader.make_request("GET", "https://api.example.com/b")
elapsed = time.monotonic() - start elapsed = time.monotonic() - start
assert elapsed >= 0.09 # Windows timer granularity rounds 0.1s down4 assert elapsed >= 0.12 # 0.15s cooldown, minus Windows timer rounding
async def test_make_request_gate_disabled_returns_429_immediately(monkeypatch): async def test_make_request_gate_disabled_returns_429_immediately(monkeypatch):