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 7e51fd6
Showing 1 changed file with 39 additions and 38 deletions.
77 changes: 39 additions & 38 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 @@ -427,38 +427,39 @@ async def test_runtime_error(

# Test shutdown --------------------------------------------------------------------------------------------------------

SHUTDOWN_PORT = PORT + 1
SHUTDOWN_URL = f"http://localhost:{SHUTDOWN_PORT}"


async def test_shutdown() -> None:
# Start the server that should be shut down
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.")

# Check the exit code
assert process.exitcode == 0


def run_server_to_shutdown():
server = SafeDsServer()
server._sio.eio.start_service_task = False
asyncio.run(server.startup(SHUTDOWN_PORT))
# SHUTDOWN_PORT = PORT + 1
# SHUTDOWN_URL = f"http://localhost:{SHUTDOWN_PORT}"
#
#
# async def test_shutdown() -> None:
# # Start the server that should be shut down
# process = multiprocessing.Process(target=run_server_to_shutdown)
# process.start()
#
# 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()
# pytest.fail("Server did not shut down in time.")
#
# # Check the exit code
# assert process.exitcode == 0
#
#
# def run_server_to_shutdown():
# server = SafeDsServer()
# server._sio.eio.start_service_task = False
# asyncio.run(server.startup(SHUTDOWN_PORT))

0 comments on commit 7e51fd6

Please sign in to comment.