diff --git a/tools/report-relay/test_relay.py b/tools/report-relay/test_relay.py index 2e1815c6..bae361cc 100644 --- a/tools/report-relay/test_relay.py +++ b/tools/report-relay/test_relay.py @@ -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()