Mercurial > hg > ngx_http_upstream_keepalive
comparison t/fastcgi_keepalive.t @ 47:5cab730994f3 draft
Keepalive: rename tests.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Tue, 22 Jan 2013 19:05:49 +0400 |
parents | t/fastcgi-keepalive.t@e10649a96f39 |
children | 32e9e4b4b5e1 |
comparison
equal
deleted
inserted
replaced
46:92125e266aa4 | 47:5cab730994f3 |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Test for fastcgi backend with keepalive. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use Test::More; | |
13 use Test::Nginx; | |
14 | |
15 ############################################################################### | |
16 | |
17 select STDERR; $| = 1; | |
18 select STDOUT; $| = 1; | |
19 | |
20 my $t = Test::Nginx->new()->write_file_expand('nginx.conf', <<'EOF'); | |
21 | |
22 %%TEST_GLOBALS%% | |
23 | |
24 master_process off; | |
25 daemon off; | |
26 | |
27 events { | |
28 } | |
29 | |
30 http { | |
31 %%TEST_GLOBALS_HTTP%% | |
32 | |
33 upstream backend { | |
34 server 127.0.0.1:8081; | |
35 keepalive 1; | |
36 } | |
37 | |
38 server { | |
39 listen 127.0.0.1:8080; | |
40 server_name localhost; | |
41 | |
42 location / { | |
43 fastcgi_pass backend; | |
44 fastcgi_keep_conn on; | |
45 } | |
46 } | |
47 } | |
48 | |
49 EOF | |
50 | |
51 $t->run_daemon(\&fastcgi_test_daemon); | |
52 | |
53 eval { | |
54 open OLDERR, ">&", \*STDERR; close STDERR; | |
55 $t->run(); | |
56 open STDERR, ">&", \*OLDERR; | |
57 }; | |
58 plan(skip_all => 'no keepalive patches') if $@; | |
59 | |
60 $t->plan(6); | |
61 | |
62 ############################################################################### | |
63 | |
64 like(http_get('/'), qr/SEE-THIS/, 'fastcgi request'); | |
65 like(http_get('/redir'), qr/302/, 'fastcgi redirect'); | |
66 like(http_get('/'), qr/^request: 3$/m, 'fastcgi third request'); | |
67 | |
68 like(http_get('/single'), qr/^connection: 1$/m, 'single connection used'); | |
69 | |
70 # New connection to fastcgi application should be established after HEAD | |
71 # requests since nginx doesn't read whole response (as it doesn't need | |
72 # body). | |
73 | |
74 unlike(http_head('/head'), qr/SEE-THIS/, 'no data in HEAD'); | |
75 | |
76 like(http_get('/after'), qr/^connection: 2$/m, 'new connection after HEAD'); | |
77 | |
78 ############################################################################### | |
79 | |
80 # Simple FastCGI responder implementation. Unlike FCGI and FCGI::Async it's | |
81 # able to count connections. | |
82 | |
83 # http://www.fastcgi.com/devkit/doc/fcgi-spec.html | |
84 | |
85 sub fastcgi_read_record($) { | |
86 my ($socket) = @_; | |
87 | |
88 my ($n, $h, $header); | |
89 | |
90 $n = $socket->read($header, 8); | |
91 return undef if !defined $n or $n != 8; | |
92 | |
93 @{$h}{qw/ version type id clen plen /} = unpack("CCnnC", $header); | |
94 | |
95 $n = $socket->read($h->{content}, $h->{clen}); | |
96 return undef if $n != $h->{clen}; | |
97 | |
98 $n = $socket->read($h->{padding}, $h->{plen}); | |
99 return undef if $n != $h->{plen}; | |
100 | |
101 $h->{socket} = $socket; | |
102 return $h; | |
103 } | |
104 | |
105 sub fastcgi_respond($$) { | |
106 my ($h, $body) = @_; | |
107 | |
108 # stdout | |
109 $h->{socket}->write(pack("CCnnCx", $h->{version}, 6, $h->{id}, | |
110 length($body), 0)); | |
111 $h->{socket}->write($body); | |
112 | |
113 # write some text to stdout and stderr splitted over multiple network | |
114 # packets to test if we correctly set pipe length in various places | |
115 | |
116 my $tt = "test text, just for test"; | |
117 | |
118 $h->{socket}->write(pack("CCnnCx", $h->{version}, 6, $h->{id}, | |
119 length($tt . $tt), 0) . $tt); | |
120 select(undef, undef, undef, 0.1); | |
121 $h->{socket}->write($tt . pack("CC", $h->{version}, 7)); | |
122 select(undef, undef, undef, 0.1); | |
123 $h->{socket}->write(pack("nnCx", $h->{id}, length($tt), 0)); | |
124 $h->{socket}->write($tt); | |
125 | |
126 # close stdout | |
127 $h->{socket}->write(pack("CCnnCx", $h->{version}, 6, $h->{id}, 0, 0)); | |
128 | |
129 select(undef, undef, undef, 0.1); | |
130 | |
131 # end request | |
132 $h->{socket}->write(pack("CCnnCx", $h->{version}, 3, $h->{id}, 8, 0)); | |
133 $h->{socket}->write(pack("NCxxx", 0, 0)); | |
134 } | |
135 | |
136 sub fastcgi_test_daemon { | |
137 my $server = IO::Socket::INET->new( | |
138 Proto => 'tcp', | |
139 LocalAddr => '127.0.0.1:8081', | |
140 Listen => 5, | |
141 Reuse => 1 | |
142 ) | |
143 or die "Can't create listening socket: $!\n"; | |
144 | |
145 local $SIG{PIPE} = 'IGNORE'; | |
146 | |
147 my $ccount = 0; | |
148 my $rcount = 0; | |
149 | |
150 while (my $client = $server->accept()) { | |
151 $client->autoflush(1); | |
152 Test::Nginx::log_core('||', "fastcgi connection"); | |
153 | |
154 $ccount++; | |
155 | |
156 while (my $h = fastcgi_read_record($client)) { | |
157 Test::Nginx::log_core('||', "fastcgi record: " | |
158 . " $h->{version}, $h->{type}, $h->{id}, " | |
159 . "'$h->{content}'"); | |
160 | |
161 # skip everything unless stdin, then respond | |
162 next if $h->{type} != 5; | |
163 | |
164 $rcount++; | |
165 | |
166 # respond | |
167 fastcgi_respond($h, <<EOF); | |
168 Location: http://localhost:8080/redirect | |
169 Content-Type: text/html | |
170 | |
171 SEE-THIS | |
172 request: $rcount | |
173 connection: $ccount | |
174 EOF | |
175 } | |
176 | |
177 close $client; | |
178 } | |
179 } | |
180 | |
181 ############################################################################### |