fix(api): /health never serves a cached result older than 4 TTLs
CI / Format & lint (push) Successful in 27s
CI / Test suite (push) Successful in 56s
Docs / Validate documentation (push) Successful in 26s
Security / Dependency vulnerabilities (push) Successful in 2m2s
Security / Static analysis (push) Successful in 21s
Security / License report (push) Successful in 1m50s

Stale-while-revalidate answered the first /health after a quiet night with
the previous evening's state (seen after the collector cutover: 'Latest data
is old: 21:00' at 13:47 while data was current). _cached_swr gains max_stale;
/health uses 4 x HEALTH_CACHE_TTL_SECONDS.
This commit is contained in:
grabowski committed 2026-10-18 13:49:39 +07:00
1 parent 9b910c3cbf
commit 64c5b02e06
2 files changed
+39 -2

No files matched your search

+27
View File
@@ -94,3 +94,30 @@ def test_manual_trigger_is_refused_when_collection_runs_elsewhere(monkeypatch):
with pytest.raises(HTTPException) as err:
asyncio.run(web_api.trigger_scraping(BackgroundTasks()))
assert err.value.status_code == 409
def test_stale_cache_older_than_max_stale_is_recomputed(monkeypatch):
"""/health must not answer with a result from hours ago."""
import asyncio
import threading
import time
from src import web_api
cache, lock, compute_lock = {}, threading.Lock(), threading.Lock()
cache["k"] = (time.monotonic() - 3600, "an hour old")
value, stale = asyncio.run(
web_api._cached_swr(
cache, lock, compute_lock, "k", 30, lambda: "fresh", max_stale=120
)
)
assert (value, stale) == ("fresh", False)
cache["k"] = (time.monotonic() - 60, "a minute old")
value, stale = asyncio.run(
web_api._cached_swr(
cache, lock, compute_lock, "k", 30, lambda: "fresh", max_stale=120
)
)
assert (value, stale) == ("a minute old", True) # SWR within the bound