Mercurial > hg > nginx-tests
annotate stream_udp_upstream_least_conn.t @ 1021:ebdf239722b9
Tests: relaxed proxy_cache_lock.t nolock tests to fix on Solaris.
Assumed that the order of responses being arrived in nolock case may be ignored.
An important part of such case is that the only last response should be cached.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Fri, 02 Sep 2016 12:17:35 +0300 |
parents | 196d33c2bb45 |
children | 3fc6817cd84a |
rev | line source |
---|---|
868 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Stream tests for upstream least_conn balancer module with datagrams. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
16 | |
17 use lib 'lib'; | |
18 use Test::Nginx; | |
19 use Test::Nginx::Stream qw/ dgram /; | |
20 | |
21 ############################################################################### | |
22 | |
23 select STDERR; $| = 1; | |
24 select STDOUT; $| = 1; | |
25 | |
947
b9e42c554ba7
Tests: skip UDP tests on win32, udp feature introduced.
Sergey Kandaurov <pluknet@nginx.com>
parents:
868
diff
changeset
|
26 my $t = Test::Nginx->new()->has(qw/stream stream_upstream_least_conn udp/) |
1020
196d33c2bb45
Tests: removed TODO and try_run() checks for legacy versions.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
27 ->plan(2)->write_file_expand('nginx.conf', <<'EOF'); |
868 | 28 |
29 %%TEST_GLOBALS%% | |
30 | |
31 daemon off; | |
32 | |
33 events { | |
34 } | |
35 | |
36 stream { | |
37 proxy_responses 1; | |
38 proxy_timeout 1s; | |
39 | |
40 upstream u { | |
41 least_conn; | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
42 server 127.0.0.1:%%PORT_8081_UDP%%; |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
43 server 127.0.0.1:%%PORT_8082_UDP%%; |
868 | 44 } |
45 | |
46 server { | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
47 listen 127.0.0.1:%%PORT_8080_UDP%% udp; |
868 | 48 proxy_pass u; |
49 } | |
50 } | |
51 | |
52 EOF | |
53 | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
54 $t->run_daemon(\&udp_daemon, port(8081), $t); |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
55 $t->run_daemon(\&udp_daemon, port(8082), $t); |
1020
196d33c2bb45
Tests: removed TODO and try_run() checks for legacy versions.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
56 $t->run(); |
868 | 57 |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
58 $t->waitforfile($t->testdir . '/' . port(8081)); |
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
59 $t->waitforfile($t->testdir . '/' . port(8082)); |
868 | 60 |
61 ############################################################################### | |
62 | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
63 my @ports = my ($port1, $port2) = (port(8081), port(8082)); |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
64 |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
65 is(many(10), "$port1: 5, $port2: 5", 'balanced'); |
868 | 66 |
67 my @sockets; | |
68 for (1 .. 2) { | |
69 my $s = dgram(); | |
70 $s->write('w'); | |
71 push @sockets, $s; | |
72 } | |
73 | |
74 select undef, undef, undef, 0.2; | |
75 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
76 is(many(10), "$port2: 10", 'least_conn'); |
868 | 77 |
78 ############################################################################### | |
79 | |
80 sub many { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
81 my ($count) = @_; |
868 | 82 my (%ports); |
83 | |
84 for (1 .. $count) { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
85 if (dgram()->io('.') =~ /(\d+)/) { |
868 | 86 $ports{$1} = 0 unless defined $ports{$1}; |
87 $ports{$1}++; | |
88 } | |
89 } | |
90 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
91 my @keys = map { my $p = $_; grep { $p == $_ } keys %ports } @ports; |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
92 return join ', ', map { $_ . ": " . $ports{$_} } @keys; |
868 | 93 } |
94 | |
95 ############################################################################### | |
96 | |
97 sub udp_daemon { | |
98 my ($port, $t) = @_; | |
99 | |
100 my $server = IO::Socket::INET->new( | |
101 Proto => 'udp', | |
102 LocalAddr => '127.0.0.1:' . $port, | |
103 Reuse => 1, | |
104 ) | |
105 or die "Can't create listening socket: $!\n"; | |
106 | |
107 # signal we are ready | |
108 | |
109 open my $fh, '>', $t->testdir() . '/' . $port; | |
110 close $fh; | |
111 | |
112 while (1) { | |
113 $server->recv(my $buffer, 65536); | |
114 | |
115 my $port = $server->sockport(); | |
116 | |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
117 if ($buffer =~ /w/ && $port == port(8081)) { |
868 | 118 select undef, undef, undef, 2.5; |
119 } | |
120 | |
121 $buffer = $port; | |
122 | |
123 $server->send($buffer); | |
124 } | |
125 } | |
126 | |
127 ############################################################################### |