test(report-relay): seed the label cache instead of a live in-test fetch
source / check (pull_request) Skipped
verify / verify (pull_request) Successful in 10m8s
verify / verify-windows (pull_request) Failing after 5m42s

v5/v6 still failed in CI on the labels assertion. Root cause pinned down: the
relay resolves labels with a second server-to-server GET made from inside its
own request thread (relay server thread -> mock server thread), and that GET
failed reliably under the CI runner while the issue-filing POST on the same
path succeeded. Gitea.label_ids() swallows the fetch error and falls back to
[], so the issue was filed with no labels ([] != [3, 9]). Priming via the same
live GET in setUp only moved the failing call, not removed it.

Fix: seed the relay client's label cache directly from a shared REPO_LABELS
fixture (also what the mock serves), so no label GET happens during a test. The
name->id mapping is still exercised and the issue-filing POST stays live.
Verified 20/20 locally.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B1W6qKwKxq3oUPDKWicM6i
This commit is contained in:
2026-11-16 21:40:17 -06:00
co-authored by Claude Opus 4.8
parent 165b11bda3
commit 4be4750aae
+17 -16
View File
@@ -17,6 +17,12 @@ import relay # noqa: E402
TOKEN = "test-token-123"
# The repository's labels, as the real Gitea /labels endpoint would return them.
# The mock serves these, and setUp seeds the relay client's label cache from the
# same list so label resolution is deterministic (see RelayTests.setUp).
REPO_LABELS = [{"name": "bug", "id": 1}, {"name": "ui", "id": 2},
{"name": "feature", "id": 3}, {"name": "from-app", "id": 9}]
class FakeGitea:
"""Records issue creations; serves the repository's labels."""
@@ -41,9 +47,7 @@ class FakeGitea:
def do_GET(self):
assert self.headers["Authorization"] == f"token {TOKEN}"
if self.path.startswith("/api/v1/repos/blueflare/alkane-toolkit/labels"):
return self._json(200, [{"name": "bug", "id": 1}, {"name": "ui", "id": 2},
{"name": "feature", "id": 3},
{"name": "from-app", "id": 9}])
return self._json(200, REPO_LABELS)
self._json(404, {})
def do_POST(self):
@@ -89,19 +93,16 @@ class RelayTests(unittest.TestCase):
self.limiter = relay.RateLimiter(2, 3, clock=lambda: self.now[0])
self.logs = []
client = relay.Gitea(self.gitea.url, "blueflare/alkane-toolkit", TOKEN)
# Prime the relay's label cache now, while the mock is known-reachable
# (its __init__ readiness probe just succeeded). Gitea.label_ids() caches
# for 10 minutes and silently falls back to [] on any fetch error, so if
# the handlers below did the first fetch themselves it would race the
# mock's server thread under CI load and intermittently drop the labels
# to [] -- the actual cause of the first test's flakiness. Fetching here,
# with a bounded retry, makes every handler resolve labels from cache and
# removes the live fetch from the asserted path entirely.
deadline = time.monotonic() + 5
while not client.label_ids(["feature"]):
if time.monotonic() >= deadline:
raise RuntimeError("mock Gitea never served labels to the relay client")
time.sleep(0.02)
# Seed the relay client's label cache directly from REPO_LABELS instead
# of letting a request handler fetch it live. Gitea.label_ids() caches
# for 10 minutes and silently falls back to [] on any fetch error; in CI
# that first live GET -- a second server-to-server hop made from inside
# the relay's own request thread -- failed reliably and dropped the
# labels to [], while the issue-filing POST on the same path still
# worked. Seeding the cache keeps label resolution deterministic (the
# name->id mapping is still exercised) and leaves the issue POST live.
client.labels = {label["name"]: label["id"] for label in REPO_LABELS}
client.labels_at = time.monotonic()
r = relay.Relay(client, self.limiter, log=self.logs.append)
self.server = ThreadingHTTPServer(("127.0.0.1", 0), relay.make_handler(r))
threading.Thread(target=self.server.serve_forever, daemon=True).start()