tests: make port picker more robust for tests
This commit is contained in:
parent
3af19c39da
commit
7b4a8e1ac5
1 changed files with 13 additions and 11 deletions
|
|
@ -21,7 +21,7 @@
|
|||
import json
|
||||
import platform
|
||||
import socket
|
||||
|
||||
import random
|
||||
import pytest
|
||||
|
||||
from rhodecode.lib.pyramid_utils import get_app_config
|
||||
|
|
@ -198,17 +198,19 @@ def ini_settings(ini_config):
|
|||
return get_app_config(ini_path)
|
||||
|
||||
|
||||
def get_available_port():
|
||||
family = socket.AF_INET
|
||||
socktype = socket.SOCK_STREAM
|
||||
host = '127.0.0.1'
|
||||
def get_available_port(min_port=40000, max_port=55555):
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
hostname = '127.0.0.1'
|
||||
|
||||
mysocket = socket.socket(family, socktype)
|
||||
mysocket.bind((host, 0))
|
||||
port = mysocket.getsockname()[1]
|
||||
mysocket.close()
|
||||
del mysocket
|
||||
return port
|
||||
for _ in range(min_port, max_port):
|
||||
pick_port = random.randint(min_port, max_port)
|
||||
try:
|
||||
sock.bind((hostname, pick_port))
|
||||
sock.close()
|
||||
del sock
|
||||
return pick_port
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue