Skip to content

Commit 65e2d86

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

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

tests/conftest.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,28 @@ 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
280+
# BaseException as we need to catch SystemExit too;
281+
# `uvicorn` calls `sys.exit(1)` at failure.
282+
server_exception = exc
283+
server_caught_exception.set()
284+
285+
thread = threading.Thread(target=_run_server)
273286
thread.start()
287+
274288
try:
275289
while not server.started:
276-
time.sleep(1e-3)
290+
if server_caught_exception.wait(1e-3):
291+
raise RuntimeError(
292+
f"Server failed to start: {server_exception!r}",
293+
) from server_exception
277294
yield server
278295
finally:
279296
server.should_exit = True

0 commit comments

Comments
 (0)