Skip to content

Commit 600495a

Browse files
authored
Merge pull request #7 from ionut-arm/socket-poll
Make the mock service poll
2 parents 9bf0f4b + 641f774 commit 600495a

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

parsec_mock/parsec_mock.py

+31-8
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,41 @@ def run_test(test_folder, parsec_socket):
4141
sock.listen(1)
4242

4343
while True:
44+
print("Waiting for connections", flush=True)
4445
connection, client_addr = sock.accept()
4546
try:
47+
print("Connection received from {}".format(client_addr), flush=True)
48+
# Optionally reduce the default connection timeout
49+
# connection.settimeout(10)
4650
received_data = connection.recv(4096)
47-
b64_received_data = base64.b64encode(received_data).decode("ascii")
48-
if b64_received_data in test_cases:
49-
(name, test_case) = test_cases[b64_received_data]
50-
print("Received expected request for test case {}".format(name))
51-
bin_response = base64.b64decode(test_case.test_data.response)
52-
connection.sendall(bin_response)
53-
else:
54-
print("Received unexpected request {}".format(b64_received_data))
51+
all_received_data = bytearray()
52+
# Read the socket in cycle until we either
53+
# - receive a correct command or
54+
# - fail to receive any data
55+
while received_data:
56+
all_received_data += received_data
57+
b64_received_data = base64.b64encode(all_received_data).decode("ascii")
58+
if b64_received_data in test_cases:
59+
(name, test_case) = test_cases[b64_received_data]
60+
print(
61+
"Received expected request for test case {}".format(name),
62+
flush=True,
63+
)
64+
bin_response = base64.b64decode(test_case.test_data.response)
65+
connection.sendall(bin_response)
66+
break
67+
68+
print(
69+
"Received unexpected request {}".format(b64_received_data),
70+
flush=True,
71+
)
72+
print("Continue receiving", flush=True)
73+
received_data = connection.recv(4096)
74+
75+
if not received_data:
76+
print("No correct command received. No more data to read", flush=True)
5577
finally:
78+
print("Closing connection", flush=True)
5679
connection.close()
5780

5881

0 commit comments

Comments
 (0)