#!/usr/bin/setsid bash # TODO(xujyan): Make the script OSX compatible. proc usage { cat << """ Run Mesos tests within a duration and attach gdb if the tests time out. This script will run the test command under the current work directory. To put the time limit only on tests, compile the project before running the script. This script works only on Linux. Usage: $0 [-h] test_cmd Command that runs the tests. e.g., MESOS_VERBOSE=1 make check GTEST_SHUFFLE=1 duration Duration (in seconds) before the tests time out. e.g., 3600, '$'((160 * 60)) -h Print this help message and exit """ } proc die { echo >&2 @Argv> !2 "$@" exit 1 } if test $(#) -ne 2 { usage exit 1 } setglobal test_cmd = $1 setglobal duration = $2 echo "This script runs with session id $Pid and can be terminated by: pkill -s $Pid" printf $[date]; echo ": start running $test_cmd" setglobal start = $[date +"%s] eval $test_cmd & setglobal test_cmd_pid = $BgPid while test $shExpr('$(date +"%s") - $start') -lt $duration { setglobal running = $[ps p $test_cmd_pid h | wc -l] if test $running -eq 0 { echo "Test finished" # Get the exit status. wait $test_cmd_pid setglobal exit_status = $Status echo "Exit status: $exit_status" exit $exit_status } sleep 5 } printf $[date]; echo ": process still running after $duration seconds" setglobal tmp = $[mktemp XXXXX] echo "thread apply all bt" > $tmp for test_pid in [$[ pgrep -s 0]] { cat << """ ========== Attaching gdb to $[ps o pid,cmd p $test_pid h] ========== """ gdb attach $test_pid < $tmp } rm $tmp echo "Test failed and killing the stuck test process" # Kill all processes in the test process session. pkill -s 0 exit 1