Skip to content

Commit

Permalink
Make port busy test more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke committed Jan 19, 2024
1 parent fbd8912 commit e47400c
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions tests/pytest/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def test_random_port():
# Port `port + j` is busy,
# Shift the test range and try again
port += j + 1
print(port)
print("Trying port: ", port)
# If no port is available, throw an error
# `attempts` should be << n
if attempts == n:
Expand All @@ -191,6 +191,19 @@ def test_random_port_unusable():


def test_random_port_starvation():
with socketserver.TCPServer(("127.0.0.1", 9000), socketserver.BaseRequestHandler):
with pytest.raises(RuntimeError, match="Failed to find a usable random port"):
random_port(9000, 9000)
port = 9000
for _ in range(100):
try:
with socketserver.TCPServer(
("127.0.0.1", port),
socketserver.BaseRequestHandler,
):
with pytest.raises(
RuntimeError, match="Failed to find a usable random port"
):
random_port(port, port)
except OSError as e:
print(e)
# Port is busy, bump the port number
port += 1
print("Trying port: ", port)

0 comments on commit e47400c

Please sign in to comment.