Mercurial > hg > nginx-tests
comparison proxy_cache_lock_age.t @ 501:0ee2899fbe3e
Tests: proxy_cache_lock_age tests.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Mon, 24 Nov 2014 17:01:25 +0300 |
parents | |
children | 071e8941e3bf |
comparison
equal
deleted
inserted
replaced
500:b4d657ba1a62 | 501:0ee2899fbe3e |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Tests for http proxy cache lock aged. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 use IO::Select; | |
16 | |
17 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
18 | |
19 use lib 'lib'; | |
20 use Test::Nginx; | |
21 | |
22 ############################################################################### | |
23 | |
24 select STDERR; $| = 1; | |
25 select STDOUT; $| = 1; | |
26 | |
27 plan(skip_all => 'win32') if $^O eq 'MSWin32'; | |
28 | |
29 my $t = Test::Nginx->new()->has(qw/http proxy cache/) | |
30 ->write_file_expand('nginx.conf', <<'EOF'); | |
31 | |
32 %%TEST_GLOBALS%% | |
33 | |
34 daemon off; | |
35 | |
36 events { | |
37 } | |
38 | |
39 http { | |
40 %%TEST_GLOBALS_HTTP%% | |
41 | |
42 proxy_cache_path %%TESTDIR%%/cache levels=1:2 | |
43 keys_zone=NAME:10m; | |
44 | |
45 server { | |
46 listen 127.0.0.1:8080; | |
47 server_name localhost; | |
48 | |
49 location / { | |
50 proxy_pass http://127.0.0.1:8081; | |
51 proxy_cache NAME; | |
52 | |
53 proxy_cache_lock on; | |
54 proxy_cache_lock_age 100ms; | |
55 } | |
56 } | |
57 } | |
58 | |
59 EOF | |
60 | |
61 $t->run_daemon(\&http_daemon, 8081); | |
62 | |
63 $t->try_run('no proxy_cache_lock_age')->plan(2); | |
64 | |
65 $t->waitforsocket('127.0.0.1:8081'); | |
66 | |
67 ############################################################################### | |
68 | |
69 my $s = http_get('/', start => 1); | |
70 | |
71 like(http_get('/'), qr/request 2/, 'request'); | |
72 like(http_get('/'), qr/request 2/, 'request cached'); | |
73 | |
74 http_get('/close'); | |
75 | |
76 ############################################################################### | |
77 | |
78 sub http_daemon { | |
79 my (@ports) = @_; | |
80 my @socks; | |
81 | |
82 for my $port (@ports) { | |
83 my $server = IO::Socket::INET->new( | |
84 Proto => 'tcp', | |
85 LocalHost => "127.0.0.1:$port", | |
86 Listen => 5, | |
87 Reuse => 1 | |
88 ) | |
89 or die "Can't create listening socket: $!\n"; | |
90 push @socks, $server; | |
91 } | |
92 | |
93 my $sel = IO::Select->new(@socks); | |
94 my $num = 0; | |
95 my $s; | |
96 | |
97 local $SIG{PIPE} = 'IGNORE'; | |
98 | |
99 while (my @ready = $sel->can_read) { | |
100 foreach my $fh (@ready) { | |
101 if (grep $_ == $fh, @socks) { | |
102 my $new = $fh->accept; | |
103 $new->autoflush(1); | |
104 $sel->add($new); | |
105 | |
106 } elsif (process_socket($fh, \$num, \$s)) { | |
107 $sel->remove($fh); | |
108 $fh->close; | |
109 } | |
110 } | |
111 } | |
112 } | |
113 | |
114 # Returns true to close connection | |
115 | |
116 sub process_socket { | |
117 my ($client, $num, $s) = @_; | |
118 | |
119 my $headers = ''; | |
120 my $uri = ''; | |
121 | |
122 while (<$client>) { | |
123 $headers .= $_; | |
124 last if (/^\x0d?\x0a?$/); | |
125 } | |
126 return 1 if $headers eq ''; | |
127 | |
128 $uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i; | |
129 return 1 if $uri eq ''; | |
130 | |
131 # finish a previously saved socket | |
132 close $$s if $uri eq '/close'; | |
133 | |
134 $$num++; | |
135 | |
136 print $client <<EOF; | |
137 HTTP/1.1 200 OK | |
138 Cache-Control: max-age=300 | |
139 Connection: close | |
140 | |
141 request $$num | |
142 EOF | |
143 | |
144 # save socket and wait | |
145 if ($$num == 1) { | |
146 $$s = $client; | |
147 return 0; | |
148 } | |
149 | |
150 return 1; | |
151 } | |
152 | |
153 ############################################################################### |