fix(fetcher): quota reserve and per-tick cap for refresh cron

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shay
2026-11-30 09:15:57 -08:00
co-authored by Claude Opus 5.5
parent d4b4a8bc52
commit 01afeeda4e
4 changed files with 65 additions and 3 deletions
+32
View File
@@ -50,3 +50,35 @@ def test_tick_runs_gc_for_abandoned_trial_users(tmp_db):
stats = cronmod.tick(hours=24, active_within_days=7, gc_days=7)
assert stats["gc_removed"] == 1
assert dbmod.get_user("ghost") is None
def _due(*names):
# Backdate each user so they're due; later names are fetched more recently.
for i, n in enumerate(names):
dbmod.upsert_user(n, {"user": {"login": n}})
with dbmod._connect() as c:
c.execute("UPDATE users SET fetched_at=?, last_requested_at=? WHERE username=?",
(f"2020-01-0{i + 1}T00:00:00Z", dbmod._now(), n))
def test_tick_caps_refreshes_oldest_first(tmp_db, monkeypatch):
_due("old", "mid", "new")
monkeypatch.setattr(cronmod.config, "REFRESH_MAX_PER_TICK", 2)
monkeypatch.setattr(cronmod, "_core_remaining", lambda: None)
with patch("src.cron.github.fetch_github_data", side_effect=lambda u, **_k: {"user": {"login": u}}) as gh:
stats = cronmod.tick(hours=24, active_within_days=7, gc_days=7)
assert [c.args[0] for c in gh.call_args_list] == ["old", "mid"]
assert stats["refreshed"] == 2 and stats["deferred"] == 1
def test_tick_stops_under_core_reserve(tmp_db, monkeypatch):
# New sign-ups need the quota more than a day-old card does: once the
# PAT's core budget drops under the reserve, the tick stops refreshing.
_due("a", "b", "c")
monkeypatch.setattr(cronmod.config, "REFRESH_CORE_RESERVE", 1500)
left = iter([4000, 1400, 1400])
monkeypatch.setattr(cronmod, "_core_remaining", lambda: next(left))
with patch("src.cron.github.fetch_github_data", side_effect=lambda u, **_k: {"user": {"login": u}}) as gh:
stats = cronmod.tick(hours=24, active_within_days=7, gc_days=7)
assert [c.args[0] for c in gh.call_args_list] == ["a"]
assert stats["refreshed"] == 1 and stats["deferred"] == 2