Mercurial > hg > nginx-tests
comparison proxy_cache_lock.t @ 438:60888e2c3f5a
Tests: new http_start() and http_end() functions.
When used together, they allow to break an http request into two separate
send/receive phases and are used to run long requests asynchronously.
An http() "start" extra flag introduced as a convenience shortcut.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Fri, 18 Jul 2014 13:19:55 +0400 |
parents | 847ea345becb |
children | 13ffba66f4e3 |
comparison
equal
deleted
inserted
replaced
437:8b4a6b8691eb | 438:60888e2c3f5a |
---|---|
13 use Socket qw/ CRLF /; | 13 use Socket qw/ CRLF /; |
14 | 14 |
15 BEGIN { use FindBin; chdir($FindBin::Bin); } | 15 BEGIN { use FindBin; chdir($FindBin::Bin); } |
16 | 16 |
17 use lib 'lib'; | 17 use lib 'lib'; |
18 use Test::Nginx; | 18 use Test::Nginx qw/ :DEFAULT http_end /; |
19 | 19 |
20 ############################################################################### | 20 ############################################################################### |
21 | 21 |
22 select STDERR; $| = 1; | 22 select STDERR; $| = 1; |
23 select STDOUT; $| = 1; | 23 select STDOUT; $| = 1; |
85 # parallel requests | 85 # parallel requests |
86 | 86 |
87 my @sockets; | 87 my @sockets; |
88 | 88 |
89 for my $i (1 .. 5) { | 89 for my $i (1 .. 5) { |
90 $sockets[$i] = http_start('/par1'); | 90 $sockets[$i] = http_get('/par1', start => 1); |
91 } | 91 } |
92 | 92 |
93 for my $i (1 .. 5) { | 93 for my $i (1 .. 5) { |
94 like(http_end($sockets[$i]), qr/request 1/, 'parallel request ' . $i); | 94 like(http_end($sockets[$i]), qr/request 1/, 'parallel request ' . $i); |
95 } | 95 } |
97 like(http_get('/par1'), qr/request 1/, 'first request cached'); | 97 like(http_get('/par1'), qr/request 1/, 'first request cached'); |
98 | 98 |
99 # parallel requests with cache lock timeout | 99 # parallel requests with cache lock timeout |
100 | 100 |
101 for my $i (1 .. 3) { | 101 for my $i (1 .. 3) { |
102 $sockets[$i] = http_start('/timeout'); | 102 $sockets[$i] = http_get('/timeout', start => 1); |
103 } | 103 } |
104 | 104 |
105 for my $i (1 .. 3) { | 105 for my $i (1 .. 3) { |
106 like(http_end($sockets[$i]), qr/request $i/, 'lock timeout ' . $i); | 106 like(http_end($sockets[$i]), qr/request $i/, 'lock timeout ' . $i); |
107 } | 107 } |
109 like(http_get('/timeout'), qr/request 3/, 'lock timeout - last cached'); | 109 like(http_get('/timeout'), qr/request 3/, 'lock timeout - last cached'); |
110 | 110 |
111 # no lock | 111 # no lock |
112 | 112 |
113 for my $i (1 .. 3) { | 113 for my $i (1 .. 3) { |
114 $sockets[$i] = http_start('/nolock'); | 114 $sockets[$i] = http_get('/nolock', start => 1); |
115 } | 115 } |
116 | 116 |
117 for my $i (1 .. 3) { | 117 for my $i (1 .. 3) { |
118 like(http_end($sockets[$i]), qr/request $i/, 'nolock ' . $i); | 118 like(http_end($sockets[$i]), qr/request $i/, 'nolock ' . $i); |
119 } | 119 } |
120 | 120 |
121 like(http_get('/nolock'), qr/request 3/, 'nolock - last cached'); | 121 like(http_get('/nolock'), qr/request 3/, 'nolock - last cached'); |
122 | 122 |
123 ############################################################################### | 123 ############################################################################### |
124 | |
125 sub http_start { | |
126 my ($uri) = @_; | |
127 | |
128 my $s; | |
129 my $request = "GET $uri HTTP/1.0" . CRLF . CRLF; | |
130 | |
131 eval { | |
132 local $SIG{ALRM} = sub { die "timeout\n" }; | |
133 local $SIG{PIPE} = sub { die "sigpipe\n" }; | |
134 alarm(3); | |
135 $s = IO::Socket::INET->new( | |
136 Proto => 'tcp', | |
137 PeerAddr => '127.0.0.1:8080' | |
138 ); | |
139 log_out($request); | |
140 $s->print($request); | |
141 alarm(0); | |
142 }; | |
143 alarm(0); | |
144 if ($@) { | |
145 log_in("died: $@"); | |
146 return undef; | |
147 } | |
148 return $s; | |
149 } | |
150 | |
151 sub http_end { | |
152 my ($s) = @_; | |
153 my $reply; | |
154 | |
155 eval { | |
156 local $SIG{ALRM} = sub { die "timeout\n" }; | |
157 local $SIG{PIPE} = sub { die "sigpipe\n" }; | |
158 alarm(3); | |
159 local $/; | |
160 $reply = $s->getline(); | |
161 log_in($reply); | |
162 alarm(0); | |
163 }; | |
164 alarm(0); | |
165 if ($@) { | |
166 log_in("died: $@"); | |
167 return undef; | |
168 } | |
169 return $reply; | |
170 } | |
171 | 124 |
172 ############################################################################### | 125 ############################################################################### |
173 | 126 |
174 sub http_fake_daemon { | 127 sub http_fake_daemon { |
175 my $server = IO::Socket::INET->new( | 128 my $server = IO::Socket::INET->new( |