A Kometa YAML request asks Jolpica for every season at once; Jolpica (4 requests a second, 500 an hour) answered 429 for 24 seasons on 2026-11-27 09:28, and lru_cache kept each failure as an empty schedule, so every poster and thumb of those seasons answered 404 until a restart. The service also ran two Jolpica clients, so the race lookup and the posters held different schedules. Now one client serves the whole service; a season that is over is kept on disk in the cache volume for good, one not yet over is asked again after six hours; requests are paced under the limit, a 429 is asked again after its Retry-After; a failure is never kept. The Kometa endpoint runs in the threadpool, as its first fill takes minutes. Claude-Session: https://claude.ai/code/session_01NcbgfJAa38xFpzy69BWrqh
51 lines
2.0 KiB
Python
51 lines
2.0 KiB
Python
"""A season's schedule: a failed fetch is never kept, a season that is over is read from disk, and a
|
|
request Jolpica throttles is asked again."""
|
|
import io
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
import urllib.error
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
from ergast_service import ErgastService
|
|
|
|
RACES = [{"raceName": "Monaco Grand Prix", "Circuit": {"circuitId": "monaco", "circuitName": "Circuit de Monaco"}}]
|
|
|
|
|
|
class ScheduleTest(unittest.TestCase):
|
|
def setUp(self):
|
|
kept = tempfile.TemporaryDirectory()
|
|
self.addCleanup(kept.cleanup)
|
|
self.cache_dir = Path(kept.name)
|
|
|
|
def service(self) -> ErgastService:
|
|
service = ErgastService(cache_dir=self.cache_dir)
|
|
service.REQUEST_INTERVAL = 0
|
|
return service
|
|
|
|
def test_a_failed_fetch_is_not_kept(self):
|
|
service = self.service()
|
|
with mock.patch.object(service, "_ask_jolpica", side_effect=[None, RACES]) as ask:
|
|
self.assertEqual(service._fetch_season_schedule(2019), [])
|
|
self.assertEqual(service._fetch_season_schedule(2019), RACES)
|
|
self.assertEqual(ask.call_count, 2)
|
|
|
|
def test_a_season_that_is_over_is_read_from_disk(self):
|
|
with mock.patch.object(ErgastService, "_ask_jolpica", return_value=RACES):
|
|
self.service()._fetch_season_schedule(2019)
|
|
with mock.patch.object(ErgastService, "_ask_jolpica") as ask:
|
|
self.assertEqual(self.service()._fetch_season_schedule(2019), RACES)
|
|
ask.assert_not_called()
|
|
|
|
def test_a_throttled_request_is_asked_again(self):
|
|
throttled = urllib.error.HTTPError("url", 429, "Too Many Requests", {"Retry-After": "0"}, None)
|
|
answer = io.BytesIO(json.dumps({"MRData": {"RaceTable": {"Races": RACES}}}).encode())
|
|
with mock.patch("urllib.request.urlopen", side_effect=[throttled, answer]) as urlopen:
|
|
self.assertEqual(self.service()._ask_jolpica(2019), RACES)
|
|
self.assertEqual(urlopen.call_count, 2)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|