Skip to content

Commit 9ac3bd2

Browse files
dcrankshawCorey-Zumar
authored andcommitted
Fix port already bound error in integration tests (#368)
* clean up socket and increase port range * format code * revert format change * add shutdown argument * Removed incorrect shutdown call * more tries * Catch Docker port-bound exception and start clipper in a retry loop * format code * Removed commented out code
1 parent 0fc0452 commit 9ac3bd2

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

integration-tests/test_utils.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __str__(self):
3232

3333

3434
# range of ports where available ports can be found
35-
PORT_RANGE = [34256, 40000]
35+
PORT_RANGE = [34256, 50000]
3636

3737

3838
def get_docker_client():
@@ -51,9 +51,12 @@ def find_unbound_port():
5151
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
5252
try:
5353
sock.bind(("127.0.0.1", port))
54+
# Make sure we clean up after binding
55+
del sock
5456
return port
55-
except socket.error:
56-
logger.debug(
57+
except socket.error as e:
58+
logger.info("Socket error: {}".format(e))
59+
logger.info(
5760
"randomly generated port %d is bound. Trying again." % port)
5861

5962

@@ -70,9 +73,24 @@ def create_docker_connection(cleanup=True, start_clipper=True):
7073
docker_client = get_docker_client()
7174
docker_client.containers.prune(filters={"label": CLIPPER_DOCKER_LABEL})
7275
if start_clipper:
73-
logging.info("Starting Clipper")
74-
cl.start_clipper()
75-
time.sleep(1)
76+
# Try to start Clipper in a retry loop here to address flaky tests
77+
# as described in https://github.com/ucbrise/clipper/issues/352
78+
while True:
79+
try:
80+
logging.info("Starting Clipper")
81+
cl.start_clipper()
82+
time.sleep(1)
83+
break
84+
except docker.errors.APIError as e:
85+
logging.info(
86+
"Problem starting Clipper: {}\nTrying again.".format(e))
87+
cl.stop_all()
88+
cm = DockerContainerManager(
89+
clipper_query_port=find_unbound_port(),
90+
clipper_management_port=find_unbound_port(),
91+
clipper_rpc_port=find_unbound_port(),
92+
redis_port=find_unbound_port())
93+
cl = ClipperConnection(cm)
7694
else:
7795
cl.connect()
7896
return cl

0 commit comments

Comments
 (0)