summaryrefslogtreecommitdiffstatshomepage
path: root/tests/multi_net/uasyncio_tcp_client_rst.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2020-03-10 02:59:14 +1100
committerDamien George <damien.p.george@gmail.com>2020-03-26 01:25:45 +1100
commit38904b89376bf628f7d70174204b5330618d49c0 (patch)
treea3aa131c9eb196405aba019d643d102b26954de6 /tests/multi_net/uasyncio_tcp_client_rst.py
parent18fa65e47402d85c53361798046e306d2cf4bac1 (diff)
downloadmicropython-38904b89376bf628f7d70174204b5330618d49c0.tar.gz
micropython-38904b89376bf628f7d70174204b5330618d49c0.zip
tests/multi_net: Add uasyncio test for TCP server and client.
Includes a test where the (non uasyncio) client does a RST on the connection, as a simple TCP server/client test where both sides are using uasyncio, and a test for TCP stream close then write.
Diffstat (limited to 'tests/multi_net/uasyncio_tcp_client_rst.py')
-rw-r--r--tests/multi_net/uasyncio_tcp_client_rst.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/multi_net/uasyncio_tcp_client_rst.py b/tests/multi_net/uasyncio_tcp_client_rst.py
new file mode 100644
index 0000000000..a3a05490c7
--- /dev/null
+++ b/tests/multi_net/uasyncio_tcp_client_rst.py
@@ -0,0 +1,56 @@
+# Test TCP server with client issuing TCP RST part way through read
+
+try:
+ import uasyncio as asyncio
+except ImportError:
+ try:
+ import asyncio
+ except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+import struct, time, socket
+
+PORT = 8000
+
+
+async def handle_connection(reader, writer):
+ data = await reader.read(10) # should succeed
+ print(data)
+ await asyncio.sleep(0.2) # wait for client to drop connection
+ try:
+ data = await reader.read(100)
+ print(data)
+ writer.close()
+ await writer.wait_closed()
+ except OSError as er:
+ print("OSError", er.args[0])
+ ev.set()
+
+
+async def main():
+ global ev
+ ev = asyncio.Event()
+ server = await asyncio.start_server(handle_connection, "0.0.0.0", PORT)
+ multitest.next()
+ async with server:
+ await asyncio.wait_for(ev.wait(), 10)
+
+
+def instance0():
+ multitest.globals(IP=multitest.get_network_ip())
+ asyncio.run(main())
+
+
+def instance1():
+ if not hasattr(socket, "SO_LINGER"):
+ multitest.skip()
+ multitest.next()
+ s = socket.socket()
+ s.connect(socket.getaddrinfo(IP, PORT)[0][-1])
+ lgr_onoff = 1
+ lgr_linger = 0
+ s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", lgr_onoff, lgr_linger))
+ s.send(b"GET / HTTP/1.0\r\n\r\n")
+ time.sleep(0.1)
+ s.close() # This issues a TCP RST since we've set the linger option