From 7ba4a6f5a02f6142d0e28fd475fd008532fb1083 Mon Sep 17 00:00:00 2001
From: Frantisek Sumsal <frantisek@sumsal.cz>
Date: Tue, 5 Mar 2019 13:50:28 +0100
Subject: [PATCH] test: parallelize tasks in TEST-24-UNIT-TESTS

(cherry picked from commit 2f2a0454efd07644a4e0ccb3f00f1db2d7043391)

Related: #1823767
---
 test/TEST-24-UNIT-TESTS/test.sh      |  2 +
 test/TEST-24-UNIT-TESTS/testsuite.sh | 97 +++++++++++++++++++++-------
 2 files changed, 77 insertions(+), 22 deletions(-)

diff --git a/test/TEST-24-UNIT-TESTS/test.sh b/test/TEST-24-UNIT-TESTS/test.sh
index 014ee52277..fc8c89fe0a 100755
--- a/test/TEST-24-UNIT-TESTS/test.sh
+++ b/test/TEST-24-UNIT-TESTS/test.sh
@@ -78,6 +78,8 @@ test_setup() {
         setup_basic_environment
         install_keymaps yes
         install_zoneinfo
+        # Install nproc to determine # of CPUs for correct parallelization
+        inst_binary nproc
 
         # setup the testsuite service
         cat >$initdir/etc/systemd/system/testsuite.service <<EOF
diff --git a/test/TEST-24-UNIT-TESTS/testsuite.sh b/test/TEST-24-UNIT-TESTS/testsuite.sh
index 7c0e495dbd..7c7a068556 100755
--- a/test/TEST-24-UNIT-TESTS/testsuite.sh
+++ b/test/TEST-24-UNIT-TESTS/testsuite.sh
@@ -4,31 +4,84 @@
 #set -ex
 #set -o pipefail
 
-for i in /usr/lib/systemd/tests/test-*; do
-    if [[ ! -x $i ]]; then continue; fi
-    NAME=${i##*/}
-    echo "Running $NAME"
-    $i > /$NAME.log 2>&1
-    ret=$?
-    if (( $ret && $ret != 77 )); then
-        echo "$NAME failed with $ret"
-        echo $NAME >> /failed-tests
-        echo "--- $NAME begin ---" >> /failed
-        cat /$NAME.log >> /failed
-        echo "--- $NAME end ---" >> /failed
-    elif (( $ret == 77 )); then
-        echo "$NAME skipped"
-        echo $NAME >> /skipped-tests
-        echo "--- $NAME begin ---" >> /skipped
-        cat /$NAME.log >> /skipped
-        echo "--- $NAME end ---" >> /skipped
+NPROC=$(nproc)
+MAX_QUEUE_SIZE=${NPROC:-2}
+IFS=$'\n' TEST_LIST=($(ls /usr/lib/systemd/tests/test-*))
+
+# Check & report test results
+# Arguments:
+#   $1: test path
+#   $2: test exit code
+function report_result() {
+    if [[ $# -ne 2 ]]; then
+        echo >&2 "check_result: missing arguments"
+        exit 1
+    fi
+
+    local name="${1##*/}"
+    local ret=$2
+
+    if [[ $ret -ne 0 && $ret != 77 ]]; then
+        echo "$name failed with $ret"
+        echo "$name" >> /failed-tests
+        {
+            echo "--- $name begin ---"
+            cat "/$name.log"
+            echo "--- $name end ---"
+        } >> /failed
+    elif [[ $ret == 77 ]]; then
+        echo "$name skipped"
+        echo "$name" >> /skipped-tests
+        {
+            echo "--- $name begin ---"
+            cat "/$name.log"
+            echo "--- $name end ---"
+        } >> /skipped
     else
-        echo "$NAME OK"
-        echo $NAME >> /testok
+        echo "$name OK"
+        echo "$name" >> /testok
+    fi
+
+    systemd-cat echo "--- $name ---"
+    systemd-cat cat "/$name.log"
+}
+
+# Associative array for running tasks, where running[test-path]=PID
+declare -A running=()
+for task in "${TEST_LIST[@]}"; do
+    # If there's MAX_QUEUE_SIZE running tasks, keep checking the running queue
+    # until one of the tasks finishes, so we can replace it.
+    while [[ ${#running[@]} -ge $MAX_QUEUE_SIZE ]]; do
+        for key in "${!running[@]}"; do
+            if ! kill -0 ${running[$key]} &>/dev/null; then
+                # Task has finished, report its result and drop it from the queue
+                wait ${running[$key]}
+                ec=$?
+                report_result "$key" $ec
+                unset running["$key"]
+                # Break from inner for loop and outer while loop to skip
+                # the sleep below when we find a free slot in the queue
+                break 2
+            fi
+        done
+
+        # Precisely* calculated constant to keep the spinlock from burning the CPU(s)
+        sleep 0.01
+    done
+
+    if [[ -x $task ]]; then
+        log_file="/${task##*/}.log"
+        $task &> "$log_file" &
+        running[$task]=$!
     fi
+done
 
-    systemd-cat echo "--- $NAME ---"
-    systemd-cat cat /$NAME.log
+# Wait for remaining running tasks
+for key in "${!running[@]}"; do
+    wait ${running[$key]}
+    ec=$?
+    report_result "$key" $ec
+    unset running["$key"]
 done
 
 exit 0
