From: Erwan Velu Date: Thu, 24 Mar 2016 09:57:38 +0000 (+0100) Subject: tests: Adding parallelism helpers in ceph-helpers.sh X-Git-Tag: v10.1.1~3^2~15 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=db31cc6cbcdb2f42ff0acaed90bc029d1720cfca;p=ceph.git tests: Adding parallelism helpers in ceph-helpers.sh This commit introduce two new functions in ceph-helpers.sh to ease parallelism in tests. It's based on two functions : run_in_background() & wait_background() The first one allow you to spawn processes or functions in background and saves the associated pid in a variable passed as first argument. The second one waits for thoses pids to complete and report their exit status. If one or more failed then wait_background() reports a failure. A typical usage looks like : pids1="" run_in_background pids1 bash -c 'sleep 5; exit 0' run_in_background pids1 bash -c 'sleep 1; exit 1' run_in_background pids1 my_bash_function wait_background pids1 The variable that contains pids is local making possible to do nested calls of thoses two new functions. Signed-off-by: Erwan Velu --- diff --git a/qa/workunits/ceph-helpers.sh b/qa/workunits/ceph-helpers.sh index 9ff7339c7aa4..4877b79517e6 100755 --- a/qa/workunits/ceph-helpers.sh +++ b/qa/workunits/ceph-helpers.sh @@ -1282,6 +1282,83 @@ function test_display_logs() { teardown $dir || return 1 } +####################################################################### +## +# Spawn a command in background and save the pid in the variable name +# passed in argument. To make the output reading easier, the output is +# prepend with the process id. +# +# Example: +# pids1="" +# run_in_background pids1 bash -c 'sleep 1; exit 1' +# +# @param pid_variable the variable name (not value) where the pids will be stored +# @param ... the command to execute +# @return only the pid_variable output should be considered and used with **wait_background** +# +function run_in_background() { + local pid_variable=$1 + shift; + # Execute the command and prepend the output with its pid + # We enforce to return the exit status of the command and not the awk one. + ("$@" |& awk '{ a[i++] = $0 }END{for (i = 0; i in a; ++i) { print PROCINFO["pid"] ": " a[i]} }'; return ${PIPESTATUS[0]}) & + eval "$pid_variable+=\" $!\"" +} + +function test_run_in_background() { + local pids + run_in_background pids sleep 1 + run_in_background pids sleep 1 + test $(echo $pids | wc -w) = 2 || return 1 + wait $pids || return 1 +} + +####################################################################### +## +# Wait for pids running in background to complete. +# This function is usually used after a **run_in_background** call +# Example: +# pids1="" +# run_in_background pids1 bash -c 'sleep 1; exit 1' +# wait_background pids1 +# +# @param pids The variable name that contains the active PIDS. Set as empty at then end of the function. +# @return returns 1 if at least one process exits in error unless returns 0 +# +function wait_background() { + # We extract the PIDS from the variable name + pids=${!1} + + return_code=0 + for pid in $pids; do + if ! wait $pid; then + # If one process failed then return 1 + return_code=1 + fi + done + + # We empty the variable reporting that all process ended + eval "$1=''" + + return $return_code +} + + +function test_wait_background() { + local pids="" + run_in_background pids bash -c "sleep 1; exit 1" + run_in_background pids bash -c "sleep 2; exit 0" + wait_background pids + if [ $? -ne 1 ]; then return 1; fi + + run_in_background pids bash -c "sleep 1; exit 0" + run_in_background pids bash -c "sleep 2; exit 0" + wait_background pids + if [ $? -ne 0 ]; then return 1; fi + + if [ ! -z "$pids" ]; then return 1; fi +} + ####################################################################### ##