comparison lib/Test/Nginx.pm @ 289:cbd4f6eca676

Tests: check if started process is alive while starting nginx. This allows faster test execution in case of startup failures, e.g. due to configuration errors. Note that just adding waitpid() to waitforfile() causes hang on win32 in wait(). To fix this, wait() calls were changed to waitpid() with pid specified.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 27 May 2013 17:15:17 +0400
parents 8fcc46212e5e
children f781b087b7aa
comparison
equal deleted inserted replaced
288:56157712d744 289:cbd4f6eca676
19 19
20 ############################################################################### 20 ###############################################################################
21 21
22 use File::Temp qw/ tempdir /; 22 use File::Temp qw/ tempdir /;
23 use IO::Socket; 23 use IO::Socket;
24 use POSIX qw/ waitpid WNOHANG /;
24 use Socket qw/ CRLF /; 25 use Socket qw/ CRLF /;
25 use Test::More qw//; 26 use Test::More qw//;
26 27
27 ############################################################################### 28 ###############################################################################
28 29
190 or die "Unable to exec(): $!\n"; 191 or die "Unable to exec(): $!\n";
191 } 192 }
192 193
193 # wait for nginx to start 194 # wait for nginx to start
194 195
195 $self->waitforfile("$testdir/nginx.pid") 196 $self->waitforfile("$testdir/nginx.pid", $pid)
196 or die "Can't start nginx"; 197 or die "Can't start nginx";
197 198
198 $self->{_started} = 1; 199 $self->{_started} = 1;
199 return $self; 200 return $self;
200 } 201 }
201 202
202 sub waitforfile($) { 203 sub waitforfile($;$) {
203 my ($self, $file) = @_; 204 my ($self, $file, $pid) = @_;
205 my $exited;
204 206
205 # wait for file to appear 207 # wait for file to appear
208 # or specified process to exit
206 209
207 for (1 .. 30) { 210 for (1 .. 30) {
208 return 1 if -e $file; 211 return 1 if -e $file;
212 return 0 if $exited;
213 $exited = waitpid($pid, WNOHANG) != 0 if $pid;
209 select undef, undef, undef, 0.1; 214 select undef, undef, undef, 0.1;
210 } 215 }
211 216
212 return undef; 217 return undef;
213 } 218 }
233 238
234 sub stop() { 239 sub stop() {
235 my ($self) = @_; 240 my ($self) = @_;
236 241
237 return $self unless $self->{_started}; 242 return $self unless $self->{_started};
243
244 local $/;
245 open F, '<' . $self->{_testdir} . '/nginx.pid'
246 or die "Can't open nginx.pid: $!";
247 my $pid = <F>;
248 close F;
238 249
239 if ($^O eq 'MSWin32') { 250 if ($^O eq 'MSWin32') {
240 my $testdir = $self->{_testdir}; 251 my $testdir = $self->{_testdir};
241 my @globals = $self->{_test_globals} ? 252 my @globals = $self->{_test_globals} ?
242 () : ('-g', "pid $testdir/nginx.pid; " 253 () : ('-g', "pid $testdir/nginx.pid; "
244 system($NGINX, '-c', "$testdir/nginx.conf", '-s', 'stop', 255 system($NGINX, '-c', "$testdir/nginx.conf", '-s', 'stop',
245 @globals) == 0 256 @globals) == 0
246 or die "system() failed: $?\n"; 257 or die "system() failed: $?\n";
247 258
248 } else { 259 } else {
249 kill 'QUIT', `cat $self->{_testdir}/nginx.pid`; 260 kill 'QUIT', $pid;
250 } 261 }
251 262
252 wait; 263 waitpid($pid, 0);
253 264
254 $self->{_started} = 0; 265 $self->{_started} = 0;
255 266
256 return $self; 267 return $self;
257 } 268 }
260 my ($self) = @_; 271 my ($self) = @_;
261 272
262 while ($self->{_daemons} && scalar @{$self->{_daemons}}) { 273 while ($self->{_daemons} && scalar @{$self->{_daemons}}) {
263 my $p = shift @{$self->{_daemons}}; 274 my $p = shift @{$self->{_daemons}};
264 kill $^O eq 'MSWin32' ? 9 : 'TERM', $p; 275 kill $^O eq 'MSWin32' ? 9 : 'TERM', $p;
265 wait; 276 waitpid($p, 0);
266 } 277 }
267 278
268 return $self; 279 return $self;
269 } 280 }
270 281