mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
refactor: migrate echo example test to use Trio for process handling
This commit is contained in:
@ -1,51 +1,65 @@
|
|||||||
import asyncio
|
|
||||||
import contextlib
|
import contextlib
|
||||||
import subprocess
|
|
||||||
import sys
|
import sys
|
||||||
import time
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
import trio
|
||||||
|
|
||||||
# This test is intentionally lightweight and can be marked as 'integration'.
|
# This test is intentionally lightweight and can be marked as 'integration'.
|
||||||
# It ensures the echo example runs and prints the new Thin Waist lines.
|
# It ensures the echo example runs and prints the new Thin Waist lines using Trio primitives.
|
||||||
|
|
||||||
EXAMPLES_DIR = Path(__file__).parent.parent.parent / "examples" / "echo"
|
EXAMPLES_DIR = Path(__file__).parent.parent.parent / "examples" / "echo"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.timeout(20)
|
@pytest.mark.trio
|
||||||
def test_echo_example_starts_and_prints_thin_waist(monkeypatch, tmp_path):
|
async def test_echo_example_starts_and_prints_thin_waist() -> None:
|
||||||
# We run: python examples/echo/echo.py -p 0
|
|
||||||
cmd = [sys.executable, str(EXAMPLES_DIR / "echo.py"), "-p", "0"]
|
cmd = [sys.executable, str(EXAMPLES_DIR / "echo.py"), "-p", "0"]
|
||||||
proc = subprocess.Popen(
|
|
||||||
cmd,
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
stderr=subprocess.STDOUT,
|
|
||||||
text=True,
|
|
||||||
)
|
|
||||||
assert proc.stdout is not None
|
|
||||||
|
|
||||||
found_selected = False
|
found_selected = False
|
||||||
found_interfaces = False
|
found_interfaces = False
|
||||||
start = time.time()
|
|
||||||
|
|
||||||
try:
|
# Use a cancellation scope as timeout (similar to previous 10s loop)
|
||||||
while time.time() - start < 10:
|
with trio.move_on_after(10) as cancel_scope:
|
||||||
line = proc.stdout.readline()
|
# Start process streaming stdout
|
||||||
if not line:
|
proc = await trio.open_process(
|
||||||
time.sleep(0.1)
|
cmd,
|
||||||
continue
|
stdout=trio.SUBPROCESS_PIPE,
|
||||||
if "Selected binding address:" in line:
|
stderr=trio.STDOUT,
|
||||||
found_selected = True
|
)
|
||||||
if "Available candidate interfaces:" in line:
|
|
||||||
found_interfaces = True
|
assert proc.stdout is not None # for type checkers
|
||||||
if "Waiting for incoming connections..." in line:
|
buffer = b""
|
||||||
break
|
|
||||||
finally:
|
try:
|
||||||
with contextlib.suppress(ProcessLookupError):
|
while not (found_selected and found_interfaces):
|
||||||
proc.terminate()
|
# Read some bytes (non-blocking with timeout scope)
|
||||||
with contextlib.suppress(ProcessLookupError):
|
data = await proc.stdout.receive_some(1024)
|
||||||
proc.kill()
|
if not data:
|
||||||
|
# Process might still be starting; yield control
|
||||||
|
await trio.sleep(0.05)
|
||||||
|
continue
|
||||||
|
buffer += data
|
||||||
|
# Process complete lines
|
||||||
|
*lines, buffer = buffer.split(b"\n") if b"\n" in buffer else ([], buffer)
|
||||||
|
for raw in lines:
|
||||||
|
line = raw.decode(errors="ignore")
|
||||||
|
if "Selected binding address:" in line:
|
||||||
|
found_selected = True
|
||||||
|
if "Available candidate interfaces:" in line:
|
||||||
|
found_interfaces = True
|
||||||
|
if "Waiting for incoming connections..." in line:
|
||||||
|
# We have reached steady state; can stop reading further
|
||||||
|
if found_selected and found_interfaces:
|
||||||
|
break
|
||||||
|
finally:
|
||||||
|
# Terminate the long-running echo example
|
||||||
|
with contextlib.suppress(Exception):
|
||||||
|
proc.terminate()
|
||||||
|
with contextlib.suppress(Exception):
|
||||||
|
await trio.move_on_after(2)(proc.wait) # best-effort wait
|
||||||
|
if cancel_scope.cancelled_caught:
|
||||||
|
# Timeout occurred
|
||||||
|
pass
|
||||||
|
|
||||||
assert found_selected, "Did not capture Thin Waist binding log line"
|
assert found_selected, "Did not capture Thin Waist binding log line"
|
||||||
assert found_interfaces, "Did not capture Thin Waist interfaces log line"
|
assert found_interfaces, "Did not capture Thin Waist interfaces log line"
|
||||||
Reference in New Issue
Block a user