comparison proxy_cache_use_stale.t @ 1178:4039b6b3a75a

Tests: added tests for background subrequests for cache updates. The tests ensure that a stale response is not delayed by background update in case the response was not sent to the client in one filter chain call.
author Sergey Kandaurov <pluknet@nginx.com>
date Tue, 30 May 2017 17:05:44 +0300
parents d6acd17ca4e3
children 660147bfe68c
comparison
equal deleted inserted replaced
1177:382011b269f1 1178:4039b6b3a75a
10 use warnings; 10 use warnings;
11 use strict; 11 use strict;
12 12
13 use Test::More; 13 use Test::More;
14 14
15 use IO::Select;
16
15 BEGIN { use FindBin; chdir($FindBin::Bin); } 17 BEGIN { use FindBin; chdir($FindBin::Bin); }
16 18
17 use lib 'lib'; 19 use lib 'lib';
18 use Test::Nginx qw/ :DEFAULT http_end /; 20 use Test::Nginx qw/ :DEFAULT http_end /;
19 21
20 ############################################################################### 22 ###############################################################################
21 23
22 select STDERR; $| = 1; 24 select STDERR; $| = 1;
23 select STDOUT; $| = 1; 25 select STDOUT; $| = 1;
24 26
25 my $t = Test::Nginx->new()->has(qw/http proxy cache rewrite limit_req/) 27 my $t = Test::Nginx->new()->has(qw/http proxy cache rewrite limit_req ssi/)
26 ->write_file_expand('nginx.conf', <<'EOF'); 28 ->write_file_expand('nginx.conf', <<'EOF');
27 29
28 %%TEST_GLOBALS%% 30 %%TEST_GLOBALS%%
29 31
30 daemon off; 32 daemon off;
41 43
42 server { 44 server {
43 listen 127.0.0.1:8080; 45 listen 127.0.0.1:8080;
44 server_name localhost; 46 server_name localhost;
45 47
48 location /ssi.html {
49 ssi on;
50 sendfile_max_chunk 4k;
51 }
52
46 location / { 53 location / {
47 proxy_pass http://127.0.0.1:8081; 54 proxy_pass http://127.0.0.1:8081;
48 55
49 proxy_cache NAME; 56 proxy_cache NAME;
50 57
70 77
71 location /updating/ { 78 location /updating/ {
72 proxy_pass http://127.0.0.1:8081/; 79 proxy_pass http://127.0.0.1:8081/;
73 80
74 proxy_cache_use_stale updating; 81 proxy_cache_use_stale updating;
82 }
83
84 location /t7.html {
85 proxy_pass http://127.0.0.1:8081;
86
87 sendfile_max_chunk 4k;
75 } 88 }
76 89
77 location /t8.html { 90 location /t8.html {
78 proxy_pass http://127.0.0.1:8081/t.html; 91 proxy_pass http://127.0.0.1:8081/t.html;
79 92
89 listen 127.0.0.1:8081; 102 listen 127.0.0.1:8081;
90 server_name localhost; 103 server_name localhost;
91 104
92 add_header Cache-Control $http_x_cache_control; 105 add_header Cache-Control $http_x_cache_control;
93 106
107 if ($arg_lim) {
108 set $limit_rate 1k;
109 }
110
94 if ($arg_e) { 111 if ($arg_e) {
95 return 500; 112 return 500;
96 } 113 }
97 114
98 location / { } 115 location / { }
99 116
100 location /t6.html { 117 location /t6.html {
101 limit_req zone=one burst=2; 118 limit_req zone=one burst=2;
119 }
120
121 location /t9.html {
122 add_header Cache-Control "max-age=1, stale-while-revalidate=10";
102 } 123 }
103 } 124 }
104 } 125 }
105 126
106 EOF 127 EOF
108 $t->write_file('t.html', 'SEE-THIS'); 129 $t->write_file('t.html', 'SEE-THIS');
109 $t->write_file('tt.html', 'SEE-THIS'); 130 $t->write_file('tt.html', 'SEE-THIS');
110 $t->write_file('t2.html', 'SEE-THIS'); 131 $t->write_file('t2.html', 'SEE-THIS');
111 $t->write_file('t3.html', 'SEE-THIS'); 132 $t->write_file('t3.html', 'SEE-THIS');
112 $t->write_file('t6.html', 'SEE-THIS'); 133 $t->write_file('t6.html', 'SEE-THIS');
113 134 $t->write_file('t7.html', 'SEE-THIS' x 1024);
114 $t->try_run('no proxy_cache_background_update')->plan(27); 135 $t->write_file('t9.html', 'SEE-THIS' x 1024);
136 $t->write_file('ssi.html', 'xxx <!--#include virtual="/t9.html" --> xxx');
137
138 $t->try_run('no proxy_cache_background_update')->plan(29);
115 139
116 ############################################################################### 140 ###############################################################################
117 141
118 like(get('/t.html', 'max-age=1, stale-if-error=5'), qr/MISS/, 'stale-if-error'); 142 like(get('/t.html', 'max-age=1, stale-if-error=5'), qr/MISS/, 'stale-if-error');
119 like(http_get('/t.html?e=1'), qr/HIT/, 's-i-e - cached'); 143 like(http_get('/t.html?e=1'), qr/HIT/, 's-i-e - cached');
125 get('/tt.html', 'max-age=1, stale-if-error=2'); 149 get('/tt.html', 'max-age=1, stale-if-error=2');
126 get('/t3.html', 'max-age=1, stale-while-revalidate=2'); 150 get('/t3.html', 'max-age=1, stale-while-revalidate=2');
127 get('/t4.html', 'max-age=1, stale-while-revalidate=2'); 151 get('/t4.html', 'max-age=1, stale-while-revalidate=2');
128 get('/t5.html', 'max-age=1, stale-while-revalidate=2'); 152 get('/t5.html', 'max-age=1, stale-while-revalidate=2');
129 get('/t6.html', 'max-age=1, stale-while-revalidate=2'); 153 get('/t6.html', 'max-age=1, stale-while-revalidate=2');
154 get('/t7.html', 'max-age=1, stale-while-revalidate=10');
155 http_get('/ssi.html');
130 get('/updating/t.html', 'max-age=1'); 156 get('/updating/t.html', 'max-age=1');
131 get('/updating/t2.html', 'max-age=1, stale-while-revalidate=2'); 157 get('/updating/t2.html', 'max-age=1, stale-while-revalidate=2');
132 get('/t8.html', 'stale-while-revalidate=10'); 158 get('/t8.html', 'stale-while-revalidate=10');
133 159
134 sleep 2; 160 sleep 2;
176 like(http_get('/t2.html?e=1'), qr/STALE/, 's-w-r - stale after revalidate'); 202 like(http_get('/t2.html?e=1'), qr/STALE/, 's-w-r - stale after revalidate');
177 like(http_get('/t3.html?e=1'), qr/ 500 /, 's-w-r - ceased'); 203 like(http_get('/t3.html?e=1'), qr/ 500 /, 's-w-r - ceased');
178 like(http_get('/tt.html?e=1'), qr/ 500 /, 's-i-e - ceased'); 204 like(http_get('/tt.html?e=1'), qr/ 500 /, 's-i-e - ceased');
179 like(http_get('/updating/t2.html'), qr/STALE/, 205 like(http_get('/updating/t2.html'), qr/STALE/,
180 's-w-r - overriden with use_stale updating'); 206 's-w-r - overriden with use_stale updating');
207
208 # stale response not blocked by background update.
209 # before 1.13.1, if stale response was not sent in one pass, its remaining
210 # part was blocked and not sent until background update has been finished
211
212 TODO: {
213 local $TODO = 'not yet' unless $t->has_version('1.13.1');
214
215 $t->write_file('t7.html', 'SEE-THAT' x 1024);
216
217 my $r = read_all(get('/t7.html?lim=1', 'max-age=1', start => 1));
218 like($r, qr/STALE.*^(SEE-THIS){1024}$/ms, 's-w-r - stale response not blocked');
219
220 $t->write_file('t9.html', 'SEE-THAT' x 1024);
221 $t->write_file('ssi.html', 'xxx <!--#include virtual="/t9.html?lim=1" --> xxx');
222
223 $r = read_all(http_get('/ssi.html', start => 1));
224 like($r, qr/^xxx (SEE-THIS){1024} xxx$/ms, 's-w-r - not blocked in subrequest');
225
226 }
181 227
182 # due to the missing content_handler inheritance in a cloned subrequest, 228 # due to the missing content_handler inheritance in a cloned subrequest,
183 # this used to access a static file in the update request 229 # this used to access a static file in the update request
184 230
185 like(http_get('/t2.html?if=1'), qr/STALE/, 'background update in if'); 231 like(http_get('/t2.html?if=1'), qr/STALE/, 'background update in if');
202 X-Cache-Control: $extra 248 X-Cache-Control: $extra
203 249
204 EOF 250 EOF
205 } 251 }
206 252
207 ############################################################################### 253 # background update is known to postpone closing connection with client
254
255 sub read_all {
256 my ($s) = @_;
257 my $r = '';
258 while (IO::Select->new($s)->can_read(1)) {
259 $s->sysread(my $buf, 8192) or last;
260 log_in($buf);
261 $r .= $buf;
262 }
263 return $r;
264 }
265
266 ###############################################################################