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.
diff --git a/lib/Test/Nginx.pm b/lib/Test/Nginx.pm index 9939793..362d3bd 100644 --- a/lib/Test/Nginx.pm +++ b/lib/Test/Nginx.pm
@@ -339,33 +339,42 @@ 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; + $port = int($port / 500) * 500 + int(rand(500)) unless $_ == 1; - $s_udp = IO::Socket::INET->new( - Proto => 'udp', - LocalAddr => '127.0.0.1:' . $port, - ) or next; - - $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: