Do not wait forever for test server to start or stop

This commit is contained in:
Aarni Koskela 2026-02-24 15:22:59 +02:00
parent 65e2d86662
commit 8a959d830e

View File

@ -268,7 +268,11 @@ class TestServer(Server):
await self.startup()
def serve_in_thread(server: TestServer) -> typing.Iterator[TestServer]:
def serve_in_thread(
server: TestServer,
*,
timeout: float = 10.0,
) -> typing.Iterator[TestServer]:
server_exception = None
server_caught_exception = threading.Event()
@ -286,15 +290,21 @@ def serve_in_thread(server: TestServer) -> typing.Iterator[TestServer]:
thread.start()
try:
while not server.started:
if server_caught_exception.wait(1e-3):
start_time = time.time()
while True:
if server.started:
break
if server_caught_exception.wait(1e-3): # pragma: nocover
raise RuntimeError(
f"Server failed to start: {server_exception!r}",
) from server_exception
if time.time() - start_time > timeout: # pragma: nocover
raise TimeoutError("Server did not start in time")
time.sleep(1e-3)
yield server
finally:
server.should_exit = True
thread.join()
thread.join(timeout=timeout)
@pytest.fixture(scope="session")