blob: 37c95b0b58a9fbcb01e973f87b0c0e2ca89be156 [file] [log] [blame]
Sergey Kandaurov84028062015-04-22 16:59:40 +03001#!/usr/bin/perl
2
3# (C) Sergey Kandaurov
4# (C) Nginx, Inc.
5
6# Stream tests for upstream module and balancers.
7
8###############################################################################
9
10use warnings;
11use strict;
12
13use Test::More;
14
15use IO::Select;
16
17BEGIN { use FindBin; chdir($FindBin::Bin); }
18
19use lib 'lib';
20use Test::Nginx;
Andrey Zelenkov3f401492015-07-20 15:06:09 +030021use Test::Nginx::Stream qw/ stream /;
Sergey Kandaurov84028062015-04-22 16:59:40 +030022
23###############################################################################
24
25select STDERR; $| = 1;
26select STDOUT; $| = 1;
27
Sergey Kandaurov7be33d22016-09-05 22:57:44 +030028my $t = Test::Nginx->new()->has(qw/stream/)
Sergey Kandaurov84028062015-04-22 16:59:40 +030029 ->write_file_expand('nginx.conf', <<'EOF');
30
31%%TEST_GLOBALS%%
32
33daemon off;
34
35events {
36}
37
38stream {
Sergey Kandaurov7be33d22016-09-05 22:57:44 +030039 log_format bytes $upstream_addr!
40 $upstream_bytes_sent!$upstream_bytes_received;
41
Sergey Kandaurov84028062015-04-22 16:59:40 +030042 upstream u {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030043 server 127.0.0.1:8084;
44 server 127.0.0.1:8085;
Sergey Kandaurov84028062015-04-22 16:59:40 +030045 }
46
47 upstream u2 {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030048 server 127.0.0.1:8086 down;
49 server 127.0.0.1:8086;
50 server 127.0.0.1:8084;
51 server 127.0.0.1:8085;
Sergey Kandaurov84028062015-04-22 16:59:40 +030052 }
53
54 upstream u3 {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030055 server 127.0.0.1:8084;
56 server 127.0.0.1:8085 weight=2;
Sergey Kandaurov84028062015-04-22 16:59:40 +030057 }
58
59 upstream u4 {
Sergey Kandaurov82f138e2016-12-29 19:30:29 +030060 server 127.0.0.1:8086 fail_timeout=1s;
Andrey Zelenkove59bf362016-07-12 17:39:03 +030061 server 127.0.0.1:8084 backup;
Sergey Kandaurov84028062015-04-22 16:59:40 +030062 }
63
Sergey Kandaurovcd952702019-09-09 15:42:31 +030064 proxy_connect_timeout 2;
65
Sergey Kandaurov84028062015-04-22 16:59:40 +030066 server {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030067 listen 127.0.0.1:8080;
Sergey Kandaurov84028062015-04-22 16:59:40 +030068 proxy_pass u;
69 }
70
71 server {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030072 listen 127.0.0.1:8081;
Sergey Kandaurov84028062015-04-22 16:59:40 +030073 proxy_pass u2;
74 }
75
76 server {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030077 listen 127.0.0.1:8082;
Sergey Kandaurov84028062015-04-22 16:59:40 +030078 proxy_pass u3;
79 }
80
81 server {
Andrey Zelenkove59bf362016-07-12 17:39:03 +030082 listen 127.0.0.1:8083;
Sergey Kandaurov84028062015-04-22 16:59:40 +030083 proxy_pass u4;
Sergey Kandaurov7be33d22016-09-05 22:57:44 +030084 access_log %%TESTDIR%%/u.log bytes;
Sergey Kandaurov84028062015-04-22 16:59:40 +030085 }
86}
87
88EOF
89
Andrey Zelenkove59bf362016-07-12 17:39:03 +030090$t->run_daemon(\&stream_daemon, port(8084));
91$t->run_daemon(\&stream_daemon, port(8085));
Sergey Kandaurov9473f142017-11-24 19:58:40 +030092$t->run()->plan(6);
Sergey Kandaurov84028062015-04-22 16:59:40 +030093
Andrey Zelenkove59bf362016-07-12 17:39:03 +030094$t->waitforsocket('127.0.0.1:' . port(8084));
95$t->waitforsocket('127.0.0.1:' . port(8085));
Sergey Kandaurov84028062015-04-22 16:59:40 +030096
97###############################################################################
98
Sergey Kandaurov7be33d22016-09-05 22:57:44 +030099my @ports = my ($port4, $port5, $port6) = (port(8084), port(8085), port(8086));
Andrey Zelenkovc91d8f82016-06-21 16:39:13 +0300100
Andrey Zelenkove59bf362016-07-12 17:39:03 +0300101is(many(30, port(8080)), "$port4: 15, $port5: 15", 'balanced');
102is(many(30, port(8081)), "$port4: 15, $port5: 15", 'failures');
103is(many(30, port(8082)), "$port4: 10, $port5: 20", 'weight');
104is(many(30, port(8083)), "$port4: 30", 'backup');
Sergey Kandaurov84028062015-04-22 16:59:40 +0300105
Sergey Kandaurov82f138e2016-12-29 19:30:29 +0300106$t->run_daemon(\&stream_daemon, port(8086));
107$t->waitforsocket('127.0.0.1:' . port(8086));
108
Sergey Kandaurov82f138e2016-12-29 19:30:29 +0300109sleep 2; # wait till fail_timeout passes
110is(parallel(30, port(8083)), "$port6: 30", 'recovery');
111
Sergey Kandaurov7be33d22016-09-05 22:57:44 +0300112$t->stop();
113
114like($t->read_file('u.log'), qr/127.0.0.1:$port6, 127.0.0.1:$port4!0, 1!0, 4/,
115 'per-upstream variables');
116
Sergey Kandaurov84028062015-04-22 16:59:40 +0300117###############################################################################
118
119sub many {
Andrey Zelenkovc91d8f82016-06-21 16:39:13 +0300120 my ($count, $port) = @_;
121 my (%ports);
Sergey Kandaurov84028062015-04-22 16:59:40 +0300122
123 for (1 .. $count) {
Andrey Zelenkovc91d8f82016-06-21 16:39:13 +0300124 if (stream("127.0.0.1:$port")->io('.') =~ /(\d+)/) {
Sergey Kandaurov84028062015-04-22 16:59:40 +0300125 $ports{$1} = 0 unless defined $ports{$1};
126 $ports{$1}++;
127 }
128 }
129
Andrey Zelenkovc91d8f82016-06-21 16:39:13 +0300130 my @keys = map { my $p = $_; grep { $p == $_ } keys %ports } @ports;
131 return join ', ', map { $_ . ": " . $ports{$_} } @keys;
Sergey Kandaurov84028062015-04-22 16:59:40 +0300132}
133
Sergey Kandaurov82f138e2016-12-29 19:30:29 +0300134sub parallel {
135 my ($count, $port) = @_;
136 my (%ports, @s);
137
138 for (1 .. $count) {
139 my $s = stream("127.0.0.1:$port");
140 $s->write('keep');
141 $s->read();
142 push @s, $s;
143 }
144
145 for (1 .. $count) {
146 if ((pop @s)->io('.') =~ /(\d+)/) {
147 $ports{$1} = 0 unless defined $ports{$1};
148 $ports{$1}++;
149 }
150 }
151
152 my @keys = map { my $p = $_; grep { $p == $_ } keys %ports } @ports;
153 return join ', ', map { $_ . ": " . $ports{$_} } @keys;
154}
155
Sergey Kandaurov84028062015-04-22 16:59:40 +0300156###############################################################################
157
158sub stream_daemon {
159 my ($port) = @_;
160
161 my $server = IO::Socket::INET->new(
162 Proto => 'tcp',
163 LocalAddr => '127.0.0.1',
164 LocalPort => $port,
165 Listen => 5,
166 Reuse => 1
167 )
168 or die "Can't create listening socket: $!\n";
169
170 my $sel = IO::Select->new($server);
171
172 local $SIG{PIPE} = 'IGNORE';
173
174 while (my @ready = $sel->can_read) {
175 foreach my $fh (@ready) {
176 if ($server == $fh) {
177 my $new = $fh->accept;
178 $new->autoflush(1);
179 $sel->add($new);
180
181 } elsif (stream_handle_client($fh)) {
182 $sel->remove($fh);
183 $fh->close;
184 }
185 }
186 }
187}
188
189sub stream_handle_client {
190 my ($client) = @_;
191
192 log2c("(new connection $client)");
193
194 $client->sysread(my $buffer, 65536) or return 1;
195
196 log2i("$client $buffer");
197
Sergey Kandaurov82f138e2016-12-29 19:30:29 +0300198 my $close = $buffer ne 'keep';
Sergey Kandaurov84028062015-04-22 16:59:40 +0300199 $buffer = $client->sockport();
200
201 log2o("$client $buffer");
202
203 $client->syswrite($buffer);
204
Sergey Kandaurov82f138e2016-12-29 19:30:29 +0300205 return $close;
Sergey Kandaurov84028062015-04-22 16:59:40 +0300206}
207
208sub log2i { Test::Nginx::log_core('|| <<', @_); }
209sub log2o { Test::Nginx::log_core('|| >>', @_); }
210sub log2c { Test::Nginx::log_core('||', @_); }
211
212###############################################################################