diff options
author | Peter Züger <zueger.peter@icloud.com> | 2023-11-03 11:19:53 +0100 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2023-11-17 13:37:43 +1100 |
commit | 36e162f11602a23682dd482efbc69e0483924800 (patch) | |
tree | c9bfed6daf636aeae1484d9ea6f3ac94a6676b97 /tests/net_hosted/asyncio_loopback.py | |
parent | a23dbdca7944e03a990f04f5aede0c1fb93d6919 (diff) |
tests/net_hosted/asyncio_loopback.py: Add loopback test.
Signed-off-by: Peter Züger <zueger.peter@icloud.com>
Diffstat (limited to 'tests/net_hosted/asyncio_loopback.py')
-rw-r--r-- | tests/net_hosted/asyncio_loopback.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/tests/net_hosted/asyncio_loopback.py b/tests/net_hosted/asyncio_loopback.py new file mode 100644 index 000000000..fd4674544 --- /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)) |