From 88f6f561d2ddfdd8793925a7970c17227191ff38 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Thu, 2 May 2024 23:20:24 +0200 Subject: [PATCH] test: wait until the server to test shutdown is ready to accept connections --- tests/safeds_runner/server/test_server.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tests/safeds_runner/server/test_server.py b/tests/safeds_runner/server/test_server.py index 05f30f5..b86be3c 100644 --- a/tests/safeds_runner/server/test_server.py +++ b/tests/safeds_runner/server/test_server.py @@ -4,6 +4,7 @@ import itertools import json import multiprocessing +import sys import threading import psutil @@ -439,9 +440,18 @@ async def test_runtime_error( async def test_shutdown() -> None: # Start the server that should be shut down - process = multiprocessing.Process(target=run_server_to_shutdown) + stdout_r, stdout_w = multiprocessing.Pipe() + process = multiprocessing.Process(target=run_server_to_shutdown, args=[stdout_w]) process.start() + # Wait until the server is ready to accept connections + for _ in range(10 * BASE_TIMEOUT): + if not stdout_r.poll(0.1): + continue + + if "Started server process" in str(stdout_r.recv()).strip(): + break + try: # Send a shutdown message async with socketio.AsyncSimpleClient() as client_: @@ -466,7 +476,10 @@ async def test_shutdown() -> None: assert process.exitcode == 0 -def run_server_to_shutdown(): +def run_server_to_shutdown(pipe: multiprocessing.connection.Connection): + sys.stdout.write = lambda value: pipe.send(value) # type: ignore[method-assign, assignment] + sys.stderr.write = lambda value: pipe.send(value) # type: ignore[method-assign, assignment] + server = SafeDsServer() server._sio.eio.start_service_task = False asyncio.run(server.startup(SHUTDOWN_PORT))