From 9f9fb0c9ff11f3dc8a6846e647fe831ec91e6254 Mon Sep 17 00:00:00 2001 From: Daiki Ueno Date: Sat, 26 Sep 2020 11:58:17 +0200 Subject: tests: create lock for tests using GETPORT This fixes a race condition in the timings between when a free port is detected and when the port is actually used. Signed-off-by: Daiki Ueno --- tests/Makefile.am | 2 +- tests/scripts/common.sh | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/tests/Makefile.am b/tests/Makefile.am index ec5ec3f505..d4867cf3af 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -620,4 +620,4 @@ LOG_COMPILER = $(VALGRIND) endif distclean-local: - rm -rf softhsm-*.db softhsm-*.config *.tmp tmp-* x509-crt-list-import-url.config.db + rm -rf softhsm-*.db softhsm-*.config *.tmp tmp-* x509-crt-list-import-url.config.db port.lock.d diff --git a/tests/scripts/common.sh b/tests/scripts/common.sh index b9ed7eff99..396df0bfd1 100644 --- a/tests/scripts/common.sh +++ b/tests/scripts/common.sh @@ -66,8 +66,27 @@ check_if_port_listening() { $PFCMD -anl|grep "[\:\.]$PORT"|grep LISTEN >/dev/null 2>&1 } +trap "rmdir \"$top_builddir/tests/port.lock.d\" > /dev/null 2>&1 || :" 1 15 2 + +obtain_port_lock() +{ + local i + for i in 1 2 3 4 5 6; do + if mkdir "$top_builddir/tests/port.lock.d" > /dev/null 2>&1; then + return + fi + echo "try $i: obtaining port lock" + sleep 2 + done + return 1 +} + # Find a port number not currently in use. GETPORT=' + obtain_port_lock() + if $? -ne 0; then + echo "failed to obtain port lock: continuing anyway" + fi rc=0 unset myrandom while test $rc = 0; do @@ -136,7 +155,7 @@ wait_for_port() ret=$? if test $ret != 0;then check_if_port_in_use ${PORT} - echo try $i + echo "try $i: waiting for port" sleep 2 else break @@ -173,13 +192,14 @@ launch_server() { wait_server() { local PID=$1 - trap "test -n \"${PID}\" && kill ${PID};exit 1" 1 15 2 + trap "test -n \"${PID}\" && kill ${PID}; exit 1" 1 15 2 wait_for_port $PORT if test $? != 0;then echo "Server $PORT did not come up" kill $PID exit 1 fi + rmdir "$top_builddir/tests/port.lock.d" > /dev/null 2>&1 || : } wait_udp_server() { -- cgit v1.2.1