From e47400c00aecf09a0ac35ca6db180759f537885d Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Fri, 19 Jan 2024 11:19:35 -0500 Subject: [PATCH] Make port busy test more robust --- tests/pytest/test_utils.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/pytest/test_utils.py b/tests/pytest/test_utils.py index 122b306aa..e4a69d03b 100644 --- a/tests/pytest/test_utils.py +++ b/tests/pytest/test_utils.py @@ -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: @@ -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)