Mercurial > hg > nginx-tests
annotate stream_udp_upstream_hash.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 | e4974af3fb12 |
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 hash 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 | |
1020
196d33c2bb45
Tests: removed TODO and try_run() checks for legacy versions.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
26 my $t = Test::Nginx->new()->has(qw/stream stream_upstream_hash udp/)->plan(2); |
868 | 27 |
28 $t->write_file_expand('nginx.conf', <<'EOF'); | |
29 | |
30 %%TEST_GLOBALS%% | |
31 | |
32 daemon off; | |
33 | |
34 events { | |
35 } | |
36 | |
37 stream { | |
38 proxy_responses 1; | |
39 proxy_timeout 1s; | |
40 | |
41 upstream hash { | |
42 hash $remote_addr; | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
43 server 127.0.0.1:%%PORT_8982_UDP%%; |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
44 server 127.0.0.1:%%PORT_8983_UDP%%; |
868 | 45 } |
46 | |
47 upstream cons { | |
48 hash $remote_addr consistent; | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
49 server 127.0.0.1:%%PORT_8982_UDP%%; |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
50 server 127.0.0.1:%%PORT_8983_UDP%%; |
868 | 51 } |
52 | |
53 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
54 listen 127.0.0.1:%%PORT_8980_UDP%% udp; |
868 | 55 proxy_pass hash; |
56 } | |
57 | |
58 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
59 listen 127.0.0.1:%%PORT_8981_UDP%% udp; |
868 | 60 proxy_pass cons; |
61 } | |
62 } | |
63 | |
64 EOF | |
65 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
66 $t->run_daemon(\&udp_daemon, port(8982), $t); |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
67 $t->run_daemon(\&udp_daemon, port(8983), $t); |
1020
196d33c2bb45
Tests: removed TODO and try_run() checks for legacy versions.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
68 $t->run(); |
868 | 69 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
70 $t->waitforfile($t->testdir . '/' . port(8982)); |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
71 $t->waitforfile($t->testdir . '/' . port(8983)); |
868 | 72 |
73 ############################################################################### | |
74 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
75 my @ports = my ($port2, $port3) = (port(8982), port(8983)); |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
76 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
77 is(many(10, port(8980)), "$port3: 10", 'hash'); |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
78 like(many(10, port(8981)), qr/($port2|$port3): 10/, 'hash consistent'); |
868 | 79 |
80 ############################################################################### | |
81 | |
82 sub many { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
83 my ($count, $port) = @_; |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
84 my (%ports); |
868 | 85 |
86 for (1 .. $count) { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
87 if (dgram("127.0.0.1:$port")->io('.') =~ /(\d+)/) { |
868 | 88 $ports{$1} = 0 unless defined $ports{$1}; |
89 $ports{$1}++; | |
90 } | |
91 } | |
92 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
93 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
|
94 return join ', ', map { $_ . ": " . $ports{$_} } @keys; |
868 | 95 } |
96 | |
97 ############################################################################### | |
98 | |
99 sub udp_daemon { | |
100 my ($port, $t) = @_; | |
101 | |
102 my $server = IO::Socket::INET->new( | |
103 Proto => 'udp', | |
104 LocalAddr => '127.0.0.1:' . $port, | |
105 Reuse => 1, | |
106 ) | |
107 or die "Can't create listening socket: $!\n"; | |
108 | |
109 # signal we are ready | |
110 | |
111 open my $fh, '>', $t->testdir() . '/' . $port; | |
112 close $fh; | |
113 | |
114 while (1) { | |
115 $server->recv(my $buffer, 65536); | |
116 $buffer = $server->sockport(); | |
117 $server->send($buffer); | |
118 } | |
119 } | |
120 | |
121 ############################################################################### |