tests: improve search of a free TCP port (#72645)
This commit is contained in:
parent
842f699e8a
commit
b3520030f5
|
@ -16,6 +16,9 @@
|
|||
|
||||
import hashlib
|
||||
import os
|
||||
import socket
|
||||
import struct
|
||||
from contextlib import closing
|
||||
|
||||
|
||||
def get_safe_db_name(max_length=53):
|
||||
|
@ -47,3 +50,12 @@ def get_safe_db_name(max_length=53):
|
|||
truncated_db_name = full_db_name[:prefix_length] + '_' + hashcode + '_' + full_db_name[-suffix_length:]
|
||||
assert len(truncated_db_name) == max_length
|
||||
return truncated_db_name
|
||||
|
||||
|
||||
def find_free_port():
|
||||
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
|
||||
s.bind(('', 0))
|
||||
# SO_LINGER (man 7 socket) l_onoff=1 l_linger=0, immediately release
|
||||
# the port on closing of the socket
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0))
|
||||
return s.getsockname()[1]
|
||||
|
|
|
@ -16,17 +16,12 @@ from test_manager import login
|
|||
|
||||
from hobo.emails.validators import validate_email_address
|
||||
from hobo.environment.models import Variable
|
||||
from hobo.test_utils import find_free_port
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def port_available():
|
||||
errno = 0
|
||||
while not errno:
|
||||
port = random.randint(49152, 65534)
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
errno = sock.connect_ex(('127.0.0.1', port))
|
||||
sock.close()
|
||||
return port
|
||||
return find_free_port()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
Loading…
Reference in New Issue