Mercurial > hg > nginx-tests
annotate stream_udp_upstream.t @ 1427:eb1d883305ea
Tests: avoid edge cases in upstream random two test.
Unavailable servers contribute to the number of attempts, if selected,
before the balancer would fall back to the default round-robin method.
This means that it's quite possible to get server with more connections.
To facilitate with selecting two alive servers, down server was removed
from the upstream configuration at the cost of slightly worse coverage.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Thu, 10 Jan 2019 17:42:34 +0300 |
parents | 5bee71e1548f |
children | 144c6ce732e4 |
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 |
c4f58dfe8207
Tests: stream udp tests adjusted for upcoming udp streams.
Roman Arutyunyan <arut@nginx.com>
parents:
1251
diff
changeset
|
98 TODO: { |
c4f58dfe8207
Tests: stream udp tests adjusted for upcoming udp streams.
Roman Arutyunyan <arut@nginx.com>
parents:
1251
diff
changeset
|
99 local $TODO = 'not yet' unless $t->has_version('1.15.0'); |
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(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
|
102 '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
|
103 |
c4f58dfe8207
Tests: stream udp tests adjusted for upcoming udp streams.
Roman Arutyunyan <arut@nginx.com>
parents:
1251
diff
changeset
|
104 } |
c4f58dfe8207
Tests: stream udp tests adjusted for upcoming udp streams.
Roman Arutyunyan <arut@nginx.com>
parents:
1251
diff
changeset
|
105 |
1424
5bee71e1548f
Tests: adjusted stream udp test with failed peer.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
106 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
|
107 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
108 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
|
109 is(many(10, port(8983)), "$port4: 10", 'backup'); |
868 | 110 |
111 ############################################################################### | |
112 | |
113 sub many { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
114 my ($count, $port) = @_; |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
115 my (%ports); |
868 | 116 |
117 for (1 .. $count) { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
118 if (dgram("127.0.0.1:$port")->io('.') =~ /(\d+)/) { |
868 | 119 $ports{$1} = 0 unless defined $ports{$1}; |
120 $ports{$1}++; | |
121 } | |
122 } | |
123 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
124 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
|
125 return join ', ', map { $_ . ": " . $ports{$_} } @keys; |
868 | 126 } |
127 | |
128 ############################################################################### | |
129 | |
130 sub udp_daemon { | |
131 my ($port, $t) = @_; | |
132 | |
133 my $server = IO::Socket::INET->new( | |
134 Proto => 'udp', | |
135 LocalAddr => '127.0.0.1:' . $port, | |
136 Reuse => 1, | |
137 ) | |
138 or die "Can't create listening socket: $!\n"; | |
139 | |
140 # signal we are ready | |
141 | |
142 open my $fh, '>', $t->testdir() . '/' . $port; | |
143 close $fh; | |
144 | |
145 while (1) { | |
146 $server->recv(my $buffer, 65536); | |
147 $buffer = $server->sockport(); | |
148 $server->send($buffer); | |
149 } | |
150 } | |
151 | |
152 ############################################################################### |