Improve test framework: sleep(1) arguments

Always pass exactly one argument to sleep(1) since the BSD sleep (used
on OSX) does not sum its arguments like GNU sleep(1)

Perform more stringent checking on various timeout parameters to ensure
they are well-formed decimal floats
This commit is contained in:
Andrew Bettison 2014-02-17 17:11:31 +10:30
parent c4944e4334
commit 5093bff12a

View File

@ -191,7 +191,10 @@ runTests() {
--jobs=*) _tfw_fatal "invalid option: $1";; --jobs=*) _tfw_fatal "invalid option: $1";;
-E|--stop-on-error) _tfw_stop_on_error=true;; -E|--stop-on-error) _tfw_stop_on_error=true;;
-F|--stop-on-failure) _tfw_stop_on_failure=true;; -F|--stop-on-failure) _tfw_stop_on_failure=true;;
--timeout=*) _tfw_timeout_override="${1#*=}";; --timeout=*)
_tfw_is_float "${1#*=}" || _tfw_fatal "invalid option: $1"
_tfw_timeout_override="${1#*=}"
;;
--) shift; break;; --) shift; break;;
--*) _tfw_fatal "unsupported option: $1";; --*) _tfw_fatal "unsupported option: $1";;
*) _tfw_fatal "spurious argument: $1";; *) _tfw_fatal "spurious argument: $1";;
@ -684,14 +687,19 @@ _tfw_execute() {
local subshell_pid=$! local subshell_pid=$!
local timer_pid= local timer_pid=
local timeout=${_tfw_timeout_override:-${_tfw_opt_timeout:-${TFW_EXECUTE_TIMEOUT:-$_tfw_default_execute_timeout}}} local timeout=${_tfw_timeout_override:-${_tfw_opt_timeout:-${TFW_EXECUTE_TIMEOUT:-$_tfw_default_execute_timeout}}}
if [ -n "$timeout" ]; then
_tfw_is_float "$timeout" || error "invalid timeout '$timeout'"
fi
if [ -n "$timeout" ]; then if [ -n "$timeout" ]; then
if type pgrep >/dev/null 2>/dev/null; then if type pgrep >/dev/null 2>/dev/null; then
( ( #)#(fix Vim syntax colouring
# For some reason, set -e does not work here. So all the following # For some reason, set -e does not work here. So all the following
# commands are postfixed with || exit $? # commands are postfixed with || exit $?
local executable_pid=$(pgrep -P $subshell_pid) || exit $? local executable_pid=$(pgrep -P $subshell_pid) || exit $?
[ -n "$executable_pid" ] || exit $? [ -n "$executable_pid" ] || exit $?
sleep $timeout 0 0 0 || exit $? if [ -n "$timeout" ]; then
sleep $timeout || exit $?
fi
kill -0 $executable_pid || exit $? kill -0 $executable_pid || exit $?
tfw_log "# timeout after $timeout seconds, sending SIGABRT to pid $executable_pid ($executed)" || exit $? tfw_log "# timeout after $timeout seconds, sending SIGABRT to pid $executable_pid ($executed)" || exit $?
kill -ABRT $executable_pid || exit $? kill -ABRT $executable_pid || exit $?
@ -835,8 +843,7 @@ _tfw_getopts() {
execute*:--core-backtrace) _tfw_opt_core_backtrace=true;; execute*:--core-backtrace) _tfw_opt_core_backtrace=true;;
@(execute*|wait_until):--timeout=@(+([0-9])?(.+([0-9]))|*([0-9]).+([0-9]))) _tfw_opt_timeout="${1#*=}";; @(execute*|wait_until):--timeout=@(+([0-9])?(.+([0-9]))|*([0-9]).+([0-9]))) _tfw_opt_timeout="${1#*=}";;
@(execute*|wait_until):--timeout=*) _tfw_error "invalid value: $1";; @(execute*|wait_until):--timeout=*) _tfw_error "invalid value: $1";;
wait_until:--sleep=@(+([0-9])?(.+([0-9]))|*([0-9]).+([0-9]))) _tfw_opt_sleep="${1#*=}";; wait_until:--sleep=*) _tfw_is_float "${1#*=}" || _tfw_error "invalid value: $1"; _tfw_opt_sleep="${1#*=}";;
wait_until:--sleep=*) _tfw_error "invalid value: $1";;
*grep:--fixed-strings) _tfw_opt_grepopts+=(-F);; *grep:--fixed-strings) _tfw_opt_grepopts+=(-F);;
assertcontentgrep:--matches=+([0-9])) _tfw_opt_matches="${1#*=}";; assertcontentgrep:--matches=+([0-9])) _tfw_opt_matches="${1#*=}";;
assertcontentgrep:--matches=*) _tfw_error "invalid value: $1";; assertcontentgrep:--matches=*) _tfw_error "invalid value: $1";;
@ -868,6 +875,13 @@ _tfw_getopts() {
return 0 return 0
} }
_tfw_is_float() {
case "$1" in
@(+([0-9])?(.+([0-9]))|*([0-9]).+([0-9]))) return 0;
esac
return 1
}
_tfw_matches_rexp() { _tfw_matches_rexp() {
local rexp="$1" local rexp="$1"
shift shift
@ -1607,7 +1621,7 @@ assertStderrGrep() {
# Wait until a given condition is met: # Wait until a given condition is met:
# - can specify the timeout with --timeout=SECONDS (overridden by command-line # - can specify the timeout with --timeout=SECONDS (overridden by command-line
# option) # option)
# - can specify the sleep interval with --sleep=SECONDS # - can specify the sleep interval with --sleep=SECONDS (can be a float)
# - the condition is a command that is executed repeatedly until returns zero # - the condition is a command that is executed repeatedly until returns zero
# status # status
# where SECONDS may be fractional, eg, 1.5 # where SECONDS may be fractional, eg, 1.5
@ -1615,14 +1629,15 @@ wait_until() {
$_tfw_assert_noise && tfw_log "# wait_until" $(shellarg "$@") $_tfw_assert_noise && tfw_log "# wait_until" $(shellarg "$@")
local start=$SECONDS local start=$SECONDS
_tfw_getopts wait_until "$@" _tfw_getopts wait_until "$@"
local seconds=${_tfw_timeout_override:-${_tfw_opt_timeout:-${TFW_WAIT_UNTIL_TIMEOUT:-${_tfw_default_wait_until_timeout:-1}}}}
shift $_tfw_getopts_shift shift $_tfw_getopts_shift
local timeout=${_tfw_timeout_override:-${_tfw_opt_timeout:-${TFW_WAIT_UNTIL_TIMEOUT:-${_tfw_default_wait_until_timeout:-1}}}}
_tfw_is_float "$timeout" || error "invalid timeout '$timeout'"
local sense= local sense=
while [ "$1" = '!' ]; do while [ "$1" = '!' ]; do
sense="$sense !" sense="$sense !"
shift shift
done done
sleep $seconds 0 & sleep $timeout &
local timeout_pid=$! local timeout_pid=$!
while true; do while true; do
"$@" "$@"