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.
This commit is contained in:
Damien George 2020-03-10 02:59:14 +11:00
parent 18fa65e474
commit 38904b8937
6 changed files with 206 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,5 @@
--- instance0 ---
b'GET / HTTP'
OSError 104
--- instance1 ---

View File

@ -0,0 +1,69 @@
# Test uasyncio TCP stream closing then writing
try:
import uasyncio as asyncio
except ImportError:
try:
import asyncio
except ImportError:
print("SKIP")
raise SystemExit
PORT = 8000
async def handle_connection(reader, writer):
# Write data to ensure connection
writer.write(b"x")
await writer.drain()
# Read, should return nothing
print("read:", await reader.read(100))
# Close connection
print("close")
writer.close()
await writer.wait_closed()
print("done")
ev.set()
async def tcp_server():
global ev
ev = asyncio.Event()
server = await asyncio.start_server(handle_connection, "0.0.0.0", PORT)
print("server running")
multitest.next()
async with server:
await asyncio.wait_for(ev.wait(), 5)
async def tcp_client():
reader, writer = await asyncio.open_connection(IP, PORT)
# Read data to ensure connection
print("read:", await reader.read(1))
# Close connection
print("close")
writer.close()
await writer.wait_closed()
# Try writing data to the closed connection
print("write")
try:
writer.write(b"x")
await writer.drain()
except OSError:
print("OSError")
def instance0():
multitest.globals(IP=multitest.get_network_ip())
asyncio.run(tcp_server())
def instance1():
multitest.next()
asyncio.run(tcp_client())

View File

@ -0,0 +1,10 @@
--- instance0 ---
server running
read: b''
close
done
--- instance1 ---
read: b'x'
close
write
OSError

View File

@ -0,0 +1,58 @@
# Test uasyncio TCP server and client using start_server() and open_connection()
try:
import uasyncio as asyncio
except ImportError:
try:
import asyncio
except ImportError:
print("SKIP")
raise SystemExit
PORT = 8000
async def handle_connection(reader, writer):
# Test that peername exists (but don't check its value, it changes)
writer.get_extra_info("peername")
data = await reader.read(100)
print("echo:", data)
writer.write(data)
await writer.drain()
print("close")
writer.close()
await writer.wait_closed()
print("done")
ev.set()
async def tcp_server():
global ev
ev = asyncio.Event()
server = await asyncio.start_server(handle_connection, "0.0.0.0", PORT)
print("server running")
multitest.next()
async with server:
await asyncio.wait_for(ev.wait(), 10)
async def tcp_client(message):
reader, writer = await asyncio.open_connection(IP, PORT)
print("write:", message)
writer.write(message)
await writer.drain()
data = await reader.read(100)
print("read:", data)
def instance0():
multitest.globals(IP=multitest.get_network_ip())
asyncio.run(tcp_server())
def instance1():
multitest.next()
asyncio.run(tcp_client(b"client data"))

View File

@ -0,0 +1,8 @@
--- instance0 ---
server running
echo: b'client data'
close
done
--- instance1 ---
write: b'client data'
read: b'client data'