From 36e162f11602a23682dd482efbc69e0483924800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Z=C3=BCger?= Date: Fri, 3 Nov 2023 11:19:53 +0100 Subject: [PATCH] tests/net_hosted/asyncio_loopback.py: Add loopback test. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Peter Züger --- tests/net_hosted/asyncio_loopback.py | 64 ++++++++++++++++++++++++ tests/net_hosted/asyncio_loopback.py.exp | 14 ++++++ 2 files changed, 78 insertions(+) create mode 100644 tests/net_hosted/asyncio_loopback.py create mode 100644 tests/net_hosted/asyncio_loopback.py.exp diff --git a/tests/net_hosted/asyncio_loopback.py b/tests/net_hosted/asyncio_loopback.py new file mode 100644 index 0000000000..fd4674544c --- /dev/null +++ b/tests/net_hosted/asyncio_loopback.py @@ -0,0 +1,64 @@ +# Test network loopback behaviour + +try: + import asyncio +except ImportError: + print("SKIP") + raise SystemExit + + +async def client(host, port): + print(f"client open_connection to {host}:{port}") + reader, writer = await asyncio.open_connection(host, port) + + data_in = b"A" * 100 + + print("client writing") + writer.write(data_in) + await writer.drain() + + await asyncio.sleep(0.1) + + print("client reading") + data = await reader.readexactly(100) + print(f"client got {len(data)} bytes") + + assert data_in == data + + print("client closing") + + writer.close() + await writer.wait_closed() + + print("client closed") + + +async def echo_handler(reader, writer): + print("handler reading") + await asyncio.sleep(0.1) + data = await reader.readexactly(100) + print(f"handler got {len(data)} bytes") + + print("handler writing") + writer.write(data) + await writer.drain() + + print("handler closing") + + writer.close() + await writer.wait_closed() + + print("handler closed") + + +async def test(host, port): + print(f"create server on {host}:{port}") + server = await asyncio.start_server(echo_handler, host, port) + + async with server: + print("server started") + await client("127.0.0.1", 8080) + print("server closed") + + +asyncio.run(test("0.0.0.0", 8080)) diff --git a/tests/net_hosted/asyncio_loopback.py.exp b/tests/net_hosted/asyncio_loopback.py.exp new file mode 100644 index 0000000000..7110b35e12 --- /dev/null +++ b/tests/net_hosted/asyncio_loopback.py.exp @@ -0,0 +1,14 @@ +create server on 0.0.0.0:8080 +server started +client open_connection to 127.0.0.1:8080 +client writing +handler reading +client reading +handler got 100 bytes +handler writing +handler closing +handler closed +client got 100 bytes +client closing +client closed +server closed