Skip to content

Commit

Permalink
Simplify port selection for Slurm/MongoDB containers
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Jan 12, 2024
1 parent 64117bc commit 324dbc9
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,11 @@
from docker.models.containers import Container


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_free_port():
"""Returns a random free port."""
sock = socket.socket()
sock.bind(("", 0))
return sock.getsockname()[1]


def _get_random_name(length=6):
Expand Down

0 comments on commit 324dbc9

Please sign in to comment.