Mercurial > hg > nginx-tests
annotate stream_udp_upstream.t @ 1263:ea3c7659b6c1
Tests: handled early pidfile write on win32 in the run() routine.
In addition to the present waiting for pidfile, which is insufficient on win32
due to the CreateProcess model, and may lead to rare startup races, search now
for the certain error message which indicates started worker process.
This change allows tolerating moderate hiccups on win32 hosts.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Tue, 19 Dec 2017 19:55:01 +0300 |
parents | 766bcbb632ee |
children | c4f58dfe8207 |
rev | line source |
---|---|
868 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Stream tests for upstream module and balancers 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 | |
1026
e74a9342eb96
Tests: stream tests for per-upstream variables.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
26 my $t = Test::Nginx->new()->has(qw/stream udp/) |
868 | 27 ->write_file_expand('nginx.conf', <<'EOF'); |
28 | |
29 %%TEST_GLOBALS%% | |
30 | |
31 daemon off; | |
32 | |
33 events { | |
34 } | |
35 | |
36 stream { | |
37 proxy_responses 1; | |
38 proxy_timeout 1s; | |
39 | |
1026
e74a9342eb96
Tests: stream tests for per-upstream variables.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
40 log_format bytes $upstream_addr! |
e74a9342eb96
Tests: stream tests for per-upstream variables.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
41 $upstream_bytes_sent!$upstream_bytes_received; |
e74a9342eb96
Tests: stream tests for per-upstream variables.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
42 |
868 | 43 upstream u { |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
44 server 127.0.0.1:%%PORT_8984_UDP%%; |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
45 server 127.0.0.1:%%PORT_8985_UDP%%; |
868 | 46 } |
47 | |
48 upstream u2 { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
49 server 127.0.0.1:%%PORT_8986_UDP%% down; |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
50 server 127.0.0.1:%%PORT_8986_UDP%%; |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
51 server 127.0.0.1:%%PORT_8984_UDP%%; |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
52 server 127.0.0.1:%%PORT_8985_UDP%%; |
868 | 53 } |
54 | |
55 upstream u3 { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
56 server 127.0.0.1:%%PORT_8984_UDP%%; |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
57 server 127.0.0.1:%%PORT_8985_UDP%% weight=2; |
868 | 58 } |
59 | |
60 upstream u4 { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
61 server 127.0.0.1:%%PORT_8986_UDP%%; |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
62 server 127.0.0.1:%%PORT_8984_UDP%% backup; |
868 | 63 } |
64 | |
65 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
66 listen 127.0.0.1:%%PORT_8980_UDP%% udp; |
868 | 67 proxy_pass u; |
68 } | |
69 | |
70 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
71 listen 127.0.0.1:%%PORT_8981_UDP%% udp; |
868 | 72 proxy_pass u2; |
73 } | |
74 | |
75 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
76 listen 127.0.0.1:%%PORT_8982_UDP%% udp; |
868 | 77 proxy_pass u3; |
78 } | |
79 | |
80 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
81 listen 127.0.0.1:%%PORT_8983_UDP%% udp; |
868 | 82 proxy_pass u4; |
1026
e74a9342eb96
Tests: stream tests for per-upstream variables.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
83 access_log %%TESTDIR%%/u.log bytes; |
868 | 84 } |
85 } | |
86 | |
87 EOF | |
88 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
89 $t->run_daemon(\&udp_daemon, port(8984), $t); |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
90 $t->run_daemon(\&udp_daemon, port(8985), $t); |
1251
766bcbb632ee
Tests: removed TODO and try_run() checks for legacy versions.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1237
diff
changeset
|
91 $t->run()->plan(5); |
868 | 92 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
93 $t->waitforfile($t->testdir . '/' . port(8984)); |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
94 $t->waitforfile($t->testdir . '/' . port(8985)); |
868 | 95 |
96 ############################################################################### | |
97 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
98 my @ports = my ($port4, $port5, $port6) = (port(8984), port(8985), port(8986)); |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
99 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
100 is(many(10, port(8980)), "$port4: 5, $port5: 5", 'balanced'); |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
101 is(many(10, port(8981)), "$port4: 5, $port5: 5", 'failures'); |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
102 is(many(9, port(8982)), "$port4: 3, $port5: 6", 'weight'); |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
103 is(many(10, port(8983)), "$port4: 10", 'backup'); |
868 | 104 |
1026
e74a9342eb96
Tests: stream tests for per-upstream variables.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
105 $t->stop(); |
e74a9342eb96
Tests: stream tests for per-upstream variables.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
106 |
e74a9342eb96
Tests: stream tests for per-upstream variables.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
107 like($t->read_file('u.log'), qr/127.0.0.1:$port6, 127.0.0.1:$port4!1, 1!0, 4/, |
e74a9342eb96
Tests: stream tests for per-upstream variables.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
108 'per-upstream variables'); |
e74a9342eb96
Tests: stream tests for per-upstream variables.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1020
diff
changeset
|
109 |
868 | 110 ############################################################################### |
111 | |
112 sub many { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
113 my ($count, $port) = @_; |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
114 my (%ports); |
868 | 115 |
116 for (1 .. $count) { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
117 if (dgram("127.0.0.1:$port")->io('.') =~ /(\d+)/) { |
868 | 118 $ports{$1} = 0 unless defined $ports{$1}; |
119 $ports{$1}++; | |
120 } | |
121 } | |
122 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
123 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
|
124 return join ', ', map { $_ . ": " . $ports{$_} } @keys; |
868 | 125 } |
126 | |
127 ############################################################################### | |
128 | |
129 sub udp_daemon { | |
130 my ($port, $t) = @_; | |
131 | |
132 my $server = IO::Socket::INET->new( | |
133 Proto => 'udp', | |
134 LocalAddr => '127.0.0.1:' . $port, | |
135 Reuse => 1, | |
136 ) | |
137 or die "Can't create listening socket: $!\n"; | |
138 | |
139 # signal we are ready | |
140 | |
141 open my $fh, '>', $t->testdir() . '/' . $port; | |
142 close $fh; | |
143 | |
144 while (1) { | |
145 $server->recv(my $buffer, 65536); | |
146 $buffer = $server->sockport(); | |
147 $server->send($buffer); | |
148 } | |
149 } | |
150 | |
151 ############################################################################### |