返回 last30days-skill
test_reddit_keyless_wait_allowance.py
根目录 / tests / test_reddit_keyless_wait_allowance.py
1 """Per-future result timeouts must cover the keyless bucket's queue.
2
3 At 1 req/s, a batch of thirteen keyless URLs on four workers queues about
4 thirteen seconds of token waits before the last fetch even starts, and four
5 subquery streams share the same bucket. A fixed 20-second future timeout then
6 expires while the fetch is still waiting for a token, and the result is
7 dropped with an empty future-failure message (seen on the 2026-08-31 smoke
8 run). The site search lane's timeout is covered in test_reddit_search.py.
9 """
10
11 import threading
12 from concurrent.futures import ThreadPoolExecutor
13 from types import SimpleNamespace
14 from unittest import mock
15
16 from lib import http, reddit_listing
17
18
19 def test_limiter_reports_waiting_threads(monkeypatch):
20 sleeping = threading.Event()
21 release = threading.Event()
22 now = [0.0]
23
24 def blocked_sleep(delay):
25 sleeping.set()
26 assert release.wait(timeout=5), "queued worker was not released"
27 now[0] += delay
28
29 monkeypatch.setattr(http, "time", SimpleNamespace(monotonic=lambda: now[0], sleep=blocked_sleep))
30 monkeypatch.delenv(http.REDDIT_KEYLESS_RATE_ENV, raising=False)
31 limiter = http.RateLimiter(rate_per_sec=1.0, burst=1)
32 monkeypatch.setattr(http, "REDDIT_KEYLESS_LIMITER", limiter)
33 assert limiter.waiting == 0
34 limiter.acquire() # drains the single token
35 with ThreadPoolExecutor(max_workers=1) as pool:
36 worker = pool.submit(limiter.acquire)
37 try:
38 assert sleeping.wait(timeout=5), "worker did not reach token wait"
39 assert not worker.done()
40 assert limiter.waiting == 1
41 assert http.reddit_keyless_wait_allowance(2) == 3.0 + http.REDDIT_KEYLESS_CONTENTION_SECONDS
42 finally:
43 release.set()
44 worker.result(timeout=5)
45 assert limiter.waiting == 0
46 assert http.reddit_keyless_wait_allowance(2) == 2.0 + http.REDDIT_KEYLESS_CONTENTION_SECONDS
47
48
49 def test_wait_allowance_scales_with_batch_and_queue(monkeypatch):
50 limiter = http.RateLimiter(rate_per_sec=1.0, burst=2)
51 monkeypatch.delenv(http.REDDIT_KEYLESS_RATE_ENV, raising=False)
52 with mock.patch.object(http, "REDDIT_KEYLESS_LIMITER", limiter):
53 pad = http.REDDIT_KEYLESS_CONTENTION_SECONDS
54 assert http.reddit_keyless_wait_allowance(13) == 13.0 + pad
55 limiter._waiting = 5
56 assert http.reddit_keyless_wait_allowance(13) == 18.0 + pad
57 # The allowance syncs the configured rate before computing.
58 monkeypatch.setenv(http.REDDIT_KEYLESS_RATE_ENV, "2")
59 assert http.reddit_keyless_wait_allowance(13) == 9.0 + pad
60
61
62 def test_listing_result_timeout_includes_the_allowance(monkeypatch):
63 limiter = http.RateLimiter(rate_per_sec=1.0, burst=2)
64 monkeypatch.delenv(http.REDDIT_KEYLESS_RATE_ENV, raising=False)
65 with mock.patch.object(http, "REDDIT_KEYLESS_LIMITER", limiter):
66 pad = http.REDDIT_KEYLESS_CONTENTION_SECONDS
67 assert reddit_listing._result_timeout(20) == reddit_listing.LISTING_TIMEOUT + 5 + 20.0 + pad
68
69
70 def test_allowance_reflects_a_process_env_rate_override(monkeypatch):
71 limiter = http.RateLimiter(rate_per_sec=1.0, burst=2)
72 with mock.patch.object(http, "REDDIT_KEYLESS_LIMITER", limiter):
73 monkeypatch.setenv(http.REDDIT_KEYLESS_RATE_ENV, "0.5")
74 pad = http.REDDIT_KEYLESS_CONTENTION_SECONDS
75 assert http.reddit_keyless_wait_allowance(10) == 20.0 + pad
76
76 lines PYTHON