Add ability to wait for the socket to stop listening.
Signed-off-by: Hubert Kario hkario@redhat.com --- src/synchronisation.sh | 17 ++++++++++++++--- src/test/synchronisationTest.sh | 26 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-)
diff --git a/src/synchronisation.sh b/src/synchronisation.sh index d29affd..15e5602 100644 --- a/src/synchronisation.sh +++ b/src/synchronisation.sh @@ -384,7 +384,7 @@ Pauses script execution until socket starts listening. Returns 0 if socket started listening, 1 if timeout was reached or PID exited. Return code is greater than 1 in case of error.
- rlWaitForSocket {port|path} [-p PID] [-t time] [-d delay] + rlWaitForSocket {port|path} [-p PID] [-t time] [-d delay] [--close]
=over
@@ -407,6 +407,10 @@ the socket is opened, the command returns with status code of 1.
Delay between subsequent checks for availability of socket. Default 1.
+=item --close + +Wait for the socket to stop listening. + =back
=cut @@ -417,9 +421,10 @@ rlWaitForSocket(){ local proc_pid=1 local delay=1 local socket="" + local close=""
# that is the GNU extended getopt syntax! - local TEMP=$(getopt -o t:p:d: -n 'rlWaitForSocket' -- "$@") + local TEMP=$(getopt -o t:p:d: --longoptions close -n 'rlWaitForSocket' -- "$@") if [[ $? != 0 ]] ; then rlLogError "rlWaitForSocket: Can't parse command options, terminating..." return 127 @@ -435,6 +440,8 @@ rlWaitForSocket(){ ;; -d) delay="$2"; shift 2 ;; + --close) close="true"; shift 1 + ;; --) shift 1 break ;; @@ -464,7 +471,11 @@ rlWaitForSocket(){
local cmd="netstat -nl | grep -E '$grep_opt' >/dev/null"
- __INTERNAL_wait_for_cmd "rlWaitForSocket" "${cmd}" -t $timeout -p $proc_pid -d $delay + if [[ ${close:-false} == true ]]; then + __INTERNAL_wait_for_cmd "rlWaitForSocket" "${cmd}" -t $timeout -p $proc_pid -d $delay -r 1 + else + __INTERNAL_wait_for_cmd "rlWaitForSocket" "${cmd}" -t $timeout -p $proc_pid -d $delay + fi }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/test/synchronisationTest.sh b/src/test/synchronisationTest.sh index a7d81ca..6681741 100644 --- a/src/test/synchronisationTest.sh +++ b/src/test/synchronisationTest.sh @@ -34,6 +34,32 @@ test_rlWaitForSocketPositive() { rm -rf $test_dir }
+test_rlWaitForSocketClose() { + local test_dir=$(mktemp -d /tmp/beakerlib-test-XXXXXX) + + (nc -l 12345 > $test_dir/out) & + local bg_pid=$! + + (sleep 5; kill $bg_pid)& + + silentIfNotDebug "rlWaitForSocket 12345" + local ret=$? + assertTrue "Check if rlWaitForSocket return 0 when socket is opened" "[[ $ret -eq 0 ]]" + + silentIfNotDebug "echo 'hello world' | nc localhost 12345" + + silentIfNotDebug "rlWaitForSocket 12345 --close" + local ret=$? + assertTrue "Check if rlWaitForSocket return 0 when socket is closed" "[[ $ret -eq 0 ]]" + + kill -s SIGKILL $bg_pid 2>/dev/null 1>&2 + wait $bg_pid 2>/dev/null 1>&2 + + assertTrue "Check if data was transferred" "grep 'hello world' $test_dir/out" + + rm -rf $test_dir +} + test_rlWaitForSocketTimeoutReached() { local test_dir=$(mktemp -d /tmp/beakerlib-test-XXXXXX)