changeset 50:b494fe5b12d1

Tests: add waitforfile() and waitforsocket() functions. This functions add ability to wait for external daemons to start before running tests.
author Maxim Dounin <mdounin@mdounin.ru>
date Wed, 19 Nov 2008 17:31:03 +0300
parents b01ae969181b
children d59ae7bab0a6
files lib/Test/Nginx.pm memcached.t
diffstat 2 files changed, 37 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/lib/Test/Nginx.pm
+++ b/lib/Test/Nginx.pm
@@ -98,17 +98,45 @@ sub run(;$) {
 
 	# wait for nginx to start
 
-	for (1 .. 30) {
-		select undef, undef, undef, 0.05;
-		last if -e "$self->{_testdir}/nginx.pid";
-	}
-
-	die "Can't start nginx" unless -e "$self->{_testdir}/nginx.pid";
+	$self->waitforfile("$testdir/nginx.pid")
+		or die "Can't start nginx";
 
 	$self->{_started} = 1;
 	return $self;
 }
 
+sub waitforfile($) {
+	my ($self, $file) = @_;
+
+	# wait for file to appear
+
+	for (1 .. 30) {
+		return 1 if -e $file;
+		select undef, undef, undef, 0.1;
+	}
+
+	return undef;
+}
+
+sub waitforsocket($) {
+	my ($self, $peer) = @_;
+
+	# wait for socket to accept connections
+
+	for (1 .. 30) {
+		my $s = IO::Socket::INET->new(
+			Proto => 'tcp',
+			PeerAddr => $peer
+		);
+
+		return 1 if defined $s;
+
+		select undef, undef, undef, 0.1;
+	}
+
+	return undef;
+}
+
 sub stop() {
 	my ($self) = @_;
 
--- a/memcached.t
+++ b/memcached.t
@@ -63,6 +63,9 @@ EOF
 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081');
 $t->run();
 
+$t->waitforsocket('127.0.0.1:8081')
+	or die "Can't start memcached";
+
 ###############################################################################
 
 my $memd = Cache::Memcached->new(servers => [ '127.0.0.1:8081' ]);