Files
StarPunk/tests/test_shared_rate_limit.py
T
PhilandClaude Opus 5.5 75946be7b2 maint: Dependency upgrades and High backlog items (v2.11.0)
- All dependencies to current releases (97 known vulnerabilities -> 0),
  Python 3.14, uv pinned, requirements-test.txt, lxml/dateutil pinned
- CSRF tokens on admin forms, logout and IndieAuth consent
- IndieAuth client and crosspost link-card fetches via safe_get
- FLASK_DEBUG/FLASK_ENV default off/production; image sets them
- Foreign keys enforced on pooled connections; migration 018 removes orphans
- Rate limits shared across processes (rate_limits, migration 019)
- datetime.utcnow() and sqlite3 default adapters replaced
- compose.yaml fixed; duplicate dev_auth.py and test.ini removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 13:09:52 -07:00

57 lines
2.5 KiB
Python

"""
Rate limits are shared by all server processes (v2.11.0)
Each gunicorn process used to keep its own counts, so with 4 workers a client
got four times the limit. Two SharedRateLimiter instances on one database
stand in for two processes.
"""
import time
from unittest.mock import patch
from starpunk.rate_limiter import SharedRateLimiter
def test_two_processes_share_one_limit(app):
first = SharedRateLimiter(limit=3, window_seconds=60, prefix="t")
second = SharedRateLimiter(limit=3, window_seconds=60, prefix="t")
with app.app_context(), patch("starpunk.rate_limiter.time.time", return_value=6000.0):
results = [limiter.is_allowed("1.2.3.4") for limiter in (first, second, first, second)]
assert results == [True, True, True, False]
def test_keys_and_prefixes_are_separate(app):
a = SharedRateLimiter(limit=1, window_seconds=60, prefix="a")
b = SharedRateLimiter(limit=1, window_seconds=60, prefix="b")
with app.app_context(), patch("starpunk.rate_limiter.time.time", return_value=6000.0):
assert a.is_allowed("x") and a.is_allowed("y") and b.is_allowed("x")
assert not a.is_allowed("x")
def test_window_slides(app):
limiter = SharedRateLimiter(limit=2, window_seconds=60, prefix="s")
with app.app_context():
# Window 100 (6000-6059 s): two requests use the limit
with patch("starpunk.rate_limiter.time.time", return_value=6000.0):
assert limiter.is_allowed("k") and limiter.is_allowed("k")
assert not limiter.is_allowed("k")
# 6030 s is still window 100: still limited
with patch("starpunk.rate_limiter.time.time", return_value=6030.0):
assert not limiter.is_allowed("k")
# 6090 s is halfway through window 101: window 100 counts half (2 x 0.5 = 1)
with patch("starpunk.rate_limiter.time.time", return_value=6090.0):
assert limiter.is_allowed("k") # 1 + 0 < 2
assert not limiter.is_allowed("k") # 1 + 1 = 2
def test_cleanup_removes_old_windows(app):
import sqlite3
limiter = SharedRateLimiter(limit=5, window_seconds=60, prefix="c")
with app.app_context():
with patch("starpunk.rate_limiter.time.time", return_value=6000.0):
limiter.is_allowed("k")
with patch("starpunk.rate_limiter.time.time", return_value=9000.0):
limiter.cleanup()
rows = sqlite3.connect(app.config["DATABASE_PATH"]).execute("SELECT COUNT(*) FROM rate_limits").fetchone()[0]
assert rows == 0