changeset 1236:93f749c1d5c5

Tests: fixed parallel tests execution with UDP. Previously, when checking ports availability, a UDP socket was always created first, then a TCP socket was created. On success, one of UDP and TCP sockets was closed (depending on the "udp" option) and the second one was used to busy this port in other scripts. This lead to the following problem: in an attempt to reopen a UDP socket used in a given testing script it could be stolen by another script as part of checking ports availability. To solve this problem, UDP and TCP ports were split into two non-overlapping ranges: TCP ports are only used in the range 8000-8499, and UDP ports - in the range 8500-8999. In addition, the order of creating sockets in UDP tests has been reversed: now a TCP socket used as a lock precedes a UDP socket.
author Andrey Zelenkov <zelenkov@nginx.com>
date Thu, 26 Oct 2017 18:00:21 +0300
parents 3fc6817cd84a
children e4974af3fb12
files lib/Test/Nginx.pm
diffstat 1 files changed, 24 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/lib/Test/Nginx.pm
+++ b/lib/Test/Nginx.pm
@@ -339,33 +339,42 @@ sub run(;$) {
 
 sub port {
 	my ($num, %opts) = @_;
-	my ($s_tcp, $s_udp, $port);
+	my ($sock, $lock, $port);
 
 	goto done if defined $ports{$num};
 
+	my $socket = sub {
+		IO::Socket::INET->new(
+			Proto => 'tcp',
+			LocalAddr => '127.0.0.1:' . shift,
+			Listen => 1,
+			Reuse => ($^O ne 'MSWin32'),
+		);
+	};
+
+	my $socketl = sub {
+		IO::Socket::INET->new(
+			Proto => 'udp',
+			LocalAddr => '127.0.0.1:' . shift,
+		);
+	};
+
+	($socket, $socketl) = ($socketl, $socket) if $opts{udp};
+
 	$port = $num;
 
 	for (1 .. 10) {
-		$port = 8000 + int(rand(1000)) unless $_ == 1;
-
-		$s_udp = IO::Socket::INET->new(
-			Proto => 'udp',
-			LocalAddr => '127.0.0.1:' . $port,
-		) or next;
+		$port = int($port / 500) * 500 + int(rand(500)) unless $_ == 1;
 
-		$s_tcp = IO::Socket::INET->new(
-			Proto => 'tcp',
-			LocalAddr => '127.0.0.1:' . $port,
-			Listen => 1,
-			Reuse => ($^O ne 'MSWin32')
-		) and last;
+		$lock = $socketl->($port) or next;
+		$sock = $socket->($port) and last;
 	}
 
-	die "Port limit exceeded" unless defined $s_tcp and defined $s_udp;
+	die "Port limit exceeded" unless defined $lock and defined $sock;
 
 	$ports{$num} = {
 		port => $port,
-		socket => $opts{udp} ? $s_tcp : $s_udp
+		socket => $lock
 	};
 
 done: