Skip to content

Commit a05766f

Browse files
committed
tests/multi_net: Add test that requires queuing UDP packets.
This commit adds a new network multi-test which sends a burst of UDP packets from the client, and the server doesn't recv them until they have all been sent. Signed-off-by: Damien George <damien@micropython.org>
1 parent 26e978e commit a05766f

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

tests/multi_net/udp_data_multi.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Test UDP reception when there are multiple incoming UDP packets that need to be
2+
# queued internally in the TCP/IP stack.
3+
4+
import socket
5+
6+
NUM_NEW_SOCKETS = 4
7+
NUM_PACKET_BURSTS = 6
8+
NUM_PACKET_GROUPS = 5
9+
TOTAL_PACKET_BURSTS = NUM_NEW_SOCKETS * NUM_PACKET_BURSTS
10+
# The tast passes if more than 75% of packets are received in each group.
11+
PACKET_RECV_THRESH = 0.75 * TOTAL_PACKET_BURSTS
12+
PORT = 8000
13+
14+
15+
# Server
16+
def instance0():
17+
recv_count = {i: 0 for i in range(NUM_PACKET_GROUPS)}
18+
multitest.globals(IP=multitest.get_network_ip())
19+
multitest.next()
20+
for i in range(NUM_NEW_SOCKETS):
21+
print("test socket", i)
22+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
23+
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
24+
s.bind(socket.getaddrinfo("0.0.0.0", PORT + i)[0][-1])
25+
s.settimeout(0.250)
26+
multitest.broadcast("server ready")
27+
for burst in range(NUM_PACKET_BURSTS):
28+
# Wait for all packets to be sent, without receiving any yet.
29+
multitest.wait("data sent burst={}".format(burst))
30+
# Try to receive all packets (they should be waiting in the queue).
31+
for group in range(NUM_PACKET_GROUPS):
32+
try:
33+
data, addr = s.recvfrom(1000)
34+
except:
35+
continue
36+
recv_burst, recv_group = data.split(b":")
37+
recv_burst = int(recv_burst)
38+
recv_group = int(recv_group)
39+
if recv_burst == burst:
40+
recv_count[recv_group] += 1
41+
# Inform the client that all data was received.
42+
multitest.broadcast("data received burst={}".format(burst))
43+
s.close()
44+
45+
# Check how many packets were received.
46+
for group, count in recv_count.items():
47+
if count >= PACKET_RECV_THRESH:
48+
print("pass group={}".format(group))
49+
else:
50+
print("fail group={} received={}%".format(group, 100 * count // TOTAL_PACKET_BURSTS))
51+
52+
53+
# Client
54+
def instance1():
55+
multitest.next()
56+
for i in range(NUM_NEW_SOCKETS):
57+
print("test socket", i)
58+
ai = socket.getaddrinfo(IP, PORT + i)[0][-1]
59+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
60+
multitest.wait("server ready")
61+
for burst in range(NUM_PACKET_BURSTS):
62+
# Send a bunch of packets all in a row.
63+
for group in range(NUM_PACKET_GROUPS):
64+
s.sendto(b"%d:%d" % (burst, group), ai)
65+
# Inform the server that the data has been sent.
66+
multitest.broadcast("data sent burst={}".format(burst))
67+
# Wait for the server to finish receiving.
68+
multitest.wait("data received burst={}".format(burst))
69+
s.close()

tests/multi_net/udp_data_multi.py.exp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--- instance0 ---
2+
test socket 0
3+
test socket 1
4+
test socket 2
5+
test socket 3
6+
pass group=0
7+
pass group=1
8+
pass group=2
9+
pass group=3
10+
pass group=4
11+
--- instance1 ---
12+
test socket 0
13+
test socket 1
14+
test socket 2
15+
test socket 3

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy