-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
86 lines (73 loc) · 2.65 KB
/
Copy pathtest.py
File metadata and controls
86 lines (73 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from streamer import Streamer
import sys
import lossy_socket
NUMS=5000
def receive(s):
expected = 0
str_buf = ""
while expected < NUMS:
data = s.recv()
print("recv returned {%s}" % data.decode('utf-8'))
str_buf += data.decode('utf-8')
for t in str_buf.split(" "):
if len(t) == 0:
# there could be a "" at the start or the end, if a space is there
continue
if int(t) == expected:
print("got %d!" % expected)
expected += 1
str_buf = ''
elif int(t) > expected:
print("ERROR: got %s but was expecting %d" %(t, expected))
sys.exit(-1)
else:
# we only received the first part of the number at the end
# we must leave it in the buffer and read more.
str_buf = t
break
def host1(listen_port, remote_port):
s = Streamer(dst_ip="localhost", dst_port=remote_port,
src_ip="localhost", src_port=listen_port)
receive(s)
print("STAGE 1 TEST PASSED!")
# send large chunks of data
i = 0
buf = ""
while i < NUMS:
buf += ("%d " % i)
if len(buf) > 12345 or i == NUMS-1:
print("sending {%s}" % buf)
s.send(buf.encode('utf-8'))
buf = ""
i += 1
s.close()
print("CHECK THE OTHER SCRIPT FOR STAGE 2 RESULTS.")
def host2(listen_port, remote_port):
s = Streamer(dst_ip="localhost", dst_port=remote_port,
src_ip="localhost", src_port=listen_port)
# send small pieces of data
for i in range(NUMS):
buf = ("%d " % i)
print("sending {%s}" % buf)
s.send(buf.encode('utf-8'))
receive(s)
s.close()
print("STAGE 2 TEST PASSED!")
def main():
lossy_socket.sim = lossy_socket.SimulationParams(loss_rate=0.1, corruption_rate=0.1,
max_delivery_delay=0.1,
become_reliable_after=100000.0)
if len(sys.argv) < 4:
print("usage is: python3 test.py [port1] [port2] [1|2]")
print("First run with last argument set to 1, then with 2 (in two different terminals on the same machine")
sys.exit(-1)
port1 = int(sys.argv[1])
port2 = int(sys.argv[2])
if sys.argv[3] == "1":
host1(port1, port2)
elif sys.argv[3] == "2":
host2(port2, port1)
else:
print("Unexpected last argument: " + sys.argv[2])
if __name__ == "__main__":
main()