test(web): trim the gh auth probe tests to two invariants

Keep: overlapping cache misses share one probe (never two `gh` at once);
a refresh issued mid-probe after a login flip gets the fresh answer and
still never overlaps. Dropped: the bounded-helper call-shape detector (the
descendant cleanup is proven by the live wrapper probe, not by asserting
the helper's name), and the two tests of pre-existing behaviour (fresh
cache short-circuit, missing `gh`).
This commit is contained in:
teknium1
2026-09-15 12:20:20 -07:00
committed by Teknium
parent ac829e8dee
commit b8052c8d4a
+51 -52
View File
@@ -14,73 +14,72 @@ from starlette.testclient import TestClient
@pytest.fixture(autouse=True)
def reset_gh_auth_probe_state():
previous_cache = git_router._gh_auth_cache
previous_task = git_router._gh_auth_probe_task
git_router._gh_auth_cache = None
git_router._gh_auth_probe_task = None
previous = (git_router._gh_auth_cache, git_router._gh_auth_probe_task, git_router._gh_auth_probe_started)
git_router._gh_auth_cache, git_router._gh_auth_probe_task, git_router._gh_auth_probe_started = None, None, 0.0
try:
yield
finally:
git_router._gh_auth_cache = previous_cache
git_router._gh_auth_probe_task = previous_task
git_router._gh_auth_cache, git_router._gh_auth_probe_task, git_router._gh_auth_probe_started = previous
def test_gh_auth_probe_uses_bounded_process_probe(monkeypatch):
monkeypatch.setattr(git_router.shutil, "which", lambda _: "/usr/bin/gh")
calls = []
class _GhProbe:
"""Stand-in for ``_probe_gh_auth``: blocks until released, reports how many ran and how many overlapped."""
def bounded(argv, *, timeout):
calls.append((argv, timeout))
return None # The bounded helper returns None after timeout/tree cleanup.
def __init__(self):
self.started, self.release = threading.Event(), threading.Event()
self.calls, self.running, self.peak, self.logged_in = 0, 0, 0, False
self._lock = threading.Lock()
monkeypatch.setattr(git_router, "bounded_probe_run", bounded)
assert git_router._probe_gh_auth() == {"available": True, "authenticated": False}
assert calls == [(["/usr/bin/gh", "auth", "status"], 10)]
def __call__(self):
with self._lock:
self.calls += 1
self.running += 1
self.peak = max(self.peak, self.running)
answer = self.logged_in # read at START, like the real `gh auth status`
self.started.set()
assert self.release.wait(timeout=2)
with self._lock:
self.running -= 1
return {"available": True, "authenticated": answer}
def test_gh_auth_concurrent_refreshes_share_one_probe(monkeypatch):
started = threading.Event()
release = threading.Event()
calls = 0
def probe():
nonlocal calls
calls += 1
started.set()
assert release.wait(timeout=1)
return {"available": True, "authenticated": True}
def test_gh_auth_concurrent_cache_misses_share_one_probe(monkeypatch):
"""Overlapping requests never start a second `gh` while one is in flight (#111509)."""
probe = _GhProbe()
monkeypatch.setattr(git_router, "_probe_gh_auth", probe)
async def exercise():
first = asyncio.create_task(git_router.gh_auth_status_route(refresh=True))
while not started.is_set():
tasks = [asyncio.create_task(git_router.gh_auth_status_route()) for _ in range(5)]
while not probe.started.is_set():
await asyncio.sleep(0)
second = asyncio.create_task(git_router.gh_auth_status_route(refresh=True))
probe.release.set()
return await asyncio.gather(*tasks)
assert asyncio.run(exercise()) == [{"available": True, "authenticated": False}] * 5
assert (probe.calls, probe.peak) == (1, 1)
def test_gh_auth_refresh_waits_out_a_probe_started_before_it(monkeypatch):
"""``refresh=true`` issued after `gh auth login` must not adopt the answer of a probe that started
while still logged out — and must not run a second `gh` concurrently to get its own."""
probe = _GhProbe()
monkeypatch.setattr(git_router, "_probe_gh_auth", probe)
async def exercise():
stale = asyncio.create_task(git_router.gh_auth_status_route()) # cache miss while logged out
while not probe.started.is_set():
await asyncio.sleep(0)
probe.logged_in = True # `gh auth login` completes
refreshed = asyncio.create_task(git_router.gh_auth_status_route(refresh=True))
await asyncio.sleep(0)
assert calls == 1
release.set()
return await asyncio.gather(first, second)
probe.release.set()
return await stale, await refreshed, await git_router.gh_auth_status_route()
assert asyncio.run(exercise()) == [
{"available": True, "authenticated": True},
{"available": True, "authenticated": True},
]
assert calls == 1
def test_gh_auth_returns_fresh_cache_without_probing(monkeypatch):
git_router._gh_auth_cache = (git_router.time.monotonic(), {"available": True, "authenticated": True})
monkeypatch.setattr(git_router, "_probe_gh_auth", lambda: pytest.fail("cache miss"))
assert asyncio.run(git_router.gh_auth_status_route()) == {"available": True, "authenticated": True}
def test_gh_auth_reports_unavailable_when_gh_is_missing(monkeypatch):
monkeypatch.setattr(git_router.shutil, "which", lambda _: None)
assert git_router._probe_gh_auth() == {"available": False, "authenticated": False}
stale, refreshed, cached = asyncio.run(exercise())
assert stale == {"available": True, "authenticated": False}
assert refreshed == {"available": True, "authenticated": True}
assert cached == {"available": True, "authenticated": True} # the TTL cache holds the fresh answer
assert (probe.calls, probe.peak) == (2, 1)
@pytest.fixture