diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index be717b5fc..570894e61 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -62,12 +62,16 @@ jobs: go test -timeout 10m -tags "${{ steps.tags.outputs.tags }}" ./internal/plugin/... -run '^TestFlowBigReplyProd$' -count=1 # The redaction tests reuse this binary in fresh processes to check - # first-use races, so it must include the race detector. + # first-use races, so it must include the race detector. The gateway's + # tests can't call t.Parallel (t.Setenv), so its suite runs split over + # one process per core (#923); each process isolates its own home. - name: Race if: runner.os != 'Windows' - # The full gateway suite can exceed Go's default 10-minute package - # timeout under the race detector on hosted macOS runners. - run: go test -race -timeout 20m -tags "${{ steps.tags.outputs.tags }}" ./internal/gateway/ ./internal/redact/ + shell: bash + run: | + go test -race -c -o "$RUNNER_TEMP/gateway.race" -tags "${{ steps.tags.outputs.tags }}" ./internal/gateway/ + build/race-shards.sh internal/gateway "$RUNNER_TEMP/gateway.race" "$(getconf _NPROCESSORS_ONLN)" -test.timeout=20m + go test -race -tags "${{ steps.tags.outputs.tags }}" ./internal/redact/ # Account usage mixes cached results with concurrent fetches. - name: Race account usage diff --git a/build/race-shards.sh b/build/race-shards.sh new file mode 100755 index 000000000..398f92041 --- /dev/null +++ b/build/race-shards.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# race-shards.sh DIR BIN N [FLAGS]: runs the tests of BIN, a package's test +# binary (go test -c), from the package's DIR in N processes at once, the +# tests split round-robin by name, and fails if any process does. FLAGS go +# to every process. A package's tests run one at a time unless they call +# t.Parallel, which t.Setenv rules out, so the gateway's suite under the +# race detector keeps one core busy; N processes keep N busy. Each process +# isolates its own home (the package's TestMain). +set -euo pipefail +pkg=$1 bin=$2 n=$3 +shift 3 +dir=$(mktemp -d) +trap 'rm -rf "$dir"' EXIT +(cd "$pkg" && "$bin" -test.list '.*') | grep -E '^(Test|Example|Fuzz)' | sort >"$dir/all" +echo "$(wc -l <"$dir/all" | tr -d ' ') tests in $n processes" +pids=() +for i in $(seq 0 $((n - 1))); do + awk -v n="$n" -v i="$i" 'NR % n == i' "$dir/all" >"$dir/s$i" + re="^($(paste -sd'|' "$dir/s$i"))\$" + (cd "$pkg" && "$bin" -test.run "$re" "$@" >"$dir/log$i" 2>&1) & + pids+=($!) +done +fail=0 +for i in "${!pids[@]}"; do + if wait "${pids[$i]}"; then st=ok; else st=FAIL fail=1; fi + echo "::group::shard $i: $st ($(wc -l <"$dir/s$i" | tr -d ' ') tests)" + cat "$dir/log$i" + echo "::endgroup::" + if [ $st = FAIL ]; then grep -E '^(--- FAIL|FAIL|panic|WARNING: DATA RACE)' "$dir/log$i" || true; fi +done +exit $fail