|
| 1 | +#!/usr/bin/env bash |
| 2 | +# End to end test of process isolation: runs the fixture binary, built in an |
| 3 | +# analysis mode, and checks the processes it used and its exit code. |
| 4 | +set -u |
| 5 | + |
| 6 | +FIXTURE="$1" |
| 7 | +EXPECTED_BENCHMARKS=(BM_First BM_Second BM_OnMainWorker) |
| 8 | +FAILING_EXIT_CODE=3 |
| 9 | +TIMEOUT_SECONDS=120 |
| 10 | + |
| 11 | +failures=0 |
| 12 | +fail() { |
| 13 | + echo "FAIL: $*" >&2 |
| 14 | + failures=$((failures + 1)) |
| 15 | +} |
| 16 | + |
| 17 | +run_fixture() { |
| 18 | + timeout "$TIMEOUT_SECONDS" "$FIXTURE" "$@" >"$OUTPUT_FILE" 2>"$ERROR_FILE" |
| 19 | + return $? |
| 20 | +} |
| 21 | + |
| 22 | +OUTPUT_FILE="$(mktemp)" |
| 23 | +ERROR_FILE="$(mktemp)" |
| 24 | +trap 'rm -f "$OUTPUT_FILE" "$ERROR_FILE"' EXIT |
| 25 | + |
| 26 | +distinct_pids() { |
| 27 | + grep '^PID ' "$OUTPUT_FILE" | awk '{print $3}' | sort -u | wc -l |
| 28 | +} |
| 29 | + |
| 30 | +check_all_benchmarks_ran() { |
| 31 | + local label="$1" |
| 32 | + for name in "${EXPECTED_BENCHMARKS[@]}"; do |
| 33 | + grep -q "::${name}" "$ERROR_FILE" || fail "$label: no 'Measured/Checked' line for $name" |
| 34 | + done |
| 35 | +} |
| 36 | + |
| 37 | +# 1. Isolation on (default and explicit): one process per benchmark. |
| 38 | +for setting in "" "true" "1"; do |
| 39 | + label="isolation=${setting:-<unset>}" |
| 40 | + if [ -z "$setting" ]; then |
| 41 | + env -u CODSPEED_PROCESS_ISOLATION -u FIXTURE_FAIL \ |
| 42 | + timeout "$TIMEOUT_SECONDS" "$FIXTURE" >"$OUTPUT_FILE" 2>"$ERROR_FILE" |
| 43 | + else |
| 44 | + CODSPEED_PROCESS_ISOLATION="$setting" run_fixture |
| 45 | + fi |
| 46 | + [ $? -eq 0 ] || fail "$label: expected exit code 0" |
| 47 | + check_all_benchmarks_ran "$label" |
| 48 | + pids=$(distinct_pids) |
| 49 | + [ "$pids" -eq "${#EXPECTED_BENCHMARKS[@]}" ] || |
| 50 | + fail "$label: expected ${#EXPECTED_BENCHMARKS[@]} distinct PIDs, got $pids" |
| 51 | + # The parent prints the context once, children must stay quiet. |
| 52 | + [ "$(grep -c 'Run on' "$ERROR_FILE")" -le 1 ] || fail "$label: context printed more than once" |
| 53 | +done |
| 54 | + |
| 55 | +# 2. Isolation off: all benchmarks share one process. |
| 56 | +for setting in "false" "0"; do |
| 57 | + label="isolation=$setting" |
| 58 | + CODSPEED_PROCESS_ISOLATION="$setting" run_fixture |
| 59 | + [ $? -eq 0 ] || fail "$label: expected exit code 0" |
| 60 | + check_all_benchmarks_ran "$label" |
| 61 | + pids=$(distinct_pids) |
| 62 | + [ "$pids" -eq 1 ] || fail "$label: expected 1 PID, got $pids" |
| 63 | +done |
| 64 | + |
| 65 | +# 3. A failing benchmark fails the whole run, with or without isolation. |
| 66 | +for setting in "true" "false"; do |
| 67 | + label="failing, isolation=$setting" |
| 68 | + FIXTURE_FAIL=1 CODSPEED_PROCESS_ISOLATION="$setting" run_fixture |
| 69 | + code=$? |
| 70 | + [ "$code" -eq "$FAILING_EXIT_CODE" ] || |
| 71 | + fail "$label: expected exit code $FAILING_EXIT_CODE, got $code" |
| 72 | +done |
| 73 | + |
| 74 | +# 4. The child ignores the command line filter. |
| 75 | +CODSPEED_PROCESS_ISOLATION=true run_fixture --benchmark_filter=BM_First |
| 76 | +[ $? -eq 0 ] || fail "filter: expected exit code 0" |
| 77 | +grep -q "::BM_First" "$ERROR_FILE" || fail "filter: BM_First did not run" |
| 78 | +grep -q "::BM_Second" "$ERROR_FILE" && fail "filter: BM_Second ran despite the filter" |
| 79 | + |
| 80 | +# 5. An invalid value is a hard error. |
| 81 | +CODSPEED_PROCESS_ISOLATION=maybe run_fixture |
| 82 | +[ $? -ne 0 ] || fail "invalid value: expected a non-zero exit code" |
| 83 | +grep -q "CODSPEED_PROCESS_ISOLATION" "$ERROR_FILE" || fail "invalid value: no error message" |
| 84 | + |
| 85 | +if [ "$failures" -ne 0 ]; then |
| 86 | + exit 1 |
| 87 | +fi |
| 88 | +echo "process isolation test passed" |
0 commit comments