From f9f8937057f8217a0f5f7d819ff6f6f3cede3a51 Mon Sep 17 00:00:00 2001 From: david Date: Fri, 16 Aug 2013 08:02:57 +0000 Subject: [PATCH] Remove hardcoded delay in favor of waiting for a LISTEN message. --- ncat/test/ncat-test.pl | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/ncat/test/ncat-test.pl b/ncat/test/ncat-test.pl index cd7f86559..79c87b248 100755 --- a/ncat/test/ncat-test.pl +++ b/ncat/test/ncat-test.pl @@ -50,10 +50,32 @@ sub ncat { return ($pid, *OUT, *IN, *ERR); } +sub wait_listen { + my $fh = shift; + my $timeout = shift || 0.3; + my $rd = ""; + vec($rd, fileno($fh), 1) = 1; + my $partial = ""; + for (;;) { + my ($n, $frag); + ($n, $timeout) = select($rd, undef, undef, $timeout); + last if $n == 0; + $n = sysread($fh, $frag, $BUFSIZ); + last if (not defined($n)) || $n == 0; + $partial = $partial . $frag; + while ($partial =~ /^(.*?)\n(.*)$/s) { + my $line = $1; + $partial = $2; + if ($line =~ /^NCAT TEST: LISTEN/) { + return; + } + } + } +} + sub ncat_server { - my @ret = ncat($HOST, $PORT, "-l", @_); - # Give it a moment to start up. - select(undef, undef, undef, 0.3); + my @ret = ncat($HOST, $PORT, "--test", "-l", @_); + wait_listen($ret[3]); return @ret; }