From 4c577bca2764e8f69ba5dbe042c1900239201fab Mon Sep 17 00:00:00 2001 From: Ghraven <115199279+Ghraven@users.noreply.github.com> Date: Tue, 28 Apr 2026 12:20:32 +0800 Subject: [PATCH] add tests/test_rate_limiter.py --- tests/test_rate_limiter.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/test_rate_limiter.py diff --git a/tests/test_rate_limiter.py b/tests/test_rate_limiter.py new file mode 100644 index 0000000..9923d78 --- /dev/null +++ b/tests/test_rate_limiter.py @@ -0,0 +1,28 @@ +"""Tests for utils.rate_limiter.""" +import time +import pytest +from utils.rate_limiter import RateLimiter + + +def test_allows_burst(): + limiter = RateLimiter(calls_per_second=100, burst=5) + start = time.monotonic() + for _ in range(5): + limiter.acquire() + elapsed = time.monotonic() - start + assert elapsed < 0.1 # burst should be near-instant + + +def test_rate_limits(): + limiter = RateLimiter(calls_per_second=20) + start = time.monotonic() + for _ in range(3): + limiter.acquire() + elapsed = time.monotonic() - start + # 3 calls at 20/s should take ~0.1s + assert elapsed >= 0.05 + + +def test_callable_interface(): + limiter = RateLimiter(calls_per_second=100) + limiter() # should not raise