blob: 6c44bc0904a074d7855b520e0c3e94d36d8f7bb7 [file] [log] [blame]
Maxim Dounin7eb317e2011-12-08 20:41:14 +03001#!/usr/bin/perl
2
3# (C) Maxim Dounin
4
5# Tests for http proxy cache lock.
6
7###############################################################################
8
9use warnings;
10use strict;
11
12use Test::More;
13use Socket qw/ CRLF /;
14
15BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17use lib 'lib';
18use Test::Nginx;
19
20###############################################################################
21
22select STDERR; $| = 1;
23select STDOUT; $| = 1;
24
25my $t = Test::Nginx->new()->has(qw/http proxy cache/)
26 ->write_file_expand('nginx.conf', <<'EOF');
27
28%%TEST_GLOBALS%%
29
30daemon off;
31
32events {
33}
34
35http {
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
67EOF
68
69$t->run_daemon(\&http_fake_daemon);
70
71eval {
72 open OLDERR, ">&", \*STDERR; close STDERR;
73 $t->run();
74 open STDERR, ">&", \*OLDERR;
75};
76plan(skip_all => 'no proxy_cache_lock') if $@;
77
78$t->plan(19);
79
80###############################################################################
81
82# sequentional requests
83
84for my $i (1 .. 5) {
85 like(http_get('/seq'), qr/request 1/, 'sequentional request ' . $i);
86}
87
88# parallel requests
89
90my @sockets;
91
92for my $i (1 .. 5) {
93 $sockets[$i] = http_start('/par1');
94}
95
96for my $i (1 .. 5) {
97 like(http_end($sockets[$i]), qr/request 1/, 'parallel request ' . $i);
98}
99
100like(http_get('/par1'), qr/request 1/, 'first request cached');
101
102# parallel requests with cache lock timeout
103
104for my $i (1 .. 3) {
105 $sockets[$i] = http_start('/timeout');
106}
107
108for my $i (1 .. 3) {
109 like(http_end($sockets[$i]), qr/request $i/, 'lock timeout ' . $i);
110}
111
112like(http_get('/timeout'), qr/request 3/, 'lock timeout - last cached');
113
114# no lock
115
116for my $i (1 .. 3) {
117 $sockets[$i] = http_start('/nolock');
118}
119
120for my $i (1 .. 3) {
121 like(http_end($sockets[$i]), qr/request $i/, 'nolock ' . $i);
122}
123
124like(http_get('/nolock'), qr/request 3/, 'nolock - last cached');
125
126###############################################################################
127
128sub 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
154sub 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
177sub 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";
206HTTP/1.1 200 OK
207Cache-Control: max-age=300
208Connection: close
209
210request $num
211EOF
212 }
213}
214
215###############################################################################