Skip to content

Commit

Permalink
Protect random port selection against flaky docker port authorisation…
Browse files Browse the repository at this point in the history
… failures
  • Loading branch information
ml-evs committed Oct 22, 2023
1 parent 9df23d1 commit 2f88e60
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,26 @@
import pytest


def _get_free_port():
sock = socket.socket()
sock.bind(("", 0))
return sock.getsockname()[1]
def _get_free_port(upper_bound=50_000):
"""Returns a random free port, with an upper bound.
The upper bound is required as Docker does not have
permissions on high port numbers on some systems.
"""
port = upper_bound + 1
attempts = 0
max_attempts = 10
while port > upper_bound and attempts < max_attempts:
sock = socket.socket()
sock.bind(("", 0))
port = sock.getsockname()[1]
attempts += 1

if attempts == max_attempts:
raise RuntimeError(f"Could not find a free port to use with the provided {upper_bound=}.")

return port


def _get_random_name(length=6):
Expand Down

0 comments on commit 2f88e60

Please sign in to comment.