From 652e5e1ec538e9f21e78f16fe2e7bfd208797ee3 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 5 Oct 2026 23:54:00 +0800 Subject: [PATCH] ci: the gateway's race suite runs over one process per core, not one at a time (wizzy-yang, #923) The Race step took 690s on ubuntu and 908s on macOS, almost all of it internal/gateway: its 1046 tests can't call t.Parallel (t.Setenv rules it out), so the package ran on one core under the race detector. build/race-shards.sh compiles nothing itself: it takes the package's race test binary (go test -race -c), splits its tests round-robin by name over N processes run at once, prints each process's log in a folded group and fails when any does. The gateway's TestMain already isolates every process in its own temporary home, keychain stand-ins and PATH, so the processes share nothing. The step runs it with one process per core (getconf _NPROCESSORS_ONLN: 4 on ubuntu, 3 on macOS), the same -race binary and the same 20-minute timeout; redact keeps its own go test -race. Every test still runs under the race detector, once. Locally (Apple silicon, 4 processes) the gateway's race suite passed in 4:39 against 567s of test time run one after another; one test, TestLargeRequestEndpoints (seven 64 MB bodies, 198s), now bounds it. --- .github/workflows/test.yml | 12 ++++++++---- build/race-shards.sh | 31 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) create mode 100755 build/race-shards.sh 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