Mercurial > hg > nginx-tests
annotate stream_udp_upstream.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 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 { | |
1609
f3ba4c74de31
Tests: added TEST_GLOBALS_STREAM variable support.
Andrei Belov <defan@nginx.com>
parents:
1535
diff
changeset
|
37 %%TEST_GLOBALS_STREAM%% |
f3ba4c74de31
Tests: added TEST_GLOBALS_STREAM variable support.
Andrei Belov <defan@nginx.com>
parents:
1535
diff
changeset
|
38 |
868 | 39 proxy_responses 1; |
40 proxy_timeout 1s; | |
41 | |
42 upstream u { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
43 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
|
44 server 127.0.0.1:%%PORT_8985_UDP%%; |
868 | 45 } |
46 | |
47 upstream u2 { | |
1237
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_8986_UDP%% down; |
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%%; |
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_8984_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_8985_UDP%%; |
868 | 52 } |
53 | |
54 upstream u3 { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
55 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
|
56 server 127.0.0.1:%%PORT_8985_UDP%% weight=2; |
868 | 57 } |
58 | |
59 upstream u4 { | |
1334
c4f58dfe8207
Tests: stream udp tests adjusted for upcoming udp streams.
Roman Arutyunyan <arut@nginx.com>
parents:
1251
diff
changeset
|
60 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
|
61 server 127.0.0.1:%%PORT_8984_UDP%% backup; |
868 | 62 } |
63 | |
64 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
65 listen 127.0.0.1:%%PORT_8980_UDP%% udp; |
868 | 66 proxy_pass u; |
67 } | |
68 | |
69 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
70 listen 127.0.0.1:%%PORT_8981_UDP%% udp; |
868 | 71 proxy_pass u2; |
72 } | |
73 | |
74 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
75 listen 127.0.0.1:%%PORT_8982_UDP%% udp; |
868 | 76 proxy_pass u3; |
77 } | |
78 | |
79 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
80 listen 127.0.0.1:%%PORT_8983_UDP%% udp; |
868 | 81 proxy_pass u4; |
82 } | |
83 } | |
84 | |
85 EOF | |
86 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
87 $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
|
88 $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
|
89 $t->run(); |
868 | 90 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
91 $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
|
92 $t->waitforfile($t->testdir . '/' . port(8985)); |
868 | 93 |
94 ############################################################################### | |
95 | |
1334
c4f58dfe8207
Tests: stream udp tests adjusted for upcoming udp streams.
Roman Arutyunyan <arut@nginx.com>
parents:
1251
diff
changeset
|
96 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
|
97 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
98 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
|
99 |
1424
5bee71e1548f
Tests: adjusted stream udp test with failed peer.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
100 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
|
101 '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
|
102 |
1424
5bee71e1548f
Tests: adjusted stream udp test with failed peer.
Sergey Kandaurov <pluknet@nginx.com>
parents:
1334
diff
changeset
|
103 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
|
104 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1165
diff
changeset
|
105 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
|
106 is(many(10, port(8983)), "$port4: 10", 'backup'); |
868 | 107 |
108 ############################################################################### | |
109 | |
110 sub many { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
111 my ($count, $port) = @_; |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
112 my (%ports); |
868 | 113 |
114 for (1 .. $count) { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
115 if (dgram("127.0.0.1:$port")->io('.') =~ /(\d+)/) { |
868 | 116 $ports{$1} = 0 unless defined $ports{$1}; |
117 $ports{$1}++; | |
118 } | |
119 } | |
120 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
121 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
|
122 return join ', ', map { $_ . ": " . $ports{$_} } @keys; |
868 | 123 } |
124 | |
125 ############################################################################### | |
126 | |
127 sub udp_daemon { | |
128 my ($port, $t) = @_; | |
129 | |
130 my $server = IO::Socket::INET->new( | |
131 Proto => 'udp', | |
132 LocalAddr => '127.0.0.1:' . $port, | |
133 Reuse => 1, | |
134 ) | |
135 or die "Can't create listening socket: $!\n"; | |
136 | |
137 # signal we are ready | |
138 | |
139 open my $fh, '>', $t->testdir() . '/' . $port; | |
140 close $fh; | |
141 | |
142 while (1) { | |
143 $server->recv(my $buffer, 65536); | |
144 $buffer = $server->sockport(); | |
145 $server->send($buffer); | |
146 } | |
147 } | |
148 | |
149 ############################################################################### |