Mercurial > hg > nginx-tests
annotate stream_udp_upstream.t @ 1606:e4e0695552ed
Tests: fixed stream_proxy_ssl_conf_command.t.
The stream_proxy_ssl_conf_command.t test used stream return module
to return the response. Since this ignores actual request, but the
perl test code used http_get(). This might result in the request being
sent after the response is returned and the connection closed by the server,
resulting in RST being generated and no response seen by the client at all.
Fix is to use "stream(...)->read()" instead of http_get(), so
no request is sent at all, eliminating possibility of RST being
generated.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Tue, 10 Nov 2020 05:03:29 +0300 |
parents | 144c6ce732e4 |
children | f3ba4c74de31 |
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 | |
1424
5bee71e1548f
Tests: adjusted stream udp test with failed peer.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
26 my $t = Test::Nginx->new()->has(qw/stream udp/)->plan(5) |
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 | |
40 upstream u { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
41 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
|
42 server 127.0.0.1:%%PORT_8985_UDP%%; |
868 | 43 } |
44 | |
45 upstream u2 { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
46 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
|
47 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
|
48 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
|
49 server 127.0.0.1:%%PORT_8985_UDP%%; |
868 | 50 } |
51 | |
52 upstream u3 { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
53 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
|
54 server 127.0.0.1:%%PORT_8985_UDP%% weight=2; |
868 | 55 } |
56 | |
57 upstream u4 { | |
1334
c4f58dfe8207
Tests: stream udp tests adjusted for upcoming udp streams.
Roman Arutyunyan <arut@nginx.com>
parents:
1251
diff
changeset
|
58 server 127.0.0.1:%%PORT_8986_UDP%% down; |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
59 server 127.0.0.1:%%PORT_8984_UDP%% backup; |
868 | 60 } |
61 | |
62 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
63 listen 127.0.0.1:%%PORT_8980_UDP%% udp; |
868 | 64 proxy_pass u; |
65 } | |
66 | |
67 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
68 listen 127.0.0.1:%%PORT_8981_UDP%% udp; |
868 | 69 proxy_pass u2; |
70 } | |
71 | |
72 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
73 listen 127.0.0.1:%%PORT_8982_UDP%% udp; |
868 | 74 proxy_pass u3; |
75 } | |
76 | |
77 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
78 listen 127.0.0.1:%%PORT_8983_UDP%% udp; |
868 | 79 proxy_pass u4; |
80 } | |
81 } | |
82 | |
83 EOF | |
84 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
85 $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
|
86 $t->run_daemon(\&udp_daemon, port(8985), $t); |
1334
c4f58dfe8207
Tests: stream udp tests adjusted for upcoming udp streams.
Roman Arutyunyan <arut@nginx.com>
parents:
1251
diff
changeset
|
87 $t->run(); |
868 | 88 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
89 $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
|
90 $t->waitforfile($t->testdir . '/' . port(8985)); |
868 | 91 |
92 ############################################################################### | |
93 | |
1334
c4f58dfe8207
Tests: stream udp tests adjusted for upcoming udp streams.
Roman Arutyunyan <arut@nginx.com>
parents:
1251
diff
changeset
|
94 my @ports = my ($port4, $port5) = (port(8984), port(8985)); |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
95 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
96 is(many(10, port(8980)), "$port4: 5, $port5: 5", 'balanced'); |
1334
c4f58dfe8207
Tests: stream udp tests adjusted for upcoming udp streams.
Roman Arutyunyan <arut@nginx.com>
parents:
1251
diff
changeset
|
97 |
1424
5bee71e1548f
Tests: adjusted stream udp test with failed peer.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
98 is(dgram('127.0.0.1:' . port(8981))->io('.', read_timeout => 0.5), '', |
5bee71e1548f
Tests: adjusted stream udp test with failed peer.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
99 'no next upstream for dgram'); |
1334
c4f58dfe8207
Tests: stream udp tests adjusted for upcoming udp streams.
Roman Arutyunyan <arut@nginx.com>
parents:
1251
diff
changeset
|
100 |
1424
5bee71e1548f
Tests: adjusted stream udp test with failed peer.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
101 is(many(10, port(8981)), "$port4: 5, $port5: 5", 'failures'); |
5bee71e1548f
Tests: adjusted stream udp test with failed peer.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
102 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
103 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
|
104 is(many(10, port(8983)), "$port4: 10", 'backup'); |
868 | 105 |
106 ############################################################################### | |
107 | |
108 sub many { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
109 my ($count, $port) = @_; |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
110 my (%ports); |
868 | 111 |
112 for (1 .. $count) { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
113 if (dgram("127.0.0.1:$port")->io('.') =~ /(\d+)/) { |
868 | 114 $ports{$1} = 0 unless defined $ports{$1}; |
115 $ports{$1}++; | |
116 } | |
117 } | |
118 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
119 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
|
120 return join ', ', map { $_ . ": " . $ports{$_} } @keys; |
868 | 121 } |
122 | |
123 ############################################################################### | |
124 | |
125 sub udp_daemon { | |
126 my ($port, $t) = @_; | |
127 | |
128 my $server = IO::Socket::INET->new( | |
129 Proto => 'udp', | |
130 LocalAddr => '127.0.0.1:' . $port, | |
131 Reuse => 1, | |
132 ) | |
133 or die "Can't create listening socket: $!\n"; | |
134 | |
135 # signal we are ready | |
136 | |
137 open my $fh, '>', $t->testdir() . '/' . $port; | |
138 close $fh; | |
139 | |
140 while (1) { | |
141 $server->recv(my $buffer, 65536); | |
142 $buffer = $server->sockport(); | |
143 $server->send($buffer); | |
144 } | |
145 } | |
146 | |
147 ############################################################################### |