daandemeyer / rpms / systemd

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