Skip to content

Commit

Permalink
tests: Use Ready Notification Instead of Polling
Browse files Browse the repository at this point in the history
Wait for the services to print a line to stdout, instead of waiting for
the port to be opened.
  • Loading branch information
holesch committed Aug 15, 2024
1 parent 6365f34 commit 4738b0a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 30 deletions.
11 changes: 3 additions & 8 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
)
Expand Down Expand Up @@ -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": []}
Expand Down
23 changes: 4 additions & 19 deletions tests/test_usbip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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.
Expand Down
9 changes: 6 additions & 3 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 4738b0a

Please sign in to comment.