-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_server.py
241 lines (211 loc) · 6.93 KB
/
test_server.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import socket
import subprocess
import struct
import time
import sys
PORT = 25864
PROGRAM = "./server"
N_CLIENTS = 10
def recv_string(sock):
size_str = sock.recv(4)
size = struct.unpack("I", size_str)[0]
msg = sock.recv(size)
return msg
def send_string(sock, string):
sock.send(struct.pack("I", len(string)))
sock.send(string)
def recv_int(sock):
num_str = sock.recv(4)
num = struct.unpack("I", num_str)[0]
return num
def connect_socket():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', PORT))
sock.settimeout(1.0)
return sock
def start_server():
server = subprocess.Popen([PROGRAM, str(PORT)])
time.sleep(0.5)
if server.poll() == None:
return server
raise Exception("Nao deu pra subir o server")
class TestServer():
def setup_method(self, method):
self.server_process = start_server()
self.socket = connect_socket()
self.socket.send("C")
send_string(self.socket, "A")
def teardown_method(self, method):
self.socket.close()
self.server_process.kill()
self.server_process.wait()
time.sleep(1.0)
def test_unknown(self):
sock = self.socket
sock.send("P")
cmd = sock.recv(1)
assert "E" == cmd
msg = recv_string(sock)
assert msg == "Desconhecido"
def test_echo(self):
test_string = "This is a test string!"
self.socket.send("E")
send_string(self.socket, test_string)
assert "E" == self.socket.recv(1)
assert test_string == recv_string(self.socket)
def test_multiple_echo(self):
test_string = "This is a test string!"
self.socket.send("E")
send_string(self.socket, test_string)
assert "E" == self.socket.recv(1)
assert test_string == recv_string(self.socket)
test_string = "This is another test string!"
self.socket.send("E")
send_string(self.socket, test_string)
assert "E" == self.socket.recv(1)
assert test_string == recv_string(self.socket)
def test_username(self):
self.socket.send("C")
send_string(self.socket, "Armando")
def test_list(self):
self.socket.send("C")
send_string(self.socket, "Armando")
self.socket.send("L")
assert "L" == self.socket.recv(1)
assert 1 == recv_int(self.socket)
assert "Armando" == recv_string(self.socket)
def test_list_multiple(self):
self.socket.send("C")
send_string(self.socket, "Armando")
self.socket.send("L")
assert "L" == self.socket.recv(1)
assert 1 == recv_int(self.socket)
assert "Armando" == recv_string(self.socket)
self.socket.send("L")
assert "L" == self.socket.recv(1)
assert 1 == recv_int(self.socket)
assert "Armando" == recv_string(self.socket)
class TestServerWithMultipleClients():
def setup_method(self, method):
self.server_process = start_server()
self.sockets = list(connect_socket() for i in range(10))
for sock in self.sockets:
sock.send("C")
send_string(sock, "a")
def teardown_method(self, method):
for socket in self.sockets:
socket.close()
self.server_process.kill()
self.server_process.wait()
time.sleep(1.0)
def test_setup_and_teardown(self):
pass
def test_echo(self):
number = 0
for sock in self.sockets:
sock.send("E")
send_string(sock, "Test number %d." % number)
number += 1
number = 0
for sock in self.sockets:
cmd = sock.recv(1)
assert "E" == cmd
str_recv = recv_string(sock)
assert ("Test number %d." % number) == str_recv
number += 1
def test_multiple_echo(self):
self.test_echo()
self.test_echo()
def test_usernames(self):
usernames = list("client %d" % i for i in range(10))
for sock, username in zip(self.sockets, usernames):
# Connect
sock.send("C")
send_string(sock, username)
def test_list(self):
usernames = list("client %d" % i for i in range(10))
self.test_usernames()
time.sleep(1.0)
for sock in self.sockets:
sock.send("L")
cmd = sock.recv(1)
assert "L" == cmd
users = recv_int(sock)
assert 10 == users
names = list()
for i in range(10):
name = recv_string(sock)
names.append(name)
names.sort()
assert usernames == names
def test_quit(self):
self.test_usernames()
disconnected = self.sockets.pop(-1)
disconnected.send("Q")
disconnected.close()
time.sleep(0.5)
usernames = list("client %d" % i for i in range(9))
for sock in self.sockets:
sock.send("L")
cmd = sock.recv(1)
assert "L" == cmd
users = recv_int(sock)
assert 9 == users
names = list()
for i in range(9):
name = recv_string(sock)
names.append(name)
names.sort()
assert usernames == names
def test_disconnection(self):
self.test_usernames()
disconnected = self.sockets.pop(-1)
disconnected.close()
for i in range(2):
for sock in self.sockets:
sock.send("H")
time.sleep(4.0)
usernames = list("client %d" % i for i in range(9))
for sock in self.sockets:
sock.send("L")
cmd = sock.recv(1)
assert "L" == cmd
users = recv_int(sock)
assert 9 == users
names = list()
for i in range(9):
name = recv_string(sock)
names.append(name)
names.sort()
assert usernames == names
def test_message(self):
self.test_usernames()
message = "TEST MESSAGE"
sender = self.sockets.pop(0)
sender.send("M")
send_string(sender, message)
for sock in self.sockets:
cmd = sock.recv(1)
assert "M" == cmd
user = recv_string(sock)
assert "client 0" == user
message_recv = recv_string(sock)
assert message == message_recv
self.sockets.append(sender)
class TestCoordinator():
def setup_method(self, method):
self.server_process = start_server()
self.socket = connect_socket()
self.socket.send("S")
def teardown_method(self, method):
self.socket.close()
self.server_process.kill()
self.server_process.wait()
time.sleep(1.0)
def test_heartbeat(self):
sock = self.socket
sock.settimeout(5.0)
cmd = sock.recv(1)
assert cmd == 'S'
cmd = sock.recv(1)
assert cmd == 'S'