#!/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=200 load=0 threads="" while [ $# -gt 0 ]; do case "$1" in -p) package="$2"; shift 2 ;; --test) target_args+=(--test "$2"); shift 2 ;; --lib) target_args+=(--lib); shift ;; --features) target_args+=(--features "$2"); shift 2 ;; --filter) filter="$2"; shift 2 ;; --runs) runs="$2"; shift 2 ;; --load) load="$2"; shift 2 ;; --test-threads) threads="$2"; shift 2 ;; *) echo "unknown argument: $1" >&2; exit 2 ;; esac done if [ -z "$package" ] || [ -z "$filter" ]; then echo "usage: $0 -p [--lib | --test ] [--features ] --filter [--runs N] [--load N] [--test-threads N]" >&2 exit 2 fi here="$(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, sys lines = [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" >&2 exit 2 fi hogs=() cleanup() { for pid in ${hogs[@]+"${hogs[@]}"}; do [ -n "$pid" ] && kill "$pid" 2>/dev/null || true; done } trap cleanup EXIT for _ in $(seq 1 "$load"); do nice -n 20 bash -c 'while :; do :; done' & hogs+=("$!") done extra=() [ -n "$threads" ] && extra+=(--test-threads "$threads") passed=0 failed=0 for 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" fi done echo "flake-hunt $package $filter: $passed passed, $failed failed of $runs (load $load)" if [ "$failed" -gt 0 ]; then echo "first failure: $out/first-failure.log" exit 1 fi