Skip to content

Commit f026241

Browse files
committed
test: cover 503 Retry-After, quota 429 Retry-After, and skip-batch 429 retry
Verify Retry-After is ignored on HTTP 503, monthly-quota 429 with Retry-After is still not retried, and events.check_many(..., skip=True) retries a 429 with Retry-After and attaches rate_limit from the subsequent 200 response.
1 parent 40e514d commit f026241

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎tests/test_client.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,24 @@ async def test_async_rate_limit_honors_retry_after(
250250
assert sleeps == [2.5]
251251

252252

253-
def test_monthly_quota_429_is_not_retried(client: WhiteCircle, api: MockAPI) -> None:
254-
api.json(429, {"error": "Monthly request quota exceeded for promotional plan."})
253+
def test_monthly_quota_429_is_not_retried(client: WhiteCircle, api: MockAPI, sleeps: list[float]) -> None:
254+
api.json(
255+
429,
256+
{"error": "Monthly request quota exceeded for promotional plan."},
257+
headers={"Retry-After": "1"},
258+
)
255259
with pytest.raises(RateLimitError, match="quota"):
256260
client.request("POST", "/api/event", body={})
257261
assert len(api.requests) == 1
262+
assert sleeps == []
263+
264+
265+
def test_retry_after_is_ignored_on_503(client: WhiteCircle, api: MockAPI, sleeps: list[float]) -> None:
266+
api.json(503, {"error": "busy"}, headers={"Retry-After": "5"})
267+
api.json(200, {})
268+
client.request("POST", "/api/event", body={})
269+
assert len(sleeps) == 1
270+
assert 0.375 <= sleeps[0] <= 0.5
258271

259272

260273
@pytest.mark.parametrize("error", [httpx2.ReadTimeout("slow"), httpx2.ReadError("reset"), httpx2.WriteError("x")])

‎tests/test_events.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,22 @@ async def test_api_status_error_exposes_rate_limit_headers(call: Call, api: Mock
355355
with pytest.raises(InternalServerError) as caught_500:
356356
await call("events", "check", Event.user("hi"))
357357
assert caught_500.value.rate_limit is None
358+
359+
360+
async def test_check_many_skip_retries_429_with_retry_after_and_attaches_rate_limit(
361+
call: Call, api: MockAPI, sleeps: list[float]
362+
) -> None:
363+
api.json(
364+
429,
365+
{"error": "Rate limit exceeded"},
366+
headers={**RATE_LIMIT_HEADERS, "x-ratelimit-events-remaining": "0", "retry-after": "2"},
367+
)
368+
api.json(
369+
200,
370+
{"flagged": False, "run_id": "run_1", "messages": [result("message", "evt_1")]},
371+
headers={**RATE_LIMIT_HEADERS, "x-ratelimit-events-remaining": "4"},
372+
)
373+
batch = await call("events", "check_many", [Event.user("hi", event_id="evt_1")], skip=True)
374+
assert len(api.requests) == 2
375+
assert sleeps == [2.0]
376+
assert batch.rate_limit == EventsRateLimit(limit=5, remaining=4, reset=1.0, window=1.0)

0 commit comments

Comments
 (0)