commit fc10c5558d5f10b160ecef56c57a77740e0651b1
Author: Jim Meyering <meyering(a)redhat.com>
Date: Fri Mar 9 18:32:04 2012 +0100
tests: avoid spurious test failure due to port-in-use
* t/init.cfg (_port_in_use): New function.
(get_port): Don't blindly use the next port. That would fail if it
happens to be in use by some other process not related to our tests.
Instead, use _port_in_use to test it.
t/init.cfg | 23 ++++++++++++++++++++---
1 files changed, 20 insertions(+), 3 deletions(-)
---
diff --git a/t/init.cfg b/t/init.cfg
index 2539823..f965c12 100644
--- a/t/init.cfg
+++ b/t/init.cfg
@@ -145,6 +145,15 @@ emit_bucket_list()
fi
}
+# See if someone is listening on the specified port.
+# If so, exit successfully (0). Otherwise, exit nonzero.
+_port_in_use()
+{
+ perl -MIO::Socket -e \
+ 'exit !
IO::Socket::INET->new("PeerAddr"=>"localhost:'"$1"'",
\
+ "Timeout"=>1,
"Proto"=>"tcp")'
+}
+
# Each test script uses its own mongod instance as well as at least one
# iwhd process. Each of those needs a unique port number, so that the
# tests may be run in parallel without interfering with one another.
@@ -152,7 +161,9 @@ emit_bucket_list()
# Inputs:
# starting_port: start with this port number
# dir_prefix (we mkdir $dir_prefix$i for some $i >= $starting_port)
-# Return the first PID >= $starting_port for which mkdir $dir_prefix$i succeeds
+# Return the first P >= $starting_port for which mkdir $dir_prefix$i
+# succeeds and for which a quick connection test of localhost:$P suggests
+# the port is not in use.
get_port()
{
local starting_port=$1
@@ -160,8 +171,14 @@ get_port()
local i=0
while :; do
local p=$((starting_port + i))
- mkdir $dir_prefix$p 2>/dev/null \
- && { echo $p; echo $$ > $dir_prefix$p/pid; break; }
+ if mkdir $dir_prefix$p 2>/dev/null; then
+ if _port_in_use $p; then
+ echo in-use-by-other > $dir_prefix$p/pid
+ warn_ "INFO: port $p appears to be in use; skipping it"
+ else
+ echo $$ > $dir_prefix$p/pid; echo $p; break
+ fi
+ fi
i=$((i+1))
done
}
Show replies by date