All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Haller <thaller@redhat.com>
To: NetFilter <netfilter-devel@vger.kernel.org>
Cc: Thomas Haller <thaller@redhat.com>
Subject: [PATCH nft 1/2] tests/shell: workaround lack of `wait -p` before bash 5.1
Date: Mon, 27 Nov 2023 20:15:35 +0100	[thread overview]
Message-ID: <20231127191713.3528973-2-thaller@redhat.com> (raw)
In-Reply-To: <20231127191713.3528973-1-thaller@redhat.com>

Before bash 5.1, `wait -p` is not supported. So we cannot know which
child process completed.

As workaround, explicitly wait for the next PID. That works, but it
significantly reduces parallel execution, because a long running job
blocks the queue.

Signed-off-by: Thomas Haller <thaller@redhat.com>
---
 tests/shell/run-tests.sh | 47 ++++++++++++++++++++++++++++++++++------
 1 file changed, 40 insertions(+), 7 deletions(-)

diff --git a/tests/shell/run-tests.sh b/tests/shell/run-tests.sh
index 3cde97b7ea17..f1345bb14e7c 100755
--- a/tests/shell/run-tests.sh
+++ b/tests/shell/run-tests.sh
@@ -30,6 +30,23 @@ array_contains() {
 	return 1
 }
 
+array_remove_first() {
+	local _varname="$1"
+	local _needle="$2"
+	local _result=()
+	local _a
+
+	eval "local _input=( \"\${$_varname[@]}\" )"
+	for _a in "${_input[@]}" ; do
+		if [ -n "${_needle+x}" -a "$_needle" = "$_a" ] ; then
+			unset _needle
+		else
+			_result+=("$_a")
+		fi
+	done
+	eval "$_varname="'( "${_result[@]}" )'
+}
+
 colorize_keywords() {
 	local out_variable="$1"
 	local color="$2"
@@ -598,13 +615,14 @@ if [ ! -x "$DIFF" ] ; then
 	DIFF=true
 fi
 
+JOBS_PIDLIST_ARR=()
 declare -A JOBS_PIDLIST
 
 _NFT_TEST_VALGRIND_VGDB_PREFIX=
 
 cleanup_on_exit() {
 	pids_search=''
-	for pid in "${!JOBS_PIDLIST[@]}" ; do
+	for pid in "${JOBS_PIDLIST_ARR[@]}" ; do
 		kill -- "-$pid" &>/dev/null
 		pids_search="$pids_search\\|\\<$pid\\>"
 	done
@@ -858,17 +876,33 @@ job_start() {
 	return "$rc_got"
 }
 
+# `wait -p` is only supported since bash 5.1
+WAIT_SUPPORTS_P=1
+[ "${BASH_VERSINFO[0]}" -le 4 -o \( "${BASH_VERSINFO[0]}" -eq 5 -a "${BASH_VERSINFO[1]}" -eq 0 \) ] && WAIT_SUPPORTS_P=0
+
 job_wait()
 {
 	local num_jobs="$1"
+	local JOBCOMPLETED
+	local rc_got
+
+	while [ "${#JOBS_PIDLIST_ARR[@]}" -gt 0 -a "${#JOBS_PIDLIST_ARR[@]}" -ge "$num_jobs" ] ; do
+		if [ "$WAIT_SUPPORTS_P" = 1 ] ; then
+			wait -n -p JOBCOMPLETED
+			rc_got="$?"
+			array_remove_first JOBS_PIDLIST_ARR "$JOBCOMPLETED"
+		else
+			# Without `wait -p` support, we need to explicitly wait
+			# for a PID. That reduces parallelism.
+			JOBCOMPLETED="${JOBS_PIDLIST_ARR[0]}"
+			JOBS_PIDLIST_ARR=( "${JOBS_PIDLIST_ARR[@]:1}" )
+			wait -n "$JOBCOMPLETED"
+			rc_got="$?"
+		fi
 
-	while [ "$JOBS_N_RUNNING" -gt 0 -a "$JOBS_N_RUNNING" -ge "$num_jobs" ] ; do
-		wait -n -p JOBCOMPLETED
-		local rc_got="$?"
 		local testfile2="${JOBS_PIDLIST[$JOBCOMPLETED]}"
 		unset JOBS_PIDLIST[$JOBCOMPLETED]
 		print_test_result "${JOBS_TEMPDIR["$testfile2"]}" "$testfile2" "$rc_got"
-		((JOBS_N_RUNNING--))
 		check_kmemleak
 	done
 }
@@ -878,7 +912,6 @@ if [ "$NFT_TEST_SHUFFLE_TESTS" = y ] ; then
 fi
 
 TESTIDX=0
-JOBS_N_RUNNING=0
 for testfile in "${TESTS[@]}" ; do
 	job_wait "$NFT_TEST_JOBS"
 
@@ -897,7 +930,7 @@ for testfile in "${TESTS[@]}" ; do
 	pid=$!
 	eval "$set_old_state"
 	JOBS_PIDLIST[$pid]="$testfile"
-	((JOBS_N_RUNNING++))
+	JOBS_PIDLIST_ARR+=( "$pid" )
 done
 
 job_wait 0
-- 
2.43.0


  reply	other threads:[~2023-11-27 19:17 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-27 19:15 [PATCH nft 0/2] tests/shell: workaround for bash Thomas Haller
2023-11-27 19:15 ` Thomas Haller [this message]
2023-11-27 19:15 ` [PATCH nft 2/2] tests/shell: workaround lack of $SRANDOM before bash 5.1 Thomas Haller
2023-11-29 18:55 ` [PATCH nft 0/2] tests/shell: workaround for bash Pablo Neira Ayuso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231127191713.3528973-2-thaller@redhat.com \
    --to=thaller@redhat.com \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.