From a3ca618857989a56e0f4aefda17abc7625d061ce Mon Sep 17 00:00:00 2001 From: sungdark Date: Thu, 26 Feb 2026 21:43:57 +0000 Subject: [PATCH] test: cover relay ping write limiting and limiter LRU eviction --- tests/test_atlas_rate_limit.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test_atlas_rate_limit.py b/tests/test_atlas_rate_limit.py index ce50390..536364a 100644 --- a/tests/test_atlas_rate_limit.py +++ b/tests/test_atlas_rate_limit.py @@ -49,6 +49,20 @@ def test_bounded_rate_limiter_ttl_cleanup_and_limit(): assert "k1" not in limiter._entries +def test_bounded_rate_limiter_enforces_max_entries_lru_eviction(): + limiter = beacon_chat.BoundedRateLimiter(max_entries=2, ttl_seconds=999, cleanup_interval_seconds=1) + + assert limiter.allow("k1", 5, now=100) + assert limiter.allow("k2", 5, now=100) + + # Trigger cleanup cycle and insert a third key to force LRU eviction. + assert limiter.allow("k3", 5, now=101) + + assert "k1" not in limiter._entries + assert "k2" in limiter._entries + assert "k3" in limiter._entries + + def test_api_bounties_read_rate_limit(): beacon_chat.app.config["TESTING"] = True beacon_chat.app.config["RATE_LIMIT_READ_PER_MIN"] = 1 @@ -71,3 +85,21 @@ def test_write_rate_limit_on_chat_endpoint(): assert r1.status_code == 400 assert r2.status_code == 429 + + +def test_write_rate_limit_on_relay_ping_endpoint(): + beacon_chat.app.config["TESTING"] = True + beacon_chat.app.config["RATE_LIMIT_WRITE_PER_MIN"] = 1 + + client = beacon_chat.app.test_client() + payload = { + "agent_id": "bcn_unsigned_rate_limit", + "name": "Unsigned", + "pubkey_hex": "00" * 32, + } + + r1 = client.post("/relay/ping", json=payload, environ_overrides={"REMOTE_ADDR": "10.3.3.3"}) + r2 = client.post("/relay/ping", json=payload, environ_overrides={"REMOTE_ADDR": "10.3.3.3"}) + + assert r1.status_code == 400 + assert r2.status_code == 429