From 5b0158ddf687bd1ca3d8f155ee633beb29d30e35 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Thu, 2 May 2024 22:14:39 +0200 Subject: [PATCH] test: always stop server used to test `shutdown` message --- tests/safeds_runner/server/test_server.py | 42 ++++++++++++----------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/tests/safeds_runner/server/test_server.py b/tests/safeds_runner/server/test_server.py index abe6af7..44b4d88 100644 --- a/tests/safeds_runner/server/test_server.py +++ b/tests/safeds_runner/server/test_server.py @@ -28,7 +28,7 @@ ) BASE_TIMEOUT = 10 -PORT = 17394 +PORT = 5000 URL = f"http://localhost:{PORT}" @@ -54,14 +54,14 @@ def run_server(): @pytest.fixture() async def client_1() -> socketio.AsyncSimpleClient: async with socketio.AsyncSimpleClient() as sio: - await sio.connect(URL, transports=["websocket"]) + await sio.connect(URL, wait_timeout=BASE_TIMEOUT) yield sio @pytest.fixture() async def client_2() -> socketio.AsyncSimpleClient: async with socketio.AsyncSimpleClient() as sio: - await sio.connect(URL, transports=["websocket"]) + await sio.connect(URL, wait_timeout=BASE_TIMEOUT) yield sio @@ -436,23 +436,25 @@ async def test_shutdown() -> None: process = multiprocessing.Process(target=run_server_to_shutdown) process.start() - # Send a shutdown message - async with socketio.AsyncSimpleClient() as client_: - await client_.connect(SHUTDOWN_URL, transports=["websocket"]) - await client_.emit(create_shutdown_message().event) - - # Joining on the process can lead to a loss of the shutdown message - for _ in range(10 * BASE_TIMEOUT): - if not process.is_alive(): - break - await asyncio.sleep(0.1) - - # Kill the process and all child processes if it did not shut down in time - if process.is_alive(): - parent = psutil.Process(process.pid) - for child in parent.children(recursive=True): - child.kill() - pytest.fail("Server did not shut down in time.") + try: + # Send a shutdown message + async with socketio.AsyncSimpleClient() as client_: + await client_.connect(SHUTDOWN_URL, wait_timeout=BASE_TIMEOUT) + await client_.emit(create_shutdown_message().event) + + # Joining on the process can lead to a loss of the shutdown message + for _ in range(10 * BASE_TIMEOUT): + if not process.is_alive(): + break + await asyncio.sleep(0.1) + finally: + # Kill the process and all child processes if it did not shut down in time + if process.is_alive(): + parent = psutil.Process(process.pid) + for child in parent.children(recursive=True): + child.kill() + parent.kill() + pytest.fail("Server did not shut down in time.") # Check the exit code assert process.exitcode == 0