#!/usr/bin/env bash# Run ONE Rust test filter many times, under CPU load, and say how often it# failed — the evidence a "flaky test" claim needs before and after a fix.## scripts/rust-flake-hunt.sh -p cotton-server --test it --filter documents_routes --runs 200 --load 6## The test binary is BUILT ONCE (under the machine-wide build lock) and then# run directly, so each run costs the test and not cargo's fingerprinting. The# load is `--load` busy loops at the lowest priority for the length of the hunt:# a race that needs a loaded runner to show should show here too. The first# failure's whole output is kept; the tally is the last line.## Exit status: 0 when every run passed, 1 when any failed, 2 on bad usage.set -euo pipefail# Every array below is expanded as ${a[@]+"${a[@]}"}: macOS's bash 3.2 calls an# empty array unbound under `set -u`.package=""target_args=()filter=""runs=200load=0threads=""while [ $# -gt 0 ]; docase"$1"in -p) package="$2"; shift2 ;; --test) target_args+=(--test "$2"); shift2 ;; --lib) target_args+=(--lib); shift ;; --features) target_args+=(--features "$2"); shift2 ;; --filter) filter="$2"; shift2 ;; --runs) runs="$2"; shift2 ;; --load) load="$2"; shift2 ;; --test-threads) threads="$2"; shift2 ;; *) echo "unknown argument: $1" >&2; exit2 ;;esacdoneif [ -z "$package" ] || [ -z "$filter" ]; then echo "usage: $0 -p <crate> [--lib | --test <name>] [--features <f>] --filter <name> [--runs N] [--load N] [--test-threads N]" >&2exit2fihere="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"root="$(cd "$here/.." && pwd)"out="$(mktemp -d "${TMPDIR:-/tmp}/flake-hunt.XXXXXX")"# Build once; cargo prints the binary it would run as JSON.binary="$( cd "$root/crates" && BUILD_LOCK_LABEL="${BUILD_LOCK_LABEL:-flake-hunt}""$here/with-build-lock.sh" \ cargo test -p "$package" ${target_args[@]+"${target_args[@]}"} --no-run --message-format=json 2>/dev/null | python3 -c 'import json, syslines = [line for line in sys.stdin if line.startswith("{")]found = [m["executable"] for m in map(json.loads, lines)if m.get("reason") == "compiler-artifact" and m.get("executable") and m.get("profile", {}).get("test")]print(found[-1] if found else"")')"if [ -z "$binary" ]; then echo "no test binary came out of the build — run the cargo command by hand to see why" >&2exit2fihogs=()cleanup() {for pid in ${hogs[@]+"${hogs[@]}"}; do [ -n "$pid" ] && kill "$pid"2>/dev/null || true; done}trap cleanup EXITfor _ in $(seq 1"$load"); do nice -n 20 bash -c 'while :; do :; done' & hogs+=("$!")doneextra=()[ -n "$threads" ] && extra+=(--test-threads "$threads")passed=0failed=0for run in $(seq 1"$runs"); do# Run from the crate's directory, as cargo would: CARGO_MANIFEST_DIR is read# at run time (scripts/__tests__/manifest-dir-at-run-time.test.ts).if (cd "$root/crates/$package" && CARGO_MANIFEST_DIR="$root/crates/$package" \"$binary""$filter" ${extra[@]+"${extra[@]}"} >"$out/run.log"2>&1); then passed=$((passed + 1))else failed=$((failed + 1)) [ -f "$out/first-failure.log" ] || cp "$out/run.log""$out/first-failure.log"fidoneecho "flake-hunt $package $filter: $passed passed, $failed failed of $runs (load $load)"if [ "$failed" -gt 0 ]; then echo "first failure: $out/first-failure.log"exit1fi