Mercurial > hg > nginx-tests
annotate stream_udp_upstream_hash.t @ 1990:85d88cd5091c
Tests: SSI include stub tests.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 04 Jul 2024 17:42:55 +0300 |
parents | f3ba4c74de31 |
children |
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 { | |
1609
f3ba4c74de31
Tests: added TEST_GLOBALS_STREAM variable support.
Andrei Belov <defan@nginx.com>
parents:
1237
diff
changeset
|
38 %%TEST_GLOBALS_STREAM%% |
f3ba4c74de31
Tests: added TEST_GLOBALS_STREAM variable support.
Andrei Belov <defan@nginx.com>
parents:
1237
diff
changeset
|
39 |
868 | 40 proxy_responses 1; |
41 proxy_timeout 1s; | |
42 | |
43 upstream hash { | |
44 hash $remote_addr; | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
45 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
|
46 server 127.0.0.1:%%PORT_8983_UDP%%; |
868 | 47 } |
48 | |
49 upstream cons { | |
50 hash $remote_addr consistent; | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
51 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
|
52 server 127.0.0.1:%%PORT_8983_UDP%%; |
868 | 53 } |
54 | |
55 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
56 listen 127.0.0.1:%%PORT_8980_UDP%% udp; |
868 | 57 proxy_pass hash; |
58 } | |
59 | |
60 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
61 listen 127.0.0.1:%%PORT_8981_UDP%% udp; |
868 | 62 proxy_pass cons; |
63 } | |
64 } | |
65 | |
66 EOF | |
67 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
68 $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
|
69 $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
|
70 $t->run(); |
868 | 71 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
72 $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
|
73 $t->waitforfile($t->testdir . '/' . port(8983)); |
868 | 74 |
75 ############################################################################### | |
76 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
77 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
|
78 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1020
diff
changeset
|
79 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
|
80 like(many(10, port(8981)), qr/($port2|$port3): 10/, 'hash consistent'); |
868 | 81 |
82 ############################################################################### | |
83 | |
84 sub many { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
85 my ($count, $port) = @_; |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
86 my (%ports); |
868 | 87 |
88 for (1 .. $count) { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
89 if (dgram("127.0.0.1:$port")->io('.') =~ /(\d+)/) { |
868 | 90 $ports{$1} = 0 unless defined $ports{$1}; |
91 $ports{$1}++; | |
92 } | |
93 } | |
94 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
95 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
|
96 return join ', ', map { $_ . ": " . $ports{$_} } @keys; |
868 | 97 } |
98 | |
99 ############################################################################### | |
100 | |
101 sub udp_daemon { | |
102 my ($port, $t) = @_; | |
103 | |
104 my $server = IO::Socket::INET->new( | |
105 Proto => 'udp', | |
106 LocalAddr => '127.0.0.1:' . $port, | |
107 Reuse => 1, | |
108 ) | |
109 or die "Can't create listening socket: $!\n"; | |
110 | |
111 # signal we are ready | |
112 | |
113 open my $fh, '>', $t->testdir() . '/' . $port; | |
114 close $fh; | |
115 | |
116 while (1) { | |
117 $server->recv(my $buffer, 65536); | |
118 $buffer = $server->sockport(); | |
119 $server->send($buffer); | |
120 } | |
121 } | |
122 | |
123 ############################################################################### |