Commit 3e7b1fff authored by Rafael Monnerat's avatar Rafael Monnerat 👻

caucasexx: update to findFreeTCPPortRange

parent 46f48451
Pipeline #40091 failed with stage
in 0 seconds
......@@ -29,17 +29,14 @@
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.utils import parseListeningAddress
from six import BytesIO
import socket
import time
import os
import shutil
import contextlib
import multiprocessing
import requests
import tarfile
from random import randint
import random
import contextlib
import caucase.cli
import caucase.http
......@@ -54,13 +51,27 @@ def canConnect(caucase_url):
return False
return True
def findFreeTCPPort(ip=''):
"""Find a free TCP port to listen to.
def findFreeTCPPortRange(ip='', count=1):
# type: (str, int) -> int
"""Find a range of consecutive `count` free TCP ports to listen to.
"""
family = socket.AF_INET6 if ':' in ip else socket.AF_INET
with contextlib.closing(socket.socket(family, socket.SOCK_STREAM)) as s:
s.bind((ip, 0))
return str(s.getsockname()[1])
for _ in range(10): # retry 10 times
port = random.randrange(20000, 30000)
for offset in range(count):
with contextlib.closing(socket.socket(
socket.AF_INET6 if ':' in ip else socket.AF_INET, socket.SOCK_STREAM)) as s:
try:
s.bind((ip, port + offset))
except socket.error:
# Keep compatibility with python2.7
port = None
break
except OSError:
port = None
break
if port is None:
raise RuntimeError("Can't find port")
return port
def retry(callback, try_count=10, try_delay=0.1):
"""
......@@ -89,7 +100,7 @@ class ERP5TypeCaucaseTestCase(ERP5TypeTestCase):
if _ip is not None:
ip = _ip
break
port = findFreeTCPPort(ip)
port = findFreeTCPPortRange(ip)
self.caucase_runtime = caucase_runtime = multiprocessing.Process(
target=caucase.http.main,
kwargs=dict(
......@@ -164,7 +175,7 @@ class ERP5TypeCaucaseTestCase(ERP5TypeTestCase):
# The server fail to start, probably another process
# running in parallel picked the port, so sleep a random
# moment to increase the chance of success
time.sleep(randint(1,3))
time.sleep(random.randint(1,3))
self._startCaucaseServer()
self.addCleanup(self._stopCaucaseServer)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment