Skip to content

test: Use random ports instead of fixed ones - #356

Open
ChrisTX wants to merge 1 commit into
ledgetech:masterfrom
ChrisTX:random-test-ports
Open

ChrisTX wants to merge 1 commit into
ledgetech:masterfrom
ChrisTX:random-test-ports

Conversation

@ChrisTX

@ChrisTX ChrisTX commented Nov 20, 2026

Copy link
Copy Markdown

Some tests start servers on fixed ports: listen *:8080 and *:8081 in t/14-host-header.t and t/16-http-proxy.t, and --- tcp_listen: 12345 in t/01-basic.t, t/07-keepalive.t, t/15-instance-reuse.t and t/16-http-proxy.t. On a host where another service holds one of these ports, nginx can't start and the suite stops:

t/14-host-header.t TEST 3: Non-default HTTP port is added to Host header - Cannot start nginx using command "nginx … -c …/t/servroot/conf/nginx.conf > /dev/null" (status code 256).

That host was a server with CrowdSec, whose API listens on 8080 by default.

This PR uses Test::Nginx's $TEST_NGINX_RAND_PORT_1 and $TEST_NGINX_RAND_PORT_2 instead. Test::Nginx expands them in the configuration, --- tcp_listen and --- response_body, giving each test block free ports. The 12345 in t/03-requestbody.t is a request body, not a port, so it stays.

Test::Nginx 0.32 doesn't expand these variables in --- error_log. So TESTs 3, 4 and 6 of t/14-host-header.t, which checked the Host header in the debug log, now have the server (or, in TEST 6, the proxy) echo the Host header it received. They compare it in --- response_body, as t/16-http-proxy.t already does with ngx.req.raw_header(). This checks the header as it arrived rather than as it was logged. They still fail if the port goes missing from the Host header: with lib/resty/http.lua changed to send only self.host, all three report got: '127.0.0.1', expected: '127.0.0.1:<port>'. (openresty/test-nginx#191 proposes the expansion in --- error_log, but this PR doesn't need it.)

Tested with nginx 1.30.7 and lua-nginx-module 0.10.32rc8, with another process listening on 8080:

  • serially (prove -r t), Test::Nginx 0.32: all 21 files and 434 tests pass. Without this change, the run stops at t/14-host-header.t.
  • in parallel (TEST_NGINX_RANDOMIZE=1 prove -j32 -r t), five times each with Test::Nginx 0.32 and with its current master: all pass.
  • two copies of the suite at the same time (-j16 each), eight times with Test::Nginx's current master: all pass. With 0.32, a random port occasionally collided with the other copy's: random ports in 0.32 aren't coordinated across processes. That affects the server port of TEST_NGINX_RANDOMIZE just the same, and Test::Nginx's master has fixed it.

🤖 Generated with Claude Code

The tests started servers on the fixed ports 8080, 8081 and 12345, so
the suite could not run on a host where another service holds one of
them, e.g. 8080. Test::Nginx's $TEST_NGINX_RAND_PORT_N variables give
each test block free ports instead.

Test::Nginx doesn't expand these variables in --- error_log, so the
Host header tests of t/14-host-header.t now have the server (or the
proxy) echo the Host header it received and compare it in
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant