Skip to content

Instantly share code, notes, and snippets.

@bryanchriswhite
Last active October 24, 2023 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanchriswhite/c4f2fe855c94dacf190a889499838805 to your computer and use it in GitHub Desktop.
Save bryanchriswhite/c4f2fe855c94dacf190a889499838805 to your computer and use it in GitHub Desktop.

itest

Purpose

Ensuring critical tests against flakiness with a faster feedback loop and easier developer experience.

  • Avoiding the uncertainty about the health of long-running tests which have no output
  • Automate the tedium of running smaller batches

Usage

$ source ./itest.sh #  or add this to your .bashrc/.zshrc/etc.
$ itest
Usage: itest <go_test_count> <loop_limit> <package_path> [optional_flags]
$ itest 20 5 ./pkg/observable/...
Iteration 1 of 5...
?       pocket/pkg/observable   [no test files]
ok      pocket/pkg/observable/channel   2.643s

Iteration 2 of 5...
?       pocket/pkg/observable   [no test files]
ok      pocket/pkg/observable/channel   2.650s

Iteration 3 of 5...
?       pocket/pkg/observable   [no test files]
ok      pocket/pkg/observable/channel   2.650s

^CInterrupted. Total tests run: 60
itest() {
# Check if correct arguments are passed
if [[ -z "$1" || -z "$2" || -z "$3" ]]; then
echo "Usage: itest <go_test_count> <loop_limit> <package_path> [go test flags...]"
return 1
fi
local go_test_count=$1
local loop_limit=$2
local pkg_path=$3
local total_tests_run=0
shift 3
trap 'echo -e "\nInterrupted. Total tests run: $total_tests_run"; return 1' SIGINT
# Suppress job control messages
setopt nomonitor
for i in $(seq 1 $loop_limit); do
echo "Iteration $i of $loop_limit..."
# Running the go test in a subshell in the background
( go test -count=$go_test_count -race "$@" $pkg_path; echo $?>/tmp/ttest_status; echo ""; ) &
local test_pid=$!
# Wait for the background test to complete
wait $test_pid 2>/dev/null
local test_exit_status=$(cat /tmp/ttest_status)
rm -f /tmp/ttest_status
total_tests_run=$((total_tests_run + go_test_count))
# If go test fails, exit the loop.
if [[ $test_exit_status -ne 0 ]]; then
echo "go test failed on iteration $i. Exiting early."
# Restore job control messages
unsetopt nomonitor
return 1
fi
done
echo "All iterations completed. Total tests run: $total_tests_run"
# Restore job control messages
unsetopt nomonitor
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment