10000 tests/multi_net/udp_data.py: Make UDP test more reliable. · iabdalkader/micropython@30a0225 · GitHub
[go: up one dir, main page]

Skip to content

Commit 30a0225

Browse files
iabdalkaderdpgeorge
authored andcommitted
tests/multi_net/udp_data.py: Make UDP test more reliable.
The current test depends on a specific number and order of packets to pass, which can't be reproduced every run due to the unreliable UDP protocol. This patch adds simple packets sequencing, retransmits with timeouts, and a packet loss threshold, to make the test more tolerant to UDP protocol packet drops and reordering.
1 parent aca4012 commit 30a0225

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

tests/multi_net/udp_data.py

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,68 @@
33
import socket
44

55
NUM_NEW_SOCKETS = 4
6-
NUM_TRANSFERS = 4
6+
NUM_TRANSFERS = 10
7+
TOTAL_PACKETS = NUM_NEW_SOCKETS * NUM_TRANSFERS
8+
# If more than 75% of packets are lost, the test fails.
9+
PACKET_LOSS_THRESH = 0.75 * TOTAL_PACKETS
710
PORT = 8000
811

12+
13+
def print_stats(seq):
14+
if (TOTAL_PACKETS - seq) > PACKET_LOSS_THRESH:
15+
print(
16+
"packet loss %.1f%% %d/%d"
17+
% (((TOTAL_PACKETS - seq) / TOTAL_PACKETS * 100), seq, TOTAL_PACKETS)
18+
)
19+
else:
20+
print("pass")
21+
22+
923
# Server
1024
def instance0():
25+
seq = 0
1126
multitest.globals(IP=multitest.get_network_ip())
1227
multitest.next()
1328
for i in range(NUM_NEW_SOCKETS):
1429
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
1530
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
16-
s.bind(socket.getaddrinfo("0.0.0.0", PORT)[0][-1])
31+
s.bind(socket.getaddrinfo("0.0.0.0", PORT + i)[0][-1])
32+
s.settimeout(0.250)
1733
multitest.broadcast("server ready")
1834
for j in range(NUM_TRANSFERS):
19-
data, addr = s.recvfrom(1000)
20-
print(data)
21-
s.sendto(b"server to client %d %d" % (i, j), addr)
35+
try:
36+
data, addr = s.recvfrom(1000)
37+
except:
38+
continue
39+
if int(data) == seq:
40+
if seq < (TOTAL_PACKETS - PACKET_LOSS_THRESH):
41+
print(seq)
42+
seq += 1
43+
s.sendto(b"%d" % (seq), addr)
2244
s.close()
2345

46+
print_stats(seq)
47+
2448

2549
# Client
2650
def instance1():
51+
seq = 0
2752
multitest.next()
28-
ai = socket.getaddrinfo(IP, PORT)[0][-1]
2953
for i in range(NUM_NEW_SOCKETS):
54+
ai = socket.getaddrinfo(IP, PORT + i)[0][-1]
3055
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
56+
s.settimeout(0.250)
3157
multitest.wait("server ready")
3258
for j in range(NUM_TRANSFERS):
33-
s.sendto(b"client to server %d %d" % (i, j), ai)
34-
data, addr = s.recvfrom(1000)
35-
print(data)
59+
s.sendto(b"%d" % (seq), ai)
60+
try:
61+
data, addr = s.recvfrom(1000)
62+
except:
63+
continue
64+
if int(data) == seq + 1:
65+
if seq < (TOTAL_PACKETS - PACKET_LOSS_THRESH):
66+
print(seq)
67+
seq += 1
3668
s.close()
69+
70+
print_stats(seq)

0 commit comments

Comments
 (0)
0