Skip to content

Commit bba59a6

Browse files
committed
Make the mock service poll
This commit makes the mock service retry reading from connections until it no longer finds any data. Signed-off-by: Ionut Mihalcea <[email protected]>
1 parent 9bf0f4b commit bba59a6

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

parsec_mock/parsec_mock.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import socket
66
import os
7+
import sys
78
from os import listdir
89
from os.path import isfile, join
910

@@ -44,7 +45,18 @@ def run_test(test_folder, parsec_socket):
4445
connection, client_addr = sock.accept()
4546
try:
4647
received_data = connection.recv(4096)
47-
b64_received_data = base64.b64encode(received_data).decode("ascii")
48+
all_received_data = received_data
49+
# Keep polling the connection until there's no more to read
50+
# for 0.5s.
51+
connection.settimeout(0.5)
52+
while True:
53+
try:
54+
received_data = connection.recv(4096)
55+
all_received_data += received_data
56+
except socket.timeout:
57+
print("Finished reading from socket")
58+
break
59+
b64_received_data = base64.b64encode(all_received_data).decode("ascii")
4860
if b64_received_data in test_cases:
4961
(name, test_case) = test_cases[b64_received_data]
5062
print("Received expected request for test case {}".format(name))
@@ -53,6 +65,7 @@ def run_test(test_folder, parsec_socket):
5365
else:
5466
print("Received unexpected request {}".format(b64_received_data))
5567
finally:
68+
sys.stdout.flush()
5669
connection.close()
5770

5871

@@ -75,7 +88,8 @@ def _traverse(key, element):
7588
def load_tests_from_folder(test_folder):
7689
tests = {}
7790
"""Read test specs from a folder"""
78-
specfiles = [f for f in listdir(test_folder) if isfile(join(test_folder, f))]
91+
specfiles = [f for f in listdir(
92+
test_folder) if isfile(join(test_folder, f))]
7993

8094
for file in specfiles:
8195
print(f"Parsing spec file: {file}")

0 commit comments

Comments
 (0)