From a14fa5c51002d676f654945dbed6822336d17ff2 Mon Sep 17 00:00:00 2001 From: ninjaahhh <38544039+ninjaahhh@users.noreply.github.com> Date: Thu, 6 Aug 2020 13:38:36 -0700 Subject: [PATCH] Use system available ports in cluster test (#915) --- quarkchain/cluster/tests/test_utils.py | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/quarkchain/cluster/tests/test_utils.py b/quarkchain/cluster/tests/test_utils.py index af3b38e25..d52edf14f 100644 --- a/quarkchain/cluster/tests/test_utils.py +++ b/quarkchain/cluster/tests/test_utils.py @@ -1,6 +1,6 @@ import asyncio -import os -from contextlib import ContextDecorator +import socket +from contextlib import ContextDecorator, closing from quarkchain.cluster.cluster_config import ( ClusterConfig, @@ -288,21 +288,11 @@ def get_shard_state(self, full_shard_id: int) -> ShardState: return shard.state -# server.close() does not release the port sometimes even after server.wait_closed() is awaited. -# we have to use unique ports for each test as a workaround. -# also check if in CircleCI to avoid port collision -if "CIRCLE_NODE_INDEX" in os.environ: - # max parallelism is 4 - PORT_START = (int(os.environ["CIRCLE_NODE_INDEX"]) + 1) * 10000 -else: - PORT_START = 50000 - - def get_next_port(): - global PORT_START - port = PORT_START - PORT_START += 1 - return port + with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: + s.bind(("", 0)) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + return s.getsockname()[1] def create_test_clusters(