]> Sergey Matveev's repositories - bfs.git/commitdiff
tests: Run test cases in parallel
authorTavian Barnes <tavianator@tavianator.com>
Mon, 23 Oct 2023 14:12:25 +0000 (10:12 -0400)
committerTavian Barnes <tavianator@tavianator.com>
Mon, 23 Oct 2023 14:12:25 +0000 (10:12 -0400)
GNUmakefile
tests/getopts.sh
tests/run.sh

index 45797e286a522c6f5746e3a23e049961ae716022..62f24fbc92175e43550d764a6030f9fd54753b16 100644 (file)
@@ -287,8 +287,13 @@ check: $(CHECKS)
 $(UNIT_CHECKS): check-%: $(BIN)/tests/%
        $<
 
+JOBS := $(filter -j%,$(MAKEFLAGS))
+ifndef JOBS
+       JOBS := -j1
+endif
+
 $(STRATEGY_CHECKS): check-%: $(BIN)/bfs $(TEST_UTILS)
-       ./tests/tests.sh --bfs="$(BIN)/bfs -S $*" $(TEST_FLAGS)
+       ./tests/tests.sh $(JOBS) --bfs="$(BIN)/bfs -S $*" $(TEST_FLAGS)
 
 # Custom test flags for distcheck
 DISTCHECK_FLAGS := -s TEST_FLAGS="--sudo --verbose=skipped"
index 7d3ef4b056c189f1bc1301726878e6f65b829548..d34df4f8bbc7138f3d7cce1896b0d19b171dc790 100644 (file)
@@ -52,6 +52,7 @@ EOF
 
 # Parse the command line
 parse_args() {
+    JOBS=0
     PATTERNS=()
     SUDO=()
     STOP=0
@@ -64,6 +65,9 @@ parse_args() {
 
     for arg; do
         case "$arg" in
+            -j*)
+                JOBS="${arg#-j}"
+                ;;
             --bfs=*)
                 BFS="${arg#*=}"
                 ;;
@@ -127,6 +131,14 @@ parse_args() {
         esac
     done
 
+    if ((JOBS == 0)); then
+        if command -v nproc &>/dev/null; then
+            JOBS=$(nproc)
+        else
+            JOBS=1
+        fi
+    fi
+
     # Try to resolve the path to $BFS before we cd, while also supporting
     # --bfs="./bin/bfs -S ids"
     read -a BFS <<<"${BFS:-$BIN/bfs}"
index 28801f4a1f871abe3d145b6c43922bbd045e18a9..b46fde63b68439a31326bb96d30c05b67d4205ba 100644 (file)
@@ -42,6 +42,42 @@ run_test() (
     source "$@"
 )
 
+# Run a test in the background
+bg_test() {
+    if ((VERBOSE_ERRORS)); then
+        run_test "$1"
+    else
+        run_test "$1" 2>"$TMP/TEST.err"
+    fi
+    ret=$?
+
+    if ((ret != 0 && ret != EX_SKIP)); then
+        ((VERBOSE_ERRORS)) || cat "$TMP/$TEST.err" >&2
+        color printf "${BOL}${RED}%s failed!${RST}\n" "$TEST"
+    fi
+
+    return $ret
+}
+
+# Reap a background job
+reap() {
+    wait -n
+    ret=$?
+    ((BG--))
+
+    case $ret in
+        0)
+            ((++passed))
+            ;;
+        $EX_SKIP)
+            ((++skipped))
+            ;;
+        *)
+            ((++failed))
+            ;;
+    esac
+}
+
 # Run all the tests
 run_tests() {
     if ((VERBOSE_TESTS)); then
@@ -70,6 +106,8 @@ run_tests() {
         TEST_FMT="."
     fi
 
+    BG=0
+
     # Turn off set -e (but turn it back on in run_test)
     set +e
 
@@ -79,23 +117,16 @@ run_tests() {
         mkdir -p "$TMP/$TEST"
         OUT="$TMP/$TEST.out"
 
-        if ((VERBOSE_ERRORS)); then
-            run_test "$TESTS/$TEST.sh"
-        else
-            run_test "$TESTS/$TEST.sh" 2>"$TMP/$TEST.err"
+        if ((BG >= JOBS)); then
+            reap
         fi
-        status=$?
+        ((++BG))
 
-        if ((status == 0)); then
-            ((++passed))
-        elif ((status == EX_SKIP)); then
-            ((++skipped))
-        else
-            ((++failed))
-            ((VERBOSE_ERRORS)) || cat "$TMP/$TEST.err" >&2
-            color printf "${BOL}${RED}%s failed!${RST}\n" "$TEST"
-            ((STOP)) && break
-        fi
+        bg_test "$TESTS/$TEST.sh" &
+    done
+
+    while ((BG > 0)); do
+        reap
     done
 
     printf "${BOL}"