Skip to content

Commit aed8afb

Browse files
authored
tests: Change TEST_NGINX_SERVER_SSL_PORT* to use random ports.
Replace fixed ports with randomly assigned ports for TEST_NGINX_SERVER_SSL_PORT* variables. This prevents port conflicts when running tests in multiple processes.
1 parent f068a85 commit aed8afb

File tree

5 files changed

+168
-125
lines changed

5 files changed

+168
-125
lines changed

t/TestCore.pm

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package t::TestCore;
33
use Test::Nginx::Socket::Lua -Base;
44
use Cwd qw(cwd realpath abs_path);
55
use File::Basename;
6+
use Test::Nginx::Util 'is_tcp_port_used';
67

78
$ENV{TEST_NGINX_HOTLOOP} ||= 10;
89
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
9-
$ENV{TEST_NGINX_SERVER_SSL_PORT} ||= 23456;
1010
$ENV{TEST_NGINX_CERT_DIR} ||= dirname(realpath(abs_path(__FILE__)));
1111

12+
sub get_unused_port ($);
13+
14+
$ENV{TEST_NGINX_SERVER_SSL_PORT} ||= get_unused_port 23456;
15+
1216
our $pwd = cwd();
1317

1418
our $lua_package_path = './lib/?.lua;./t/lib/?.lua;../lua-resty-lrucache/lib/?.lua;;';
@@ -41,6 +45,7 @@ our @EXPORT = qw(
4145
$lua_package_path
4246
$init_by_lua_block
4347
$HttpConfig
48+
get_unused_port
4449
);
4550

4651
add_block_preprocessor(sub {
@@ -51,4 +56,20 @@ add_block_preprocessor(sub {
5156
}
5257
});
5358

59+
sub get_unused_port ($) {
60+
my $port = shift;
61+
62+
my $i = 1000;
63+
srand($$); # reset the random seed
64+
while ($i-- > 0) {
65+
my $rand_port = $port + int(rand(65535 - $port));
66+
if (!is_tcp_port_used $rand_port) {
67+
#warn "found unused port $rand_port, pid $$\n";
68+
return $rand_port;
69+
}
70+
}
71+
72+
die "no unused port available";
73+
}
74+
5475
1;

t/TestCore/Stream.pm

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package t::TestCore::Stream;
22

33
use Test::Nginx::Socket::Lua::Stream -Base;
44
use Cwd qw(cwd);
5+
use Test::Nginx::Util 'is_tcp_port_used';
56

67
$ENV{TEST_NGINX_HOTLOOP} ||= 10;
78

9+
sub get_unused_port ($);
10+
811
our $pwd = cwd();
912

1013
our $lua_package_path = './lib/?.lua;../lua-resty-lrucache/lib/?.lua;;';
@@ -37,6 +40,7 @@ our @EXPORT = qw(
3740
$lua_package_path
3841
$init_by_lua_block
3942
$StreamConfig
43+
get_unused_port
4044
);
4145

4246
add_block_preprocessor(sub {
@@ -47,4 +51,20 @@ add_block_preprocessor(sub {
4751
}
4852
});
4953

54+
sub get_unused_port ($) {
55+
my $port = shift;
56+
57+
my $i = 1000;
58+
srand($$); # reset the random seed
59+
while ($i-- > 0) {
60+
my $rand_port = $port + int(rand(65535 - $port));
61+
if (!is_tcp_port_used $rand_port) {
62+
#warn "found unused port $rand_port, pid $$\n";
63+
return $rand_port;
64+
}
65+
}
66+
67+
die "no unused port available";
68+
}
69+
5070
1;

t/balancer-keepalive-localaddr.t

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ __DATA__
133133
127.0.0.11
134134
127.0.0.11
135135
--- grep_error_log_out eval
136-
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
137-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
138-
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
139-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
136+
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
137+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
138+
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
139+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
140140
$/
141141
142142
@@ -184,12 +184,12 @@ $/
184184
127.0.0.11
185185
127.0.0.11
186186
--- grep_error_log_out eval
187-
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
188-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
189-
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
190-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
191-
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
192-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
187+
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
188+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
189+
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
190+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
191+
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
192+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
193193
$/
194194
195195
@@ -236,10 +236,10 @@ $/
236236
127.0.0.10
237237
127.0.0.11
238238
--- grep_error_log_out eval
239-
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
240-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
241-
lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
242-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
239+
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
240+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
241+
lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
242+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
243243
$/
244244
245245
@@ -290,14 +290,14 @@ $/
290290
127.0.0.11
291291
127.0.0.10
292292
--- grep_error_log_out eval
293-
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
294-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
295-
lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
296-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
297-
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
298-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
299-
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
300-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
293+
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
294+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
295+
lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
296+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
297+
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
298+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
299+
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
300+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
301301
$/
302302
303303
@@ -343,10 +343,10 @@ $/
343343
127.0.0.11
344344
127.0.0.11
345345
--- grep_error_log_out eval
346-
qr/^lua balancer: keepalive no free connection, host: 127.0.0.1:23456, name: test.com
347-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.1:23456, name: test.com
348-
lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
349-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
346+
qr/^lua balancer: keepalive no free connection, host: 127.0.0.1:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
347+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.1:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
348+
lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
349+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
350350
$/
351351
352352
@@ -392,10 +392,10 @@ $/
392392
127.0.0.11
393393
127.0.0.11
394394
--- grep_error_log_out eval
395-
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
396-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
397-
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
398-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
395+
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
396+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
397+
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
398+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
399399
$/
400400
401401
@@ -441,8 +441,8 @@ $/
441441
127.0.0.11:64321
442442
127.0.0.11:64321
443443
--- grep_error_log_out eval
444-
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:23456, name: test.com
445-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
446-
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
447-
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:23456, name: test.com
444+
qr/^lua balancer: keepalive no free connection, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
445+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
446+
lua balancer: keepalive reusing connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
447+
lua balancer: keepalive saving connection [0-9A-F]+, host: 127.0.0.2:$ENV{TEST_NGINX_SERVER_SSL_PORT}, name: test.com
448448
$/

0 commit comments

Comments
 (0)