|
| 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() |
0 commit comments