daandemeyer / rpms / systemd

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