mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
ScreenScraper returns the account's allowances on every response, but only maxthreads was read. Use the rest: - Pace requests against the reported maxrequestspermin (falling back to the documented threads x 50), so fast lookups can no longer overrun the per-minute budget without ever exceeding the thread cap. - Report the remaining daily and unrecognized-ROM quotas after each platform and at the end of a scan, and warn once when either drops below 10%. - Hint once per scan when SCAN_WORKERS is below the thread allowance (scans slower than the account allows) or above it (extra workers just queue). - Raise a distinct error when a request is refused twice for exceeding the rate, so the ROM is reported as skipped in the scan summary instead of being saved with no ScreenScraper data. - Route ScreenScraper media downloads (covers, screenshots, manuals, mixed media) through the same limiters, since they are api2 endpoints that count against the same allowances, and derive their timeout from the account's advertised download speed. Fixes #3978 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
163 lines
5.3 KiB
Python
163 lines
5.3 KiB
Python
import asyncio
|
|
|
|
import pytest
|
|
|
|
from utils.rate_limiter import ConcurrencyLimiter, RateLimiter
|
|
|
|
|
|
def _record_sleeps(monkeypatch) -> list[float]:
|
|
"""Replace asyncio.sleep with a no-op that records requested delays."""
|
|
sleeps: list[float] = []
|
|
|
|
async def fake_sleep(delay: float) -> None:
|
|
sleeps.append(delay)
|
|
|
|
monkeypatch.setattr(asyncio, "sleep", fake_sleep)
|
|
return sleeps
|
|
|
|
|
|
class TestRateLimiter:
|
|
async def test_first_acquire_does_not_sleep(self, monkeypatch):
|
|
sleeps = _record_sleeps(monkeypatch)
|
|
limiter = RateLimiter(requests_per_second=4)
|
|
|
|
await limiter.acquire()
|
|
|
|
assert sleeps == []
|
|
|
|
async def test_slots_advance_by_interval(self, monkeypatch):
|
|
sleeps = _record_sleeps(monkeypatch)
|
|
limiter = RateLimiter(requests_per_second=4)
|
|
|
|
for _ in range(5):
|
|
await limiter.acquire()
|
|
|
|
# First grant is immediate; the next four are spaced one interval apart.
|
|
assert len(sleeps) == 4
|
|
for index, delay in enumerate(sleeps, start=1):
|
|
assert delay == pytest.approx(index * 0.25, abs=0.05)
|
|
|
|
async def test_concurrent_callers_are_spaced(self):
|
|
rate = 50
|
|
interval = 1 / rate
|
|
count = 5
|
|
limiter = RateLimiter(requests_per_second=rate)
|
|
|
|
loop = asyncio.get_running_loop()
|
|
start = loop.time()
|
|
await asyncio.gather(*(limiter.acquire() for _ in range(count)))
|
|
elapsed = loop.time() - start
|
|
|
|
assert elapsed >= (count - 1) * interval
|
|
assert elapsed < (count - 1) * interval + 0.5
|
|
|
|
@pytest.mark.parametrize("rate", [0, -1, -0.5])
|
|
def test_non_positive_rate_raises(self, rate):
|
|
with pytest.raises(ValueError):
|
|
RateLimiter(requests_per_second=rate)
|
|
|
|
async def test_raising_the_rate_shortens_the_interval(self, monkeypatch):
|
|
sleeps = _record_sleeps(monkeypatch)
|
|
limiter = RateLimiter(requests_per_second=1)
|
|
|
|
limiter.set_requests_per_second(4)
|
|
await limiter.acquire()
|
|
await limiter.acquire()
|
|
|
|
assert limiter.requests_per_second == pytest.approx(4)
|
|
assert sleeps == [pytest.approx(0.25, abs=0.05)]
|
|
|
|
async def test_lowering_the_rate_widens_the_interval(self, monkeypatch):
|
|
sleeps = _record_sleeps(monkeypatch)
|
|
limiter = RateLimiter(requests_per_second=4)
|
|
|
|
limiter.set_requests_per_second(1)
|
|
await limiter.acquire()
|
|
await limiter.acquire()
|
|
|
|
assert sleeps == [pytest.approx(1.0, abs=0.05)]
|
|
|
|
@pytest.mark.parametrize("rate", [0, -1, -0.5])
|
|
def test_setting_non_positive_rate_raises(self, rate):
|
|
limiter = RateLimiter(requests_per_second=4)
|
|
with pytest.raises(ValueError):
|
|
limiter.set_requests_per_second(rate)
|
|
|
|
|
|
class TestConcurrencyLimiter:
|
|
@pytest.mark.parametrize("value", [0, -1])
|
|
def test_non_positive_capacity_raises(self, value):
|
|
with pytest.raises(ValueError):
|
|
ConcurrencyLimiter(max_concurrency=value)
|
|
|
|
async def test_acquire_release_tracks_in_flight(self):
|
|
limiter = ConcurrencyLimiter(max_concurrency=2)
|
|
|
|
await limiter.acquire()
|
|
await limiter.acquire()
|
|
assert limiter.in_flight == 2
|
|
|
|
limiter.release()
|
|
assert limiter.in_flight == 1
|
|
|
|
async def test_context_manager_releases(self):
|
|
limiter = ConcurrencyLimiter(max_concurrency=1)
|
|
|
|
async with limiter:
|
|
assert limiter.in_flight == 1
|
|
assert limiter.in_flight == 0
|
|
|
|
async def test_context_manager_releases_on_error(self):
|
|
limiter = ConcurrencyLimiter(max_concurrency=1)
|
|
|
|
with pytest.raises(RuntimeError):
|
|
async with limiter:
|
|
raise RuntimeError("boom")
|
|
|
|
assert limiter.in_flight == 0
|
|
|
|
async def test_acquire_blocks_until_slot_freed(self):
|
|
limiter = ConcurrencyLimiter(max_concurrency=1)
|
|
await limiter.acquire()
|
|
|
|
waiter = asyncio.ensure_future(limiter.acquire())
|
|
await asyncio.sleep(0) # Let the waiter run and block.
|
|
assert not waiter.done()
|
|
|
|
limiter.release()
|
|
await asyncio.wait_for(waiter, timeout=1)
|
|
assert limiter.in_flight == 1
|
|
|
|
async def test_set_max_concurrency_wakes_waiters(self):
|
|
limiter = ConcurrencyLimiter(max_concurrency=1)
|
|
await limiter.acquire()
|
|
|
|
waiter = asyncio.ensure_future(limiter.acquire())
|
|
await asyncio.sleep(0)
|
|
assert not waiter.done()
|
|
|
|
# Opening a second slot should release the blocked acquirer.
|
|
limiter.set_max_concurrency(2)
|
|
await asyncio.wait_for(waiter, timeout=1)
|
|
assert limiter.in_flight == 2
|
|
|
|
async def test_lowering_capacity_blocks_new_acquirers(self):
|
|
limiter = ConcurrencyLimiter(max_concurrency=2)
|
|
await limiter.acquire()
|
|
limiter.set_max_concurrency(1)
|
|
|
|
waiter = asyncio.ensure_future(limiter.acquire())
|
|
await asyncio.sleep(0)
|
|
# Already at the new cap, so the next acquirer must wait.
|
|
assert not waiter.done()
|
|
|
|
limiter.release()
|
|
await asyncio.wait_for(waiter, timeout=1)
|
|
assert limiter.in_flight == 1
|
|
|
|
@pytest.mark.parametrize("value", [0, -1])
|
|
def test_set_non_positive_capacity_raises(self, value):
|
|
limiter = ConcurrencyLimiter(max_concurrency=1)
|
|
with pytest.raises(ValueError):
|
|
limiter.set_max_concurrency(value)
|