Skip to content

Commit

Permalink
tests: Change TEST_NGINX_SERVER_SSL_PORT* to use random ports.
Browse files Browse the repository at this point in the history
Replace fixed ports with randomly assigned ports for TEST_NGINX_SERVER_SSL_PORT*
variables. This prevents port conflicts when running tests in multiple processes.
  • Loading branch information
Johnny Wang authored Jul 29, 2024
1 parent f068a85 commit aed8afb
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 125 deletions.
23 changes: 22 additions & 1 deletion t/TestCore.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package t::TestCore;
use Test::Nginx::Socket::Lua -Base;
use Cwd qw(cwd realpath abs_path);
use File::Basename;
use Test::Nginx::Util 'is_tcp_port_used';

$ENV{TEST_NGINX_HOTLOOP} ||= 10;
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
$ENV{TEST_NGINX_SERVER_SSL_PORT} ||= 23456;
$ENV{TEST_NGINX_CERT_DIR} ||= dirname(realpath(abs_path(__FILE__)));

sub get_unused_port ($);

$ENV{TEST_NGINX_SERVER_SSL_PORT} ||= get_unused_port 23456;

our $pwd = cwd();

our $lua_package_path = './lib/?.lua;./t/lib/?.lua;../lua-resty-lrucache/lib/?.lua;;';
Expand Down Expand Up @@ -41,6 +45,7 @@ our @EXPORT = qw(
$lua_package_path
$init_by_lua_block
$HttpConfig
get_unused_port
);

add_block_preprocessor(sub {
Expand All @@ -51,4 +56,20 @@ add_block_preprocessor(sub {
}
});

sub get_unused_port ($) {
my $port = shift;

my $i = 1000;
srand($$); # reset the random seed
while ($i-- > 0) {
my $rand_port = $port + int(rand(65535 - $port));
if (!is_tcp_port_used $rand_port) {
#warn "found unused port $rand_port, pid $$\n";
return $rand_port;
}
}

die "no unused port available";
}

1;
20 changes: 20 additions & 0 deletions t/TestCore/Stream.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package t::TestCore::Stream;

use Test::Nginx::Socket::Lua::Stream -Base;
use Cwd qw(cwd);
use Test::Nginx::Util 'is_tcp_port_used';

$ENV{TEST_NGINX_HOTLOOP} ||= 10;

sub get_unused_port ($);

our $pwd = cwd();

our $lua_package_path = './lib/?.lua;../lua-resty-lrucache/lib/?.lua;;';
Expand Down Expand Up @@ -37,6 +40,7 @@ our @EXPORT = qw(
$lua_package_path
$init_by_lua_block
$StreamConfig
get_unused_port
);

add_block_preprocessor(sub {
Expand All @@ -47,4 +51,20 @@ add_block_preprocessor(sub {
}
});

sub get_unused_port ($) {
my $port = shift;

my $i = 1000;
srand($$); # reset the random seed
while ($i-- > 0) {
my $rand_port = $port + int(rand(65535 - $port));
if (!is_tcp_port_used $rand_port) {
#warn "found unused port $rand_port, pid $$\n";
return $rand_port;
}
}

die "no unused port available";
}

1;
68 changes: 34 additions & 34 deletions t/balancer-keepalive-localaddr.t
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ __DATA__
127.0.0.11
127.0.0.11
--- grep_error_log_out eval
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
$/


Expand Down Expand Up @@ -184,12 +184,12 @@ $/
127.0.0.11
127.0.0.11
--- grep_error_log_out eval
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
$/


Expand Down Expand Up @@ -236,10 +236,10 @@ $/
127.0.0.10
127.0.0.11
--- grep_error_log_out eval
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
$/


Expand Down Expand Up @@ -290,14 +290,14 @@ $/
127.0.0.11
127.0.0.10
--- grep_error_log_out eval
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
$/


Expand Down Expand Up @@ -343,10 +343,10 @@ $/
127.0.0.11
127.0.0.11
--- grep_error_log_out eval
qr/^lua balancer: keepalive no free connection, host: 127.0.0.1:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.1:23456, name: test.com
lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
qr/^lua balancer: keepalive no free connection, host: 127.0.0.1:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.1:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
$/


Expand Down Expand Up @@ -392,10 +392,10 @@ $/
127.0.0.11
127.0.0.11
--- grep_error_log_out eval
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
$/


Expand Down Expand Up @@ -441,8 +441,8 @@ $/
127.0.0.11:64321
127.0.0.11:64321
--- grep_error_log_out eval
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
$/
Loading

0 comments on commit aed8afb

Please sign in to comment.