Mercurial > hg > nginx-tests
comparison limit-req.t @ 103:4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
1. Make sure rejected requests are not counted, as leaky bucket algorithm
suggests (finally correctly done in 0.8.18).
2. Make sure negative excess values are handled properly (bug appeared in
0.8.18, fixed in 0.8.19).
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Fri, 09 Oct 2009 21:05:42 +0400 |
parents | 506586cc2f3b |
children | 1c0ec30614c6 |
comparison
equal
deleted
inserted
replaced
102:9f723d3ba52d | 103:4ae2198b97ec |
---|---|
19 ############################################################################### | 19 ############################################################################### |
20 | 20 |
21 select STDERR; $| = 1; | 21 select STDERR; $| = 1; |
22 select STDOUT; $| = 1; | 22 select STDOUT; $| = 1; |
23 | 23 |
24 my $t = Test::Nginx->new()->plan(3); | 24 my $t = Test::Nginx->new()->plan(5); |
25 | 25 |
26 $t->write_file_expand('nginx.conf', <<'EOF'); | 26 $t->write_file_expand('nginx.conf', <<'EOF'); |
27 | 27 |
28 master_process off; | 28 master_process off; |
29 daemon off; | 29 daemon off; |
37 | 37 |
38 client_body_temp_path %%TESTDIR%%/client_body_temp; | 38 client_body_temp_path %%TESTDIR%%/client_body_temp; |
39 fastcgi_temp_path %%TESTDIR%%/fastcgi_temp; | 39 fastcgi_temp_path %%TESTDIR%%/fastcgi_temp; |
40 proxy_temp_path %%TESTDIR%%/proxy_temp; | 40 proxy_temp_path %%TESTDIR%%/proxy_temp; |
41 | 41 |
42 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/m; | 42 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; |
43 limit_req_zone $binary_remote_addr zone=long:10m rate=1r/m; | 43 limit_req_zone $binary_remote_addr zone=long:10m rate=1r/m; |
44 limit_req_zone $binary_remote_addr zone=fast:10m rate=1000r/s; | |
44 | 45 |
45 server { | 46 server { |
46 listen 127.0.0.1:8080; | 47 listen 127.0.0.1:8080; |
47 server_name localhost; | 48 server_name localhost; |
48 location / { | 49 location / { |
49 limit_req zone=one burst=1 nodelay; | 50 limit_req zone=one burst=1 nodelay; |
50 } | 51 } |
51 location /long { | 52 location /long { |
52 limit_req zone=long burst=5; | 53 limit_req zone=long burst=5; |
53 } | 54 } |
55 location /fast { | |
56 limit_req zone=fast burst=1; | |
57 } | |
54 } | 58 } |
55 } | 59 } |
56 | 60 |
57 EOF | 61 EOF |
58 | 62 |
59 $t->write_file('test1.html', 'XtestX'); | 63 $t->write_file('test1.html', 'XtestX'); |
60 $t->write_file('long.html', "1234567890\n" x (1 << 16)); | 64 $t->write_file('long.html', "1234567890\n" x (1 << 16)); |
65 $t->write_file('fast.html', 'XtestX'); | |
61 $t->run(); | 66 $t->run(); |
62 | 67 |
63 ############################################################################### | 68 ############################################################################### |
64 | 69 |
65 like(http_get('/test1.html'), qr/^HTTP\/1.. 200 /m, 'request'); | 70 like(http_get('/test1.html'), qr/^HTTP\/1.. 200 /m, 'request'); |
66 http_get('/test1.html'); | 71 http_get('/test1.html'); |
67 like(http_get('/test1.html'), qr/^HTTP\/1.. 503 /m, 'request rejected'); | 72 like(http_get('/test1.html'), qr/^HTTP\/1.. 503 /m, 'request rejected'); |
73 http_get('/test1.html'); | |
74 http_get('/test1.html'); | |
68 | 75 |
69 # Second request will be delayed by limit_req, make sure it isn't truncated. | 76 # Second request will be delayed by limit_req, make sure it isn't truncated. |
70 # The bug only manifests itself if buffer will be filled, so sleep for a while | 77 # The bug only manifests itself if buffer will be filled, so sleep for a while |
71 # before reading response. | 78 # before reading response. |
72 | 79 |
73 my $l1 = length(http_get('/long.html')); | 80 my $l1 = length(http_get('/long.html')); |
74 my $l2 = length(http_get('/long.html', sleep => 1.1)); | 81 my $l2 = length(http_get('/long.html', sleep => 1.1)); |
75 is($l2, $l1, 'delayed big request not truncated'); | 82 is($l2, $l1, 'delayed big request not truncated'); |
76 | 83 |
84 # make sure rejected requests are not counted, and access is again allowed | |
85 # after 1/rate seconds | |
86 | |
87 like(http_get('/test1.html'), qr/^HTTP\/1.. 200 /m, 'rejects not counted'); | |
88 | |
89 # make sure negative excess values are handled properly | |
90 | |
91 http_get('/fast.html'); | |
92 select undef, undef, undef, 0.1; | |
93 like(http_get('/fast.html'), qr/^HTTP\/1.. 200 /m, 'negative excess'); | |
94 | |
77 ############################################################################### | 95 ############################################################################### |