diff stream_upstream_least_conn.t @ 952:e9064d691790

Tests: converted tests to run in parallel.
author Andrey Zelenkov <zelenkov@nginx.com>
date Tue, 21 Jun 2016 16:39:13 +0300
parents 77359b849cd5
children 882267679006
line wrap: on
line diff
--- a/stream_upstream_least_conn.t
+++ b/stream_upstream_least_conn.t
@@ -38,28 +38,30 @@ events {
 stream {
     upstream u {
         least_conn;
-        server 127.0.0.1:8081;
-        server 127.0.0.1:8082;
+        server 127.0.0.1:%%PORT_1%%;
+        server 127.0.0.1:%%PORT_2%%;
     }
 
     server {
-        listen      127.0.0.1:8080;
+        listen      127.0.0.1:%%PORT_0%%;
         proxy_pass  u;
     }
 }
 
 EOF
 
-$t->run_daemon(\&stream_daemon, 8081);
-$t->run_daemon(\&stream_daemon, 8082);
+$t->run_daemon(\&stream_daemon, port(1));
+$t->run_daemon(\&stream_daemon, port(2));
 $t->run();
 
-$t->waitforsocket('127.0.0.1:8081');
-$t->waitforsocket('127.0.0.1:8082');
+$t->waitforsocket('127.0.0.1:' . port(1));
+$t->waitforsocket('127.0.0.1:' . port(2));
 
 ###############################################################################
 
-is(many('.', 10), '8081: 5, 8082: 5', 'balanced');
+my @ports = my ($port1, $port2) = (port(1), port(2));
+
+is(many(10), "$port1: 5, $port2: 5", 'balanced');
 
 my @sockets;
 for (1 .. 2) {
@@ -70,22 +72,23 @@ for (1 .. 2) {
 
 select undef, undef, undef, 0.2;
 
-is(many('.', 10), '8082: 10', 'least_conn');
+is(many(10), "$port2: 10", 'least_conn');
 
 ###############################################################################
 
 sub many {
-	my ($data, $count, %opts) = @_;
+	my ($count) = @_;
 	my (%ports);
 
 	for (1 .. $count) {
-		if (stream()->io($data) =~ /(\d+)/) {
+		if (stream()->io('.') =~ /(\d+)/) {
 			$ports{$1} = 0 unless defined $ports{$1};
 			$ports{$1}++;
 		}
 	}
 
-	return join ', ', map { $_ . ": " . $ports{$_} } sort keys %ports;
+	my @keys = map { my $p = $_; grep { $p == $_ } keys %ports } @ports;
+	return join ', ', map { $_ . ": " . $ports{$_} } @keys;
 }
 
 ###############################################################################
@@ -132,7 +135,7 @@ sub stream_handle_client {
 
 	my $port = $client->sockport();
 
-	if ($buffer =~ /w/ && $port == 8081) {
+	if ($buffer =~ /w/ && $port == port(1)) {
 		Test::Nginx::log_core('||', "$port: sleep(2.5)");
 		select undef, undef, undef, 2.5;
 	}