Mercurial > hg > nginx-tests
annotate stream_udp_upstream_least_conn.t @ 1248:70192b1baf01
Tests: added exception test to stream_js.t using 'require'.
The stream js tests introduced in edf5a3c9e36a fail on njs 0.1.14. It doesn't
currently provide an easy way to check its version, whilst we are obligated to
gracefully handle such cases somehow. With such an addition of 'require', now
the tests are skipped instead on the previous versions.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Tue, 21 Nov 2017 13:16:39 +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 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 { | |
37 proxy_responses 1; | |
38 proxy_timeout 1s; | |
39 | |
40 upstream u { | |
41 least_conn; | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
42 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
|
43 server 127.0.0.1:%%PORT_8982_UDP%%; |
868 | 44 } |
45 | |
46 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
47 listen 127.0.0.1:%%PORT_8980_UDP%% udp; |
868 | 48 proxy_pass u; |
49 } | |
50 } | |
51 | |
52 EOF | |
53 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
54 $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
|
55 $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
|
56 $t->run(); |
868 | 57 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
58 $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
|
59 $t->waitforfile($t->testdir . '/' . port(8982)); |
868 | 60 |
61 ############################################################################### | |
62 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
63 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
|
64 |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
65 is(many(10), "$port1: 5, $port2: 5", 'balanced'); |
868 | 66 |
67 my @sockets; | |
68 for (1 .. 2) { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
69 my $s = dgram('127.0.0.1:' . port(8980)); |
868 | 70 $s->write('w'); |
71 push @sockets, $s; | |
72 } | |
73 | |
74 select undef, undef, undef, 0.2; | |
75 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
76 is(many(10), "$port2: 10", 'least_conn'); |
868 | 77 |
78 ############################################################################### | |
79 | |
80 sub many { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
81 my ($count) = @_; |
868 | 82 my (%ports); |
83 | |
84 for (1 .. $count) { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
85 if (dgram('127.0.0.1:' . port(8980))->io('.') =~ /(\d+)/) { |
868 | 86 $ports{$1} = 0 unless defined $ports{$1}; |
87 $ports{$1}++; | |
88 } | |
89 } | |
90 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
91 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
|
92 return join ', ', map { $_ . ": " . $ports{$_} } @keys; |
868 | 93 } |
94 | |
95 ############################################################################### | |
96 | |
97 sub udp_daemon { | |
98 my ($port, $t) = @_; | |
99 | |
100 my $server = IO::Socket::INET->new( | |
101 Proto => 'udp', | |
102 LocalAddr => '127.0.0.1:' . $port, | |
103 Reuse => 1, | |
104 ) | |
105 or die "Can't create listening socket: $!\n"; | |
106 | |
107 # signal we are ready | |
108 | |
109 open my $fh, '>', $t->testdir() . '/' . $port; | |
110 close $fh; | |
111 | |
112 while (1) { | |
113 $server->recv(my $buffer, 65536); | |
114 | |
115 my $port = $server->sockport(); | |
116 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
117 if ($buffer =~ /w/ && $port == port(8981)) { |
868 | 118 select undef, undef, undef, 2.5; |
119 } | |
120 | |
121 $buffer = $port; | |
122 | |
123 $server->send($buffer); | |
124 } | |
125 } | |
126 | |
127 ############################################################################### |