From daf9f69b6258e4ecdead322585728f5dee7b16d4 Mon Sep 17 00:00:00 2001 From: zixuanzh Date: Tue, 20 Nov 2018 23:25:49 -0500 Subject: [PATCH] raw_connection echo test --- tests/network/__init__.py | 0 tests/network/test_connection.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 tests/network/__init__.py create mode 100644 tests/network/test_connection.py diff --git a/tests/network/__init__.py b/tests/network/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/network/test_connection.py b/tests/network/test_connection.py new file mode 100644 index 0000000..64c1a91 --- /dev/null +++ b/tests/network/test_connection.py @@ -0,0 +1,29 @@ +import pytest +import asyncio + +from network.connection.raw_connection import RawConnection + + +async def handle_echo(reader, writer): + data = await reader.read(100) + message = data.decode() + + writer.write(data) + await writer.drain() + + writer.close() + +@pytest.mark.asyncio +async def test_echo(): + server_ip = '127.0.0.1' + server_port = 8888 + await asyncio.start_server(handle_echo, server_ip, server_port) + + reader, writer = await asyncio.open_connection(server_ip, server_port) + raw_connection = RawConnection(server_ip, server_port, reader, writer) + + test_message = "hello world" + writer.write(test_message.encode()) + response = (await reader.read()).decode() + + assert response == (test_message)