Mercurial > hg > nginx-tests
comparison proxy_cache_lock.t @ 184:f432f11a3b12
Tests: proxy cache lock tests.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 08 Dec 2011 20:41:14 +0300 |
parents | |
children | bc6ecf23cc9c |
comparison
equal
deleted
inserted
replaced
183:e43af26ac6ea | 184:f432f11a3b12 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Tests for http proxy cache lock. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use Test::More; | |
13 use Socket qw/ CRLF /; | |
14 | |
15 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
16 | |
17 use lib 'lib'; | |
18 use Test::Nginx; | |
19 | |
20 ############################################################################### | |
21 | |
22 select STDERR; $| = 1; | |
23 select STDOUT; $| = 1; | |
24 | |
25 my $t = Test::Nginx->new()->has(qw/http proxy cache/) | |
26 ->write_file_expand('nginx.conf', <<'EOF'); | |
27 | |
28 %%TEST_GLOBALS%% | |
29 | |
30 daemon off; | |
31 | |
32 events { | |
33 } | |
34 | |
35 http { | |
36 %%TEST_GLOBALS_HTTP%% | |
37 | |
38 proxy_cache_path %%TESTDIR%%/cache levels=1:2 | |
39 keys_zone=NAME:10m; | |
40 | |
41 server { | |
42 listen 127.0.0.1:8080; | |
43 server_name localhost; | |
44 | |
45 location / { | |
46 proxy_pass http://127.0.0.1:8081; | |
47 proxy_cache NAME; | |
48 | |
49 proxy_cache_lock on; | |
50 } | |
51 | |
52 location /timeout { | |
53 proxy_pass http://127.0.0.1:8081; | |
54 proxy_cache NAME; | |
55 | |
56 proxy_cache_lock on; | |
57 proxy_cache_lock_timeout 300ms; | |
58 } | |
59 | |
60 location /nolock { | |
61 proxy_pass http://127.0.0.1:8081; | |
62 proxy_cache NAME; | |
63 } | |
64 } | |
65 } | |
66 | |
67 EOF | |
68 | |
69 $t->run_daemon(\&http_fake_daemon); | |
70 | |
71 eval { | |
72 open OLDERR, ">&", \*STDERR; close STDERR; | |
73 $t->run(); | |
74 open STDERR, ">&", \*OLDERR; | |
75 }; | |
76 plan(skip_all => 'no proxy_cache_lock') if $@; | |
77 | |
78 $t->plan(19); | |
79 | |
80 ############################################################################### | |
81 | |
82 # sequentional requests | |
83 | |
84 for my $i (1 .. 5) { | |
85 like(http_get('/seq'), qr/request 1/, 'sequentional request ' . $i); | |
86 } | |
87 | |
88 # parallel requests | |
89 | |
90 my @sockets; | |
91 | |
92 for my $i (1 .. 5) { | |
93 $sockets[$i] = http_start('/par1'); | |
94 } | |
95 | |
96 for my $i (1 .. 5) { | |
97 like(http_end($sockets[$i]), qr/request 1/, 'parallel request ' . $i); | |
98 } | |
99 | |
100 like(http_get('/par1'), qr/request 1/, 'first request cached'); | |
101 | |
102 # parallel requests with cache lock timeout | |
103 | |
104 for my $i (1 .. 3) { | |
105 $sockets[$i] = http_start('/timeout'); | |
106 } | |
107 | |
108 for my $i (1 .. 3) { | |
109 like(http_end($sockets[$i]), qr/request $i/, 'lock timeout ' . $i); | |
110 } | |
111 | |
112 like(http_get('/timeout'), qr/request 3/, 'lock timeout - last cached'); | |
113 | |
114 # no lock | |
115 | |
116 for my $i (1 .. 3) { | |
117 $sockets[$i] = http_start('/nolock'); | |
118 } | |
119 | |
120 for my $i (1 .. 3) { | |
121 like(http_end($sockets[$i]), qr/request $i/, 'nolock ' . $i); | |
122 } | |
123 | |
124 like(http_get('/nolock'), qr/request 3/, 'nolock - last cached'); | |
125 | |
126 ############################################################################### | |
127 | |
128 sub http_start { | |
129 my ($uri) = @_; | |
130 | |
131 my $s; | |
132 my $request = "GET $uri HTTP/1.0" . CRLF . CRLF; | |
133 | |
134 eval { | |
135 local $SIG{ALRM} = sub { die "timeout\n" }; | |
136 local $SIG{PIPE} = sub { die "sigpipe\n" }; | |
137 alarm(2); | |
138 $s = IO::Socket::INET->new( | |
139 Proto => 'tcp', | |
140 PeerAddr => '127.0.0.1:8080' | |
141 ); | |
142 log_out($request); | |
143 $s->print($request); | |
144 alarm(0); | |
145 }; | |
146 alarm(0); | |
147 if ($@) { | |
148 log_in("died: $@"); | |
149 return undef; | |
150 } | |
151 return $s; | |
152 } | |
153 | |
154 sub http_end { | |
155 my ($s) = @_; | |
156 my $reply; | |
157 | |
158 eval { | |
159 local $SIG{ALRM} = sub { die "timeout\n" }; | |
160 local $SIG{PIPE} = sub { die "sigpipe\n" }; | |
161 alarm(2); | |
162 local $/; | |
163 $reply = $s->getline(); | |
164 log_in($reply); | |
165 alarm(0); | |
166 }; | |
167 alarm(0); | |
168 if ($@) { | |
169 log_in("died: $@"); | |
170 return undef; | |
171 } | |
172 return $reply; | |
173 } | |
174 | |
175 ############################################################################### | |
176 | |
177 sub http_fake_daemon { | |
178 my $server = IO::Socket::INET->new( | |
179 Proto => 'tcp', | |
180 LocalAddr => '127.0.0.1:8081', | |
181 Listen => 1, | |
182 Reuse => 1 | |
183 ) | |
184 or die "Can't create listening socket: $!\n"; | |
185 | |
186 my $num = 0; | |
187 my $uri = ''; | |
188 | |
189 while (my $client = $server->accept()) { | |
190 $client->autoflush(1); | |
191 | |
192 while (<$client>) { | |
193 if (/GET (.*) HTTP/ && $1 ne $uri) { | |
194 $uri = $1; | |
195 $num = 0; | |
196 } | |
197 | |
198 $uri = $1 if /GET (.*) HTTP/; | |
199 last if /^\x0d?\x0a?$/; | |
200 } | |
201 | |
202 sleep(1); | |
203 | |
204 $num++; | |
205 print $client <<"EOF"; | |
206 HTTP/1.1 200 OK | |
207 Cache-Control: max-age=300 | |
208 Connection: close | |
209 | |
210 request $num | |
211 EOF | |
212 } | |
213 } | |
214 | |
215 ############################################################################### |