From 4738b0a48e726026d6d444893dd514b3e0a715f6 Mon Sep 17 00:00:00 2001 From: Simon Holesch Date: Thu, 15 Aug 2024 14:08:01 +0200 Subject: [PATCH] tests: Use Ready Notification Instead of Polling Wait for the services to print a line to stdout, instead of waiting for the port to be opened. --- tests/test_http.py | 11 +++-------- tests/test_usbip.py | 23 ++++------------------- tests/util.py | 9 ++++++--- 3 files changed, 13 insertions(+), 30 deletions(-) diff --git a/tests/test_http.py b/tests/test_http.py index e97fbe4..8aa5830 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -25,18 +25,14 @@ async def tinyproxy(): async def test_proxy_connect(tinyproxy): - async with sh_task("not-my-board hub", "hub"): - await wait_for_ports(2092) - + async with sh_task("not-my-board hub", "hub", wait_ready=True): client = http.Client(proxies={"http": tinyproxy}) response = await client.get_json("http://127.0.0.1:2092/api/v1/places") assert response == {"places": []} async def test_proxy_ignore(): - async with sh_task("not-my-board hub", "hub"): - await wait_for_ports(2092) - + async with sh_task("not-my-board hub", "hub", wait_ready=True): client = http.Client( proxies={"http": "http://non-existing.localhost", "no": "127.0.0.1"} ) @@ -89,9 +85,8 @@ async def test_proxy_connect_https(tinyproxy): "not_my_board:asgi_app" ), "hub", + wait_ready=True, ): - await wait_for_ports(2092) - client = http.Client(ca_files=[root_cert], proxies={"https": tinyproxy}) response = await client.get_json("https://127.0.0.1:2092/api/v1/places") assert response == {"places": []} diff --git a/tests/test_usbip.py b/tests/test_usbip.py index bba3215..f66eadc 100644 --- a/tests/test_usbip.py +++ b/tests/test_usbip.py @@ -3,11 +3,8 @@ async def test_raw_usb_forwarding(vms): async with vms.exporter.ssh_task_root( - "python3 -m not_my_board._usbip export 2-1", "usbip export" + "python3 -m not_my_board._usbip export 2-1", "usbip export", wait_ready=True ): - # wait for listening socket - await vms.exporter.ssh_poll("nc -z 127.0.0.1 3240") - async with vms.client.ssh_task_root( "python3 -m not_my_board._usbip import exporter.local 2-1 0", "usbip import", @@ -26,27 +23,15 @@ async def test_raw_usb_forwarding(vms): async def test_usb_forwarding(vms): - async with vms.hub.ssh_task("not-my-board hub", "hub"): - # wait for listening socket - await vms.hub.ssh_poll("nc -z 127.0.0.1 2092") - + async with vms.hub.ssh_task("not-my-board hub", "hub", wait_ready=True): async with vms.exporter.ssh_task_root( "not-my-board export http://hub.local:2092 ./src/tests/qemu-usb-place.toml", "export", + wait_ready=True, ): - await vms.client.ssh("""'doas rm -f "/run/not-my-board-agent.sock"'""") async with vms.client.ssh_task_root( - "not-my-board agent http://hub.local:2092", "agent" + "not-my-board agent http://hub.local:2092", "agent", wait_ready=True ): - # wait until exported place is registered - await vms.client.ssh_poll( - "wget -q -O - http://192.168.200.1:2092/api/v1/places | grep -q qemu-usb" - ) - # wait until agent is ready - await vms.client.ssh_poll( - """'test -e "/run/not-my-board-agent.sock"'""" - ) - await vms.client.ssh("not-my-board attach ./src/tests/qemu-usb.toml") # TODO attach still returns before the device is available. # would be nice if it blocks until the device is ready. diff --git a/tests/util.py b/tests/util.py index f9e2458..5b765e2 100644 --- a/tests/util.py +++ b/tests/util.py @@ -85,19 +85,22 @@ async def sh(cmd, check=True, strip=True, prefix=None): @contextlib.asynccontextmanager -async def sh_task(cmd, prefix=None, terminate=True): +async def sh_task(cmd, prefix=None, terminate=True, wait_ready=False): # need to exec, otherwise only the shell process is killed with # proc.terminate() proc = await asyncio.create_subprocess_shell( f"exec {cmd}", stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.STDOUT, + stderr=asyncio.subprocess.PIPE if wait_ready else asyncio.subprocess.STDOUT, ) logging_task = None + log_stream = proc.stderr if wait_ready else proc.stdout try: - logging_task = asyncio.create_task(_log_output(proc.stdout, cmd, prefix)) + logging_task = asyncio.create_task(_log_output(log_stream, cmd, prefix)) + if wait_ready: + await proc.stdout.readuntil(b"\n") yield finally: proc.stdin.close()