mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
126 lines
5.5 KiB
Python
126 lines
5.5 KiB
Python
import os
|
|
import tempfile
|
|
import pytest
|
|
from unittest.mock import patch
|
|
from src import db as dbmod
|
|
from src import cron as cronmod
|
|
from src import github as ghmod
|
|
|
|
|
|
@pytest.fixture
|
|
def tmp_db(monkeypatch):
|
|
with tempfile.TemporaryDirectory() as d:
|
|
monkeypatch.setattr(dbmod, "DB_PATH", os.path.join(d, "t.db"))
|
|
dbmod.init_db()
|
|
yield
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def sleeps(monkeypatch):
|
|
# The tick pauses between users and before a retry; record, don't wait.
|
|
calls = []
|
|
monkeypatch.setattr(cronmod.time, "sleep", calls.append)
|
|
return calls
|
|
|
|
|
|
def test_refresh_tick_calls_github_for_due_users(tmp_db):
|
|
dbmod.upsert_user("alice", {"user": {"login": "alice"}})
|
|
# Backdate
|
|
with dbmod._connect() as c:
|
|
c.execute("UPDATE users SET fetched_at='2020-01-01T00:00:00Z', last_requested_at=? WHERE username='alice'",
|
|
(dbmod._now(),))
|
|
with patch("src.cron.github.fetch_github_data", return_value={"user": {"login": "alice", "updated": True}}) as gh:
|
|
stats = cronmod.tick(hours=24, active_within_days=7, gc_days=7)
|
|
assert gh.called
|
|
assert stats["refreshed"] == 1
|
|
|
|
|
|
def test_refresh_rate_limit_keeps_last_good_and_counts_metric(tmp_db):
|
|
# A rate-limited refresh must NOT overwrite the cached row, and must be
|
|
# counted as rate_limited so the monitoring stack can see it.
|
|
dbmod.upsert_user("alice", {"user": {"login": "alice"}, "total_commits": 1841})
|
|
with dbmod._connect() as c:
|
|
c.execute("UPDATE users SET fetched_at='2020-01-01T00:00:00Z', last_requested_at=? WHERE username='alice'",
|
|
(dbmod._now(),))
|
|
with patch("src.cron.github.fetch_github_data",
|
|
side_effect=ghmod.GitHubTransientError("rate limited")):
|
|
stats = cronmod.tick(hours=24, active_within_days=7, gc_days=7)
|
|
assert stats["failed"] == 1 and stats["refreshed"] == 0
|
|
# Last-good data retained.
|
|
assert dbmod.get_user("alice")["data"]["total_commits"] == 1841
|
|
assert dbmod.read_fetch_metrics()["rate_limited"] == 1
|
|
|
|
|
|
def test_tick_runs_gc_for_abandoned_trial_users(tmp_db):
|
|
dbmod.upsert_user("ghost", {"user": {"login": "ghost"}})
|
|
with dbmod._connect() as c:
|
|
c.execute("UPDATE users SET last_requested_at='2020-01-01T00:00:00Z' WHERE username='ghost'")
|
|
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
|
|
|
|
|
|
def test_tick_retries_once_after_capped_retry_after(tmp_db, monkeypatch, sleeps):
|
|
# A secondary rate limit clears after a wait: honour Retry-After (capped)
|
|
# and retry the same user once; pause between users.
|
|
_due("a", "b")
|
|
monkeypatch.setattr(cronmod, "_core_remaining", lambda: None)
|
|
results = iter([ghmod.GitHubTransientError("rate limited", retry_after=500),
|
|
{"user": {"login": "a"}}, {"user": {"login": "b"}}])
|
|
|
|
def fake(u, **_k):
|
|
r = next(results)
|
|
if isinstance(r, Exception):
|
|
raise r
|
|
return r
|
|
with patch("src.cron.github.fetch_github_data", side_effect=fake) 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", "a", "b"]
|
|
assert sleeps == [120, 1.5]
|
|
assert stats["refreshed"] == 2 and stats["failed"] == 0
|
|
|
|
|
|
def test_tick_stops_and_defers_after_second_rate_limit(tmp_db, monkeypatch, sleeps):
|
|
# Hammering on after a repeat rate limit only fails every remaining user;
|
|
# stop the tick and leave them due for the next one.
|
|
_due("a", "b", "c")
|
|
monkeypatch.setattr(cronmod, "_core_remaining", lambda: None)
|
|
with patch("src.cron.github.fetch_github_data",
|
|
side_effect=ghmod.GitHubTransientError("rate limited")) 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", "a"]
|
|
assert sleeps == [60]
|
|
assert stats["failed"] == 1 and stats["refreshed"] == 0 and stats["deferred"] == 2
|