LichessStatTgWeb/tests/service/test_rate_limiter.py
vrubelroman a7571f13c1
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 16s
v1.5.1: not-found players paused, tests in repo + CI, log-based daily summary, docs pruned
- players absent from two consecutive bulk answers (deleted/renamed on Lichess) are paused
  like closed accounts with their own notice; ❓ mark; stats service answers 404 instead
  of "0 games" for unknown users
- /getgamers table stays unmarked, a legend line under it lists closed / not-found players
- daily SUMMARY is computed from events.log (survives restarts); lichess_429 comes from the
  stats service /health, which now exposes lichess_429_24h / lichess_429_total
- admin /status reads its numbers through Database.get_admin_stats
- tests/: 36 bot + 15 service unittest cases, run_tests.sh runs them inside the real images,
  CI runs them after the build and before pushing/deploying
- docs: USER_GUIDE/about/API_DOCUMENTATION rewritten for the current code, ARCHITECTURE.md and
  two sub-READMEs removed, 7 obsolete helper scripts removed, CURRENT_STATE updated
- .claude/agents/designer.md: Telegram-aware designer role

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-21 02:48:00 +00:00

98 lines
3.5 KiB
Python

"""SharedTokenGate and ActivityRateLimiter timing, with small gaps so the whole file runs in ~1 s."""
import asyncio
import time
import unittest
from rate_limiter import SharedTokenGate, ActivityRateLimiter
def run(coro):
return asyncio.run(coro)
class SharedTokenGateTests(unittest.TestCase):
def test_min_gap_between_release_and_next_acquire(self):
async def scenario():
gate = SharedTokenGate(min_gap=0.2)
t0 = time.monotonic()
await gate.acquire("background")
first = time.monotonic() - t0
gate.release()
t1 = time.monotonic()
await gate.acquire("background")
second = time.monotonic() - t1
gate.release()
return first, second
first, second = run(scenario())
self.assertLess(first, 0.05) # first acquire is immediate
self.assertGreaterEqual(second, 0.19) # the gap is enforced after a release
def test_pause_holds_every_priority(self):
async def scenario():
gate = SharedTokenGate(min_gap=0.0)
gate.pause(0.3)
gate.pause(0.1) # a shorter pause never cuts the active one short
t0 = time.monotonic()
await gate.acquire("interactive")
waited = time.monotonic() - t0
gate.release()
return waited
self.assertGreaterEqual(run(scenario()), 0.29)
def test_interactive_waiter_goes_before_background(self):
async def scenario():
gate = SharedTokenGate(min_gap=0.0)
order = []
async def waiter(priority):
await gate.acquire(priority)
order.append(priority)
gate.release()
await gate.acquire("background") # hold the gate
bg = asyncio.create_task(waiter("background"))
await asyncio.sleep(0.02) # background queues first
ia = asyncio.create_task(waiter("interactive"))
await asyncio.sleep(0.02)
gate.release() # hand over: interactive must win
await asyncio.gather(bg, ia)
return order
self.assertEqual(["interactive", "background"], run(scenario()))
def test_release_without_waiters_frees_the_gate(self):
async def scenario():
gate = SharedTokenGate(min_gap=0.0)
await gate.acquire()
gate.release()
await gate.acquire() # would hang forever if _busy stayed True
gate.release()
return True
self.assertTrue(run(scenario()))
class ActivityRateLimiterTests(unittest.TestCase):
def test_spacing_measured_from_mark(self):
async def scenario():
limiter = ActivityRateLimiter(min_delay=0.2)
async with limiter:
limiter.mark()
t0 = time.monotonic()
async with limiter:
limiter.mark()
return time.monotonic() - t0
self.assertGreaterEqual(run(scenario()), 0.19)
def test_no_wait_before_first_call_and_without_mark(self):
async def scenario():
limiter = ActivityRateLimiter(min_delay=0.2)
t0 = time.monotonic()
async with limiter:
pass # no mark(): nothing to space from
async with limiter:
pass
return time.monotonic() - t0
self.assertLess(run(scenario()), 0.05)
if __name__ == '__main__':
unittest.main()