rate-limit: zentraler Rate-Limiter in RCRestClient._request() + 429-Retry + 15s Cooldown
AUFGABE 1 — Rate-Limiter in _request(): - RCRestClient.__init__ nimmt optionalen rate_limiter - _request() ruft rate_limiter.acquire() VOR jedem API-Call auf - Schützt ALLE Endpoints: postMessage, channels.history, users.info, chat.delete - rc_client Fixture (session-scoped) injiziert rate_limiter in den Client AUFGABE 2 — 15s Cooldown zwischen Tests: - rate_limit_cooldown autouse Fixture in conftest.py - yield + time.sleep(15) nach jedem Test - RC Rate-Limit-Window (~10s) wird zuverlässig resettet AUFGABE 3 — test_03 Skip-Ursache: - test_02 und test_03 pollen nicht mehr direkt hintereinander ohne Pause - 15s Cooldown + zentraler Rate-Limiter verhindern kaskadierende 429s AUFGABE 4 — 429-Retry mit Backoff: - _request() retryed HTTP 429 mit 3s → 6s → 12s Backoff (max 3 Retries) - post_message failt nicht mehr sofort bei 429 CLEANUP: - Redundante rate_limiter.acquire() aus polling.py (wait_for_bot_message, get_latest_timestamp), conftest.py (post_test_message) und test_04_multi_room.py entfernt - rate_limiter Fixture auf session-scope (war eh globaler Singleton) - Docstrings aktualisiert
This commit is contained in:
@@ -217,15 +217,18 @@ def rc_config() -> dict:
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def rc_client(rc_config: dict) -> RCRestClient:
|
||||
def rc_client(rc_config: dict, rate_limiter) -> RCRestClient:
|
||||
"""Create an authenticated RCRestClient for the test session.
|
||||
|
||||
Performs self-check: verifies that the token matches the user identity.
|
||||
The rate_limiter is injected into the client so every API call
|
||||
(post, history, delete, users.info) is rate-limited automatically.
|
||||
"""
|
||||
client = RCRestClient(
|
||||
base_url=rc_config["url"],
|
||||
auth_token=rc_config["token"],
|
||||
user_id=rc_config["user_id"],
|
||||
rate_limiter=rate_limiter,
|
||||
)
|
||||
|
||||
# P0: Self-check — verify token matches user identity
|
||||
@@ -249,6 +252,18 @@ def _suite_cleanup(request, rc_config: dict):
|
||||
_release_suite_lock()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def rate_limit_cooldown():
|
||||
"""Wait 15s between tests for RC rate-limit window reset.
|
||||
|
||||
RC's rate-limit window is ~10s. A 15s pause between tests ensures
|
||||
the token bucket has time to refill, preventing cascading 429s
|
||||
when tests run back-to-back.
|
||||
"""
|
||||
yield
|
||||
time.sleep(15)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_room_id(rc_config: dict) -> str:
|
||||
"""The test room ID for the current test."""
|
||||
@@ -310,9 +325,9 @@ def bot_status_check(rc_client: RCRestClient, bot_user_id: str, bot_username: st
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@pytest.fixture(scope="session")
|
||||
def rate_limiter() -> RateLimiter:
|
||||
"""Rate limiter to stay under 30 calls/minute."""
|
||||
"""Rate limiter to stay under 30 calls/minute (session-scoped)."""
|
||||
return _get_rate_limiter()
|
||||
|
||||
|
||||
@@ -370,20 +385,21 @@ def post_test_message(
|
||||
posted_ids: list,
|
||||
limiter: RateLimiter | None = None,
|
||||
) -> str:
|
||||
"""Post a test message with rate limiting and cleanup tracking.
|
||||
"""Post a test message with cleanup tracking.
|
||||
|
||||
Rate limiting is handled by the RCRestClient internally (via _request).
|
||||
The limiter parameter is kept for backward compatibility but is unused.
|
||||
|
||||
Args:
|
||||
client: Authenticated RCRestClient.
|
||||
room_id: Target room ID.
|
||||
text: Message text.
|
||||
posted_ids: List to append the message ID to (for cleanup).
|
||||
limiter: Optional rate limiter.
|
||||
limiter: Deprecated — rate limiting is now in RCRestClient._request().
|
||||
|
||||
Returns:
|
||||
The message _id.
|
||||
"""
|
||||
if limiter:
|
||||
limiter.acquire()
|
||||
msg_id = client.post_message(room_id, text)
|
||||
posted_ids.append(msg_id)
|
||||
return msg_id
|
||||
|
||||
Reference in New Issue
Block a user