-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathstream_udp_proxy_requests.t
217 lines (151 loc) · 5.51 KB
/
stream_udp_proxy_requests.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/perl
# (C) Sergey Kandaurov
# (C) Nginx, Inc.
# Tests for stream proxy module, the proxy_requests directive.
###############################################################################
use warnings;
use strict;
use Test::More;
BEGIN { use FindBin; chdir($FindBin::Bin); }
use lib 'lib';
use Test::Nginx;
use Test::Nginx::Stream qw/ dgram /;
###############################################################################
select STDERR; $| = 1;
select STDOUT; $| = 1;
my $t = Test::Nginx->new()->has(qw/stream udp/)->plan(26)
->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
daemon off;
events {
}
stream {
%%TEST_GLOBALS_STREAM%%
proxy_timeout 2100ms;
log_format status $status;
server {
listen 127.0.0.1:%%PORT_8980_UDP%% udp;
proxy_pass 127.0.0.1:%%PORT_8990_UDP%%;
proxy_requests 0;
}
server {
listen 127.0.0.1:%%PORT_8981_UDP%% udp;
proxy_pass 127.0.0.1:%%PORT_8990_UDP%%;
proxy_requests 1;
}
server {
listen 127.0.0.1:%%PORT_8982_UDP%% udp;
proxy_pass 127.0.0.1:%%PORT_8990_UDP%%;
proxy_requests 2;
}
server {
listen 127.0.0.1:%%PORT_8983_UDP%% udp;
proxy_pass 127.0.0.1:%%PORT_8990_UDP%%;
}
server {
listen 127.0.0.1:%%PORT_8984_UDP%% udp;
proxy_pass 127.0.0.1:%%PORT_8991_UDP%%;
proxy_requests 2;
access_log %%TESTDIR%%/s.log status;
}
server {
listen 127.0.0.1:%%PORT_8985_UDP%% udp;
proxy_pass 127.0.0.1:%%PORT_8991_UDP%%;
proxy_requests 2;
proxy_responses 2;
access_log %%TESTDIR%%/s2.log status;
}
}
EOF
$t->run_daemon(\&udp_daemon, $t, port(8990));
$t->run_daemon(\&udp_daemon, $t, port(8991));
$t->run();
$t->waitforfile($t->testdir . '/' . port(8990));
$t->waitforfile($t->testdir . '/' . port(8991));
###############################################################################
# proxy_requests 0, binding is not dropped across streams
my $s = dgram('127.0.0.1:' . port(8980));
my $n = $s->io('1', read => 1);
ok($n, 'requests 0 create');
is($s->read(), '1', 'requests 0 create - response');
is($s->io('1', read => 1), $n, 'requests 0 second - binding saved');
is($s->read(), '1', 'requests 0 second - response');
is($s->io('1', read => 1), $n, 'requests 0 follow - binding saved');
is($s->read(), '1', 'requests 0 follow - response');
# proxy_requests 1, binding is dropped on every next stream
$s = dgram('127.0.0.1:' . port(8981));
$n = $s->io('1', read => 1);
ok($n, 'requests 1 create');
is($s->read(), '1', 'requests 1 create - response');
isnt($s->io('1', read => 1), $n, 'requests 1 second - binding lost');
is($s->read(), '1', 'requests 1 second - response');
# proxy_requests 2, binding is dropped on every second stream
$s = dgram('127.0.0.1:' . port(8982));
$n = $s->io('1', read => 1);
ok($n, 'requests 2 create');
is($s->read(), '1', 'requests 2 create - response');
is($s->io('1', read => 1), $n, 'requests 2 second - binding saved');
is($s->read(), '1', 'requests 2 second - response');
isnt($s->io('1', read => 1), $n, 'requests 2 follow - binding lost');
is($s->read(), '1', 'requests 2 follow - response');
# proxy_requests unset, binding is not dropped across streams
$s = dgram('127.0.0.1:' . port(8983));
$n = $s->io('1', read => 1);
ok($n, 'requests unset create');
is($s->read(), '1', 'requests unset create - response');
is($s->io('1', read => 1), $n, 'requests unset second - binding saved');
is($s->read(), '1', 'requests unset second - response');
is($s->io('1', read => 1), $n, 'requests unset follow - binding saved');
is($s->read(), '1', 'requests unset follow - response');
# proxy_requests 2, with slow backend
# client sends 5 packets, each responded with 3 packets
# expects all packets proxied from backend, the last (uneven) session succeed
$s = dgram('127.0.0.1:' . port(8984));
like(many($s, '2', 5, 15),
qr/^(\d+ 1 2) \1 (?!\1)(\d+ 1 2) \2 (?!\2)\d+ 1 2$/, 'slow backend');
# proxy_requests 2, proxy_responses 2
# client sends 5 packets, each responded with 2 packets
# expects all packets proxied from backend, the last (uneven) session succeed
$s = dgram('127.0.0.1:' . port(8985));
like(many($s, '1', 5, 10),
qr/^(\d+ 1) \1 (?!\1)(\d+ 1) \2 (?!\2)\d+ 1$/, 'requests - responses');
$t->stop();
is($t->read_file('s.log'), <<EOF, 'uneven session status - slow backend');
200
200
200
EOF
is($t->read_file('s2.log'), <<EOF, 'uneven session status - responses');
200
200
200
EOF
###############################################################################
sub many {
my ($s, $buf, $wcount, $rcount) = @_;
$s->write($buf) for 1 .. $wcount;
join ' ', map { $_->[1] }
sort { $a->[0] <=> $b->[0] }
map { [ $s->read() =~ /^(\d+) (.+)/ ] } 1 .. $rcount;
}
###############################################################################
sub udp_daemon {
my ($t, $port) = @_;
my $server = IO::Socket::INET->new(
Proto => 'udp',
LocalAddr => "127.0.0.1:$port",
Reuse => 1
)
or die "Can't create listening socket: $!\n";
# signal we are ready
open my $fh, '>', $t->testdir() . "/$port";
close $fh;
my ($slp, $i) = (1, 1) if $port == port(8991);
while (1) {
$server->recv(my $buffer, 65536);
sleep 1, $slp = 0 if $slp;
$server->send(($i ? "${\($i++)} " : "") . $server->peerport());
$server->send(($i ? "${\($i++)} " : "") . $_) for (1..$buffer);
}
}
###############################################################################