Mercurial > hg > nginx-tests
annotate stream_udp_upstream_least_conn.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 least_conn 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 | |
947
b9e42c554ba7
Tests: skip UDP tests on win32, udp feature introduced.
Sergey Kandaurov <pluknet@nginx.com>
parents:
868
diff
changeset
|
26 my $t = Test::Nginx->new()->has(qw/stream stream_upstream_least_conn udp/) |
1020
196d33c2bb45
Tests: removed TODO and try_run() checks for legacy versions.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
27 ->plan(2)->write_file_expand('nginx.conf', <<'EOF'); |
868 | 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:
1237
diff
changeset
|
37 %%TEST_GLOBALS_STREAM%% |
f3ba4c74de31
Tests: added TEST_GLOBALS_STREAM variable support.
Andrei Belov <defan@nginx.com>
parents:
1237
diff
changeset
|
38 |
868 | 39 proxy_responses 1; |
40 proxy_timeout 1s; | |
41 | |
42 upstream u { | |
43 least_conn; | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
44 server 127.0.0.1:%%PORT_8981_UDP%%; |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
45 server 127.0.0.1:%%PORT_8982_UDP%%; |
868 | 46 } |
47 | |
48 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
49 listen 127.0.0.1:%%PORT_8980_UDP%% udp; |
868 | 50 proxy_pass u; |
51 } | |
52 } | |
53 | |
54 EOF | |
55 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
56 $t->run_daemon(\&udp_daemon, port(8981), $t); |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
57 $t->run_daemon(\&udp_daemon, port(8982), $t); |
1020
196d33c2bb45
Tests: removed TODO and try_run() checks for legacy versions.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
58 $t->run(); |
868 | 59 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
60 $t->waitforfile($t->testdir . '/' . port(8981)); |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
61 $t->waitforfile($t->testdir . '/' . port(8982)); |
868 | 62 |
63 ############################################################################### | |
64 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
65 my @ports = my ($port1, $port2) = (port(8981), port(8982)); |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
66 |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
67 is(many(10), "$port1: 5, $port2: 5", 'balanced'); |
868 | 68 |
69 my @sockets; | |
70 for (1 .. 2) { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
71 my $s = dgram('127.0.0.1:' . port(8980)); |
868 | 72 $s->write('w'); |
73 push @sockets, $s; | |
74 } | |
75 | |
76 select undef, undef, undef, 0.2; | |
77 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
78 is(many(10), "$port2: 10", 'least_conn'); |
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) = @_; |
868 | 84 my (%ports); |
85 | |
86 for (1 .. $count) { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
87 if (dgram('127.0.0.1:' . port(8980))->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 | |
117 my $port = $server->sockport(); | |
118 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
119 if ($buffer =~ /w/ && $port == port(8981)) { |
868 | 120 select undef, undef, undef, 2.5; |
121 } | |
122 | |
123 $buffer = $port; | |
124 | |
125 $server->send($buffer); | |
126 } | |
127 } | |
128 | |
129 ############################################################################### |