Mercurial > hg > nginx-tests
annotate stream_udp_upstream_least_conn.t @ 973:7d7aef8b9f3a
Tests: revert 313e3f1905d4 and set reuse properly.
It is too late to use setsockopt(SO_REUSEADDR) after a socket is created
as bind() happens during socket construction, and fails if there is
a conflicting TIME-WAIT socket and SO_REUSEADDR is not set.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Sun, 10 Jul 2016 18:57:12 +0300 |
parents | e9064d691790 |
children | 882267679006 |
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/) |
868 | 27 ->write_file_expand('nginx.conf', <<'EOF'); |
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; | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
42 server 127.0.0.1:%%PORT_1_UDP%%; |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
43 server 127.0.0.1:%%PORT_2_UDP%%; |
868 | 44 } |
45 | |
46 server { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
47 listen 127.0.0.1:%%PORT_0_UDP%% udp; |
868 | 48 proxy_pass u; |
49 } | |
50 } | |
51 | |
52 EOF | |
53 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
54 $t->run_daemon(\&udp_daemon, port(1), $t); |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
55 $t->run_daemon(\&udp_daemon, port(2), $t); |
868 | 56 $t->try_run('no stream udp')->plan(2); |
57 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
58 $t->waitforfile($t->testdir . '/' . port(1)); |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
59 $t->waitforfile($t->testdir . '/' . port(2)); |
868 | 60 |
61 ############################################################################### | |
62 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
63 my @ports = my ($port1, $port2) = (port(1), port(2)); |
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) { | |
69 my $s = dgram(); | |
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) { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
85 if (dgram()->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 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
117 if ($buffer =~ /w/ && $port == port(1)) { |
868 | 118 select undef, undef, undef, 2.5; |
119 } | |
120 | |
121 $buffer = $port; | |
122 | |
123 $server->send($buffer); | |
124 } | |
125 } | |
126 | |
127 ############################################################################### |