changeset 969:1edb092149e2

Tests: simplified parallel tests. Now configurations are automatically scanned for "127.0.0.1:8xxx", and ports are auto-replaced by free ones. The exact port specified is used if it's free.
author Maxim Dounin <mdounin@mdounin.ru>
date Fri, 08 Jul 2016 02:21:14 +0300
parents ce687b25ea49
children c227348453db
files lib/Test/Nginx.pm lib/Test/Nginx/HTTP2.pm lib/Test/Nginx/IMAP.pm lib/Test/Nginx/POP3.pm lib/Test/Nginx/SMTP.pm lib/Test/Nginx/Stream.pm
diffstat 6 files changed, 24 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/lib/Test/Nginx.pm
+++ b/lib/Test/Nginx.pm
@@ -31,7 +31,7 @@ use Test::More qw//;
 
 our $NGINX = defined $ENV{TEST_NGINX_BINARY} ? $ENV{TEST_NGINX_BINARY}
 	: '../nginx/objs/nginx';
-our @ports = ();
+our %ports = ();
 
 sub new {
 	my $self = {};
@@ -313,10 +313,13 @@ sub port {
 	my ($num, %opts) = @_;
 	my ($s_tcp, $s_udp, $port);
 
-	goto done if defined $ports[$num];
+	$num += 8080 if $num < 1000;
+	goto done if defined $ports{$num};
+
+	$port = $num;
 
 	for (1 .. 10) {
-		$port = 8000 + int(rand(1000));
+		$port = 8000 + int(rand(1000)) unless $_ == 1;
 
 		$s_udp = IO::Socket::INET->new(
 			Proto => 'udp',
@@ -332,11 +335,14 @@ sub port {
 
 	die "Port limit exceeded" unless defined $s_tcp and defined $s_udp;
 
-	$ports[$num] = {port => $port, socket => $opts{udp} ? $s_tcp : $s_udp};
+	$ports{$num} = {
+		port => $port,
+		socket => $opts{udp} ? $s_tcp : $s_udp
+	};
 
 done:
-	return $ports[$num]{socket} if $opts{socket};
-	return $ports[$num]{port};
+	return $ports{$num}{socket} if $opts{socket};
+	return $ports{$num}{port};
 }
 
 sub dump_config() {
@@ -462,6 +468,9 @@ sub write_file_expand($$) {
 	$content =~ s/%%TEST_GLOBALS%%/$self->test_globals()/gmse;
 	$content =~ s/%%TEST_GLOBALS_HTTP%%/$self->test_globals_http()/gmse;
 	$content =~ s/%%TESTDIR%%/$self->{_testdir}/gms;
+
+	$content =~ s/127\.0\.0\.1:(8\d\d\d)/'127.0.0.1:' . port($1)/gmse;
+
 	$content =~ s/%%PORT_(\d+)%%/port($1)/gmse;
 	$content =~ s/%%PORT_(\d+)_UDP%%/port($1, udp => 1)/gmse;
 
@@ -645,7 +654,7 @@ sub http_start($;%) {
 
 		$s = $extra{socket} || IO::Socket::INET->new(
 			Proto => 'tcp',
-			PeerAddr => '127.0.0.1:' . ($ports[0]{port} || 8080)
+			PeerAddr => '127.0.0.1:' . port(8080)
 		)
 			or die "Can't connect to nginx: $!\n";
 
--- a/lib/Test/Nginx/HTTP2.pm
+++ b/lib/Test/Nginx/HTTP2.pm
@@ -479,7 +479,7 @@ sub new_socket {
 	my $alpn = $extra{'alpn'};
 	my $s;
 
-	$port = ($Test::Nginx::ports[0]{port} || 8080) unless defined $port;
+	$port ||= port(8080);
 
 	eval {
 		local $SIG{ALRM} = sub { die "timeout\n" };
--- a/lib/Test/Nginx/IMAP.pm
+++ b/lib/Test/Nginx/IMAP.pm
@@ -21,7 +21,7 @@ sub new {
 
 	$self->{_socket} = IO::Socket::INET->new(
 		Proto => "tcp",
-		PeerAddr => "127.0.0.1:8143",
+		PeerAddr => "127.0.0.1:" . port(8143),
 		@_
 	)
 		or die "Can't connect to nginx: $!\n";
@@ -93,7 +93,7 @@ sub imap_test_daemon {
 
 	my $server = IO::Socket::INET->new(
 		Proto => 'tcp',
-		LocalAddr => '127.0.0.1:' . ($port || 8144),
+		LocalAddr => '127.0.0.1:' . ($port || port(8144)),
 		Listen => 5,
 		Reuse => 1
 	)
--- a/lib/Test/Nginx/POP3.pm
+++ b/lib/Test/Nginx/POP3.pm
@@ -21,7 +21,7 @@ sub new {
 
 	$self->{_socket} = IO::Socket::INET->new(
 		Proto => "tcp",
-		PeerAddr => "127.0.0.1:8110",
+		PeerAddr => "127.0.0.1:" . port(8110),
 		@_
 	)
 		or die "Can't connect to nginx: $!\n";
@@ -93,7 +93,7 @@ sub pop3_test_daemon {
 
 	my $server = IO::Socket::INET->new(
 		Proto => 'tcp',
-		LocalAddr => '127.0.0.1:' . ($port || 8111),
+		LocalAddr => '127.0.0.1:' . ($port || port(8111)),
 		Listen => 5,
 		Reuse => 1
 	)
--- a/lib/Test/Nginx/SMTP.pm
+++ b/lib/Test/Nginx/SMTP.pm
@@ -21,7 +21,7 @@ sub new {
 
 	$self->{_socket} = IO::Socket::INET->new(
 		Proto => "tcp",
-		PeerAddr => "127.0.0.1:8025",
+		PeerAddr => "127.0.0.1:" . port(8025),
 		@_
 	)
 		or die "Can't connect to nginx: $!\n";
@@ -97,7 +97,7 @@ sub smtp_test_daemon {
 
 	my $server = IO::Socket::INET->new(
 		Proto => 'tcp',
-		LocalAddr => '127.0.0.1:' . ($port || 8026),
+		LocalAddr => '127.0.0.1:' . ($port || port(8026)),
 		Listen => 5,
 		Reuse => 1
 	)
--- a/lib/Test/Nginx/Stream.pm
+++ b/lib/Test/Nginx/Stream.pm
@@ -41,7 +41,7 @@ sub new {
 	$self->{_socket} = IO::Socket::INET->new(
 		Proto => "tcp",
 		PeerAddr => '127.0.0.1',
-		PeerPort => ($Test::Nginx::ports[0]{port} || 8080),
+		PeerPort => port(8080),
 		@_
 	)
 		or die "Can't connect to nginx: $!\n";