Skip to content

Commit e8cb0e3

Browse files
taimoorzaeemsteve-chavez
authored andcommitted
test(io): fix freeport function to prevent failures
Sometimes, a healthcheck related test fails as occurred in https://github.com/PostgREST/postgrest/actions/runs/19771357953/job/56655949002. This happens due to freeport function accidently picking up a used port. Signed-off-by: Taimoor Zaeem <taimoorzaeem@gmail.com> (cherry picked from commit 50eec77)
1 parent 05074f4 commit e8cb0e3

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

test/io/postgrest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def run(
113113
env["PGRST_SERVER_UNIX_SOCKET"] = str(socketfile)
114114
baseurl = "http+unix://" + urllib.parse.quote_plus(str(socketfile))
115115

116-
adminport = freeport(port)
116+
adminport = freeport(used_ports=[port])
117117
env["PGRST_ADMIN_SERVER_PORT"] = str(adminport)
118118
adminhost = f"[{host}]" if host and is_ipv6(host) else localhost
119119
adminurl = f"http://{adminhost}:{adminport}"
@@ -165,14 +165,14 @@ def run(
165165
process.wait()
166166

167167

168-
def freeport(used_port=None):
169-
"Find a free port on localhost."
168+
def freeport(used_ports=None):
169+
"Find an unused free port on localhost."
170170
while True:
171171
with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
172172
s.bind(("", 0))
173173
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
174174
port = s.getsockname()[1]
175-
if port != used_port:
175+
if used_ports is None or port not in used_ports:
176176
return port
177177

178178

test/io/test_cli.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ def test_jwt_secret_min_length(defaultenv):
286286
assert "The JWT secret must be at least 32 characters long." in error
287287

288288

289+
# TODO: Improve readability of "--ready" healthcheck tests
289290
@pytest.mark.parametrize("host", ["127.0.0.1", "::1"], ids=["IPv4", "IPv6"])
290291
def test_cli_ready_flag_success(host, defaultenv):
291292
"test PostgREST ready flag succeeds when ready"
@@ -340,8 +341,11 @@ def test_cli_ready_flag_fail_with_http_exception(defaultenv):
340341

341342
# when healthcheck process sends the request to a wrong endpoint
342343
with run(env=defaultenv, port=port) as postgrest:
343-
# we set it to some freeport where admin is not running
344-
postgrest.config["PGRST_ADMIN_SERVER_PORT"] = str(freeport())
344+
# we set it to some freeport where server and admin server is not running
345+
admin_port = int(postgrest.config["PGRST_ADMIN_SERVER_PORT"])
346+
used_ports = [port, admin_port]
347+
348+
postgrest.config["PGRST_ADMIN_SERVER_PORT"] = str(freeport(used_ports))
345349
output = cli(["--ready"], env=postgrest.config, expect_error=True)
346350
(admin_host, admin_port) = get_admin_host_and_port_from_config(postgrest.config)
347351

0 commit comments

Comments
 (0)