changeset 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 56157712d744
children f781b087b7aa
files lib/Test/Nginx.pm
diffstat 1 files changed, 17 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/lib/Test/Nginx.pm
+++ b/lib/Test/Nginx.pm
@@ -21,6 +21,7 @@ our %EXPORT_TAGS = (
 
 use File::Temp qw/ tempdir /;
 use IO::Socket;
+use POSIX qw/ waitpid WNOHANG /;
 use Socket qw/ CRLF /;
 use Test::More qw//;
 
@@ -192,20 +193,24 @@ sub run(;$) {
 
 	# wait for nginx to start
 
-	$self->waitforfile("$testdir/nginx.pid")
+	$self->waitforfile("$testdir/nginx.pid", $pid)
 		or die "Can't start nginx";
 
 	$self->{_started} = 1;
 	return $self;
 }
 
-sub waitforfile($) {
-	my ($self, $file) = @_;
+sub waitforfile($;$) {
+	my ($self, $file, $pid) = @_;
+	my $exited;
 
 	# wait for file to appear
+	# or specified process to exit
 
 	for (1 .. 30) {
 		return 1 if -e $file;
+		return 0 if $exited;
+		$exited = waitpid($pid, WNOHANG) != 0 if $pid;
 		select undef, undef, undef, 0.1;
 	}
 
@@ -236,6 +241,12 @@ sub stop() {
 
 	return $self unless $self->{_started};
 
+	local $/;
+	open F, '<' . $self->{_testdir} . '/nginx.pid'
+		or die "Can't open nginx.pid: $!";
+	my $pid = <F>;
+	close F;
+
 	if ($^O eq 'MSWin32') {
 		my $testdir = $self->{_testdir};
 		my @globals = $self->{_test_globals} ?
@@ -246,10 +257,10 @@ sub stop() {
 			or die "system() failed: $?\n";
 
 	} else {
-		kill 'QUIT', `cat $self->{_testdir}/nginx.pid`;
+		kill 'QUIT', $pid;
 	}
 
-	wait;
+	waitpid($pid, 0);
 
 	$self->{_started} = 0;
 
@@ -262,7 +273,7 @@ sub stop_daemons() {
 	while ($self->{_daemons} && scalar @{$self->{_daemons}}) {
 		my $p = shift @{$self->{_daemons}};
 		kill $^O eq 'MSWin32' ? 9 : 'TERM', $p;
-		wait;
+		waitpid($p, 0);
 	}
 
 	return $self;