diff --git a/tools/report-relay/test_relay.py b/tools/report-relay/test_relay.py index 07f53a10..ffde9d99 100644 --- a/tools/report-relay/test_relay.py +++ b/tools/report-relay/test_relay.py @@ -7,6 +7,7 @@ import json import pathlib import sys import threading +import time import unittest import urllib.error import urllib.request @@ -58,6 +59,23 @@ class FakeGitea: self.server = ThreadingHTTPServer(("127.0.0.1", 0), H) threading.Thread(target=self.server.serve_forever, daemon=True).start() self.url = f"http://127.0.0.1:{self.server.server_address[1]}" + # Block until the serve_forever thread is actually handling requests, so + # the relay's first label fetch cannot race server startup. The socket + # listens from __init__, but under load the daemon thread can be starved + # past the relay's HTTP timeout, which silently drops labels to [] and + # made the first test flaky in CI. + probe = urllib.request.Request( + self.url + "/api/v1/repos/blueflare/alkane-toolkit/labels", + headers={"Authorization": f"token {TOKEN}"}) + deadline = time.monotonic() + 5 + while True: + try: + urllib.request.urlopen(probe, timeout=1).read() + break + except Exception: + if time.monotonic() >= deadline: + raise + time.sleep(0.02) def close(self): self.server.shutdown()