Skip to content

Commit

Permalink
test: always stop server used to test shutdown message
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed May 2, 2024
1 parent 5923ba6 commit 5b0158d
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions tests/safeds_runner/server/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)

BASE_TIMEOUT = 10
PORT = 17394
PORT = 5000
URL = f"http://localhost:{PORT}"


Expand All @@ -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


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5b0158d

Please sign in to comment.