diff --git a/test/io/postgrest.py b/test/io/postgrest.py index efea3ef4f3..74f40ac75d 100644 --- a/test/io/postgrest.py +++ b/test/io/postgrest.py @@ -113,7 +113,7 @@ def run( env["PGRST_SERVER_UNIX_SOCKET"] = str(socketfile) baseurl = "http+unix://" + urllib.parse.quote_plus(str(socketfile)) - adminport = freeport(port) + adminport = freeport(used_ports=[port]) env["PGRST_ADMIN_SERVER_PORT"] = str(adminport) adminhost = f"[{host}]" if host and is_ipv6(host) else localhost adminurl = f"http://{adminhost}:{adminport}" @@ -165,14 +165,14 @@ def run( process.wait() -def freeport(used_port=None): - "Find a free port on localhost." +def freeport(used_ports=None): + "Find an unused free port on localhost." while True: with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: s.bind(("", 0)) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) port = s.getsockname()[1] - if port != used_port: + if used_ports is None or port not in used_ports: return port diff --git a/test/io/test_cli.py b/test/io/test_cli.py index 0f46e4c0b0..df33547fe5 100644 --- a/test/io/test_cli.py +++ b/test/io/test_cli.py @@ -286,6 +286,7 @@ def test_jwt_secret_min_length(defaultenv): assert "The JWT secret must be at least 32 characters long." in error +# TODO: Improve readability of "--ready" healthcheck tests @pytest.mark.parametrize("host", ["127.0.0.1", "::1"], ids=["IPv4", "IPv6"]) def test_cli_ready_flag_success(host, defaultenv): "test PostgREST ready flag succeeds when ready" @@ -340,8 +341,11 @@ def test_cli_ready_flag_fail_with_http_exception(defaultenv): # when healthcheck process sends the request to a wrong endpoint with run(env=defaultenv, port=port) as postgrest: - # we set it to some freeport where admin is not running - postgrest.config["PGRST_ADMIN_SERVER_PORT"] = str(freeport()) + # we set it to some freeport where server and admin server is not running + admin_port = int(postgrest.config["PGRST_ADMIN_SERVER_PORT"]) + used_ports = [port, admin_port] + + postgrest.config["PGRST_ADMIN_SERVER_PORT"] = str(freeport(used_ports)) output = cli(["--ready"], env=postgrest.config, expect_error=True) (admin_host, admin_port) = get_admin_host_and_port_from_config(postgrest.config)