Skip to content

Commit 8ec6c40

Browse files
committed
Catch errors from TestServer (instead of hanging if server.started never becomes true)
1 parent b5addb6 commit 8ec6c40

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

tests/conftest.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,26 @@ async def watch_restarts(self) -> None: # pragma: no cover
269269

270270

271271
def serve_in_thread(server: TestServer) -> typing.Iterator[TestServer]:
272-
thread = threading.Thread(target=server.run)
272+
server_exception = None
273+
server_caught_exception = threading.Event()
274+
275+
def _run_server() -> None:
276+
nonlocal server_exception
277+
try:
278+
server.run()
279+
except BaseException as exc: # pragma: nocover # Yes, we need to catch SystemExit too
280+
server_exception = exc
281+
server_caught_exception.set()
282+
283+
thread = threading.Thread(target=_run_server)
273284
thread.start()
285+
274286
try:
275287
while not server.started:
276-
time.sleep(1e-3)
288+
if server_caught_exception.wait(1e-3):
289+
raise RuntimeError(
290+
f"Server failed to start: {server_exception!r}",
291+
) from server_exception
277292
yield server
278293
finally:
279294
server.should_exit = True

0 commit comments

Comments
 (0)