Mercurial > hg > nginx-tests
comparison stream_limit_rate.t @ 632:c8d6f816e094
Tests: stream limit rate tests.
author | Andrey Zelenkov <zelenkov@nginx.com> |
---|---|
date | Tue, 07 Jul 2015 14:45:11 +0300 |
parents | |
children | 40675bfad7d3 |
comparison
equal
deleted
inserted
replaced
631:fca0079e0aab | 632:c8d6f816e094 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Andrey Zelenkov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for stream proxy module, limit rate directives. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 use IO::Select; | |
16 | |
17 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
18 | |
19 use lib 'lib'; | |
20 use Test::Nginx; | |
21 | |
22 ############################################################################### | |
23 | |
24 select STDERR; $| = 1; | |
25 select STDOUT; $| = 1; | |
26 | |
27 my $t = Test::Nginx->new()->has(qw/stream/) | |
28 ->write_file_expand('nginx.conf', <<'EOF'); | |
29 | |
30 %%TEST_GLOBALS%% | |
31 | |
32 daemon off; | |
33 | |
34 events { | |
35 } | |
36 | |
37 stream { | |
38 # download and upload rates are set equal to the maximum | |
39 # number of bytes transmitted | |
40 | |
41 # proxy_download_rate value comes from following calculations: | |
42 # test string length (1000) + whitespace (1) + time string length (10) | |
43 | |
44 proxy_download_rate 1011; | |
45 proxy_upload_rate 1000; | |
46 | |
47 server { | |
48 listen 127.0.0.1:8081; | |
49 proxy_pass 127.0.0.1:8080; | |
50 } | |
51 | |
52 server { | |
53 listen 127.0.0.1:8082; | |
54 proxy_pass 127.0.0.1:8080; | |
55 proxy_download_rate 0; | |
56 proxy_upload_rate 0; | |
57 } | |
58 | |
59 server { | |
60 listen 127.0.0.1:8083; | |
61 proxy_pass 127.0.0.1:8080; | |
62 proxy_download_rate 1; | |
63 } | |
64 | |
65 server { | |
66 listen 127.0.0.1:8084; | |
67 proxy_pass 127.0.0.1:8080; | |
68 proxy_upload_rate 1; | |
69 } | |
70 | |
71 server { | |
72 listen 127.0.0.1:8085; | |
73 proxy_pass 127.0.0.1:8080; | |
74 proxy_download_rate 500; | |
75 } | |
76 | |
77 server { | |
78 listen 127.0.0.1:8086; | |
79 proxy_pass 127.0.0.1:8090; | |
80 proxy_upload_rate 500; | |
81 } | |
82 } | |
83 | |
84 EOF | |
85 | |
86 $t->run_daemon(\&stream_daemon, 8080); | |
87 $t->run_daemon(\&stream_daemon, 8090); | |
88 | |
89 $t->try_run('no proxy_download_rate and/or proxy_upload_rate')->plan(8); | |
90 | |
91 $t->waitforsocket('127.0.0.1:8080'); | |
92 $t->waitforsocket('127.0.0.1:8090'); | |
93 | |
94 ############################################################################### | |
95 | |
96 my $str = '1234567890' x 100; | |
97 | |
98 my %r = stream_get($str, peer => '127.0.0.1:8081'); | |
99 is($r{'data'}, $str, 'exact limit'); | |
100 | |
101 %r = stream_get($str, peer => '127.0.0.1:8082'); | |
102 is($r{'data'}, $str, 'unlimited'); | |
103 | |
104 SKIP: { | |
105 skip 'unsafe on VM', 2 unless $ENV{TEST_NGINX_UNSAFE}; | |
106 | |
107 # if interaction between backend and client is slow then proxy can add extra | |
108 # bytes to upload/download data | |
109 | |
110 %r = stream_get($str, peer => '127.0.0.1:8083', readonce => 1); | |
111 is($r{'data'}, '1', 'download - one byte'); | |
112 | |
113 %r = stream_get($str, peer => '127.0.0.1:8084'); | |
114 is($r{'data'}, '1', 'upload - one byte'); | |
115 | |
116 } | |
117 | |
118 # Three chunks are split with two 1s delays + 1s error: | |
119 # the first two chunks are halfs of test string | |
120 # and the third one is some extra data from backend. | |
121 | |
122 %r = stream_get($str, peer => '127.0.0.1:8085'); | |
123 my $diff = time() - $r{'time'}; | |
124 cmp_ok(abs($diff - 2), '<=', 1, 'download - time'); | |
125 is($r{'data'}, $str, 'download - data'); | |
126 | |
127 my $time = time(); | |
128 %r = stream_get($str . 'close', peer => '127.0.0.1:8086'); | |
129 $diff = time() - $time; | |
130 cmp_ok(abs($diff - 2), '<=', 1, 'upload - time'); | |
131 is($r{'data'}, $str . 'close', 'upload - data'); | |
132 | |
133 ############################################################################### | |
134 | |
135 sub stream_get { | |
136 my ($data, %extra) = @_; | |
137 | |
138 my $s = stream_connect($extra{'peer'}); | |
139 stream_write($s, $data); | |
140 | |
141 $data = ''; | |
142 while (my $buf = stream_read($s)) { | |
143 $data .= $buf; | |
144 last if $extra{'readonce'}; | |
145 } | |
146 $data =~ /([\S]*)\s?(\d+)?/; | |
147 | |
148 return ('data' => $1, 'time' => $2); | |
149 } | |
150 | |
151 sub stream_connect { | |
152 my $peer = shift; | |
153 my $s = IO::Socket::INET->new( | |
154 Proto => 'tcp', | |
155 PeerAddr => $peer | |
156 ) | |
157 or die "Can't connect to nginx: $!\n"; | |
158 | |
159 return $s; | |
160 } | |
161 | |
162 sub stream_write { | |
163 my ($s, $message) = @_; | |
164 | |
165 local $SIG{PIPE} = 'IGNORE'; | |
166 | |
167 $s->blocking(0); | |
168 while (IO::Select->new($s)->can_write(1.5)) { | |
169 my $n = $s->syswrite($message); | |
170 last unless $n; | |
171 $message = substr($message, $n); | |
172 last unless length $message; | |
173 } | |
174 | |
175 if (length $message) { | |
176 $s->close(); | |
177 } | |
178 } | |
179 | |
180 sub stream_read { | |
181 my ($s) = @_; | |
182 my ($buf); | |
183 | |
184 $s->blocking(0); | |
185 if (IO::Select->new($s)->can_read(3)) { | |
186 $s->sysread($buf, 1024); | |
187 }; | |
188 | |
189 log_in($buf); | |
190 return $buf; | |
191 } | |
192 | |
193 ############################################################################### | |
194 | |
195 sub stream_daemon { | |
196 my $port = shift; | |
197 my $server = IO::Socket::INET->new( | |
198 Proto => 'tcp', | |
199 LocalAddr => '127.0.0.1', | |
200 LocalPort => $port, | |
201 Listen => 5, | |
202 Reuse => 1 | |
203 ) | |
204 or die "Can't create listening socket: $!\n"; | |
205 | |
206 my $sel = IO::Select->new($server); | |
207 | |
208 local $SIG{PIPE} = 'IGNORE'; | |
209 | |
210 while (my @ready = $sel->can_read) { | |
211 foreach my $fh (@ready) { | |
212 if ($server == $fh) { | |
213 my $new = $fh->accept; | |
214 $new->autoflush(1); | |
215 $sel->add($new); | |
216 | |
217 } elsif (stream_handle_client($fh)) { | |
218 $sel->remove($fh); | |
219 $fh->close; | |
220 } | |
221 } | |
222 } | |
223 } | |
224 | |
225 sub stream_handle_client { | |
226 my ($client) = @_; | |
227 | |
228 log2c("(new connection $client)"); | |
229 | |
230 $client->sysread(my $buffer, 65536) or return 1; | |
231 | |
232 log2i("$client $buffer"); | |
233 | |
234 $buffer .= " " . time() if $client->sockport() eq 8080; | |
235 | |
236 log2o("$client $buffer"); | |
237 | |
238 $client->syswrite($buffer); | |
239 | |
240 return $client->sockport() eq 8080 ? 1 : $buffer =~ /close/; | |
241 } | |
242 | |
243 sub log2i { Test::Nginx::log_core('|| <<', @_); } | |
244 sub log2o { Test::Nginx::log_core('|| >>', @_); } | |
245 sub log2c { Test::Nginx::log_core('||', @_); } | |
246 | |
247 ############################################################################### |