blob: ffc16795f81e6f37cfa11b8faf9b61a42c1db809 [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
Maxim Douninb01ea372012-06-03 05:20:39 +040025plan(skip_all => 'win32') if $^O eq 'MSWin32';
26
Maxim Dounin7eb317e2011-12-08 20:41:14 +030027my $t = Test::Nginx->new()->has(qw/http proxy cache/)
28 ->write_file_expand('nginx.conf', <<'EOF');
29
30%%TEST_GLOBALS%%
31
32daemon off;
33
34events {
35}
36
37http {
38 %%TEST_GLOBALS_HTTP%%
39
40 proxy_cache_path %%TESTDIR%%/cache levels=1:2
41 keys_zone=NAME:10m;
42
43 server {
44 listen 127.0.0.1:8080;
45 server_name localhost;
46
47 location / {
48 proxy_pass http://127.0.0.1:8081;
49 proxy_cache NAME;
50
51 proxy_cache_lock on;
52 }
53
54 location /timeout {
55 proxy_pass http://127.0.0.1:8081;
56 proxy_cache NAME;
57
58 proxy_cache_lock on;
Maxim Douninaad5f812012-09-10 20:18:05 +040059 proxy_cache_lock_timeout 200ms;
Maxim Dounin7eb317e2011-12-08 20:41:14 +030060 }
61
62 location /nolock {
63 proxy_pass http://127.0.0.1:8081;
64 proxy_cache NAME;
65 }
66 }
67}
68
69EOF
70
71$t->run_daemon(\&http_fake_daemon);
72
73eval {
74 open OLDERR, ">&", \*STDERR; close STDERR;
75 $t->run();
76 open STDERR, ">&", \*OLDERR;
77};
78plan(skip_all => 'no proxy_cache_lock') if $@;
79
80$t->plan(19);
Maxim Dounin64549ca2012-03-23 20:28:26 +040081$t->waitforsocket('127.0.0.1:8081');
Maxim Dounin7eb317e2011-12-08 20:41:14 +030082
83###############################################################################
84
85# sequentional requests
86
87for my $i (1 .. 5) {
88 like(http_get('/seq'), qr/request 1/, 'sequentional request ' . $i);
89}
90
91# parallel requests
92
93my @sockets;
94
95for my $i (1 .. 5) {
96 $sockets[$i] = http_start('/par1');
97}
98
99for my $i (1 .. 5) {
100 like(http_end($sockets[$i]), qr/request 1/, 'parallel request ' . $i);
101}
102
103like(http_get('/par1'), qr/request 1/, 'first request cached');
104
105# parallel requests with cache lock timeout
106
107for my $i (1 .. 3) {
108 $sockets[$i] = http_start('/timeout');
109}
110
111for my $i (1 .. 3) {
112 like(http_end($sockets[$i]), qr/request $i/, 'lock timeout ' . $i);
113}
114
115like(http_get('/timeout'), qr/request 3/, 'lock timeout - last cached');
116
117# no lock
118
119for my $i (1 .. 3) {
120 $sockets[$i] = http_start('/nolock');
121}
122
123for my $i (1 .. 3) {
124 like(http_end($sockets[$i]), qr/request $i/, 'nolock ' . $i);
125}
126
127like(http_get('/nolock'), qr/request 3/, 'nolock - last cached');
128
129###############################################################################
130
131sub http_start {
132 my ($uri) = @_;
133
134 my $s;
135 my $request = "GET $uri HTTP/1.0" . CRLF . CRLF;
136
137 eval {
138 local $SIG{ALRM} = sub { die "timeout\n" };
139 local $SIG{PIPE} = sub { die "sigpipe\n" };
Maxim Douninaad5f812012-09-10 20:18:05 +0400140 alarm(3);
Maxim Dounin7eb317e2011-12-08 20:41:14 +0300141 $s = IO::Socket::INET->new(
142 Proto => 'tcp',
143 PeerAddr => '127.0.0.1:8080'
144 );
145 log_out($request);
146 $s->print($request);
147 alarm(0);
148 };
149 alarm(0);
150 if ($@) {
151 log_in("died: $@");
152 return undef;
153 }
154 return $s;
155}
156
157sub http_end {
158 my ($s) = @_;
159 my $reply;
160
161 eval {
162 local $SIG{ALRM} = sub { die "timeout\n" };
163 local $SIG{PIPE} = sub { die "sigpipe\n" };
Maxim Douninaad5f812012-09-10 20:18:05 +0400164 alarm(3);
Maxim Dounin7eb317e2011-12-08 20:41:14 +0300165 local $/;
166 $reply = $s->getline();
167 log_in($reply);
168 alarm(0);
169 };
170 alarm(0);
171 if ($@) {
172 log_in("died: $@");
173 return undef;
174 }
175 return $reply;
176}
177
178###############################################################################
179
180sub http_fake_daemon {
181 my $server = IO::Socket::INET->new(
182 Proto => 'tcp',
183 LocalAddr => '127.0.0.1:8081',
Maxim Douninb39769f2011-12-26 14:49:21 +0300184 Listen => 5,
Maxim Dounin7eb317e2011-12-08 20:41:14 +0300185 Reuse => 1
186 )
187 or die "Can't create listening socket: $!\n";
188
189 my $num = 0;
190 my $uri = '';
191
192 while (my $client = $server->accept()) {
193 $client->autoflush(1);
194
195 while (<$client>) {
196 if (/GET (.*) HTTP/ && $1 ne $uri) {
197 $uri = $1;
198 $num = 0;
199 }
Maxim Dounin57d784c2012-03-23 20:24:13 +0400200
Maxim Dounin7eb317e2011-12-08 20:41:14 +0300201 $uri = $1 if /GET (.*) HTTP/;
202 last if /^\x0d?\x0a?$/;
203 }
204
Maxim Dounin64549ca2012-03-23 20:28:26 +0400205 next unless $uri;
206
Maxim Douninaad5f812012-09-10 20:18:05 +0400207 select(undef, undef, undef, 0.5);
Maxim Dounin7eb317e2011-12-08 20:41:14 +0300208
209 $num++;
210 print $client <<"EOF";
211HTTP/1.1 200 OK
212Cache-Control: max-age=300
213Connection: close
214
215request $num
216EOF
217 }
218}
219
220###############################################################################