Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 67 lines (56 sloc) 2.57 KB
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
print-fail() {
echo -e "${ANSI_RED}FAIL: $@${RESET_COLOR}"
simple-test-wait-for() {
echo "TEST: wait-for $@"
$SCRIPT_DIR/../wait-for "$@"
# Wait for "done" to be printed by a bash command that will only
# print "done" if we're more than $block_ms milliseconds after the
# testcase started. Then, verify that wait-for blocked for at least
# $expected_minimum_ms but not more than $expected_maximum_ms, given
# that the poll interval is set to $poll_ms milliseconds.
test-wait-for() {
# We will tell wait-for to run the command every $poll_ms milliseconds.
# The command won't print 'done' until $block_ms milliseconds has passed.
# Test will pass if total waiting time in milliseconds was between
# $expected_minimum_ms= and $expected_maximum_ms milliseconds.
start_ms="$(($(date +%s%N)/1000000))"
stop_ms="$(($start_ms + $block_ms))"
# $(($(date +%s%N)/1000000)) is the number of milliseconds since 1970, but we
# need to escape the $ chars so that it will be re-evaluated each time $cmd runs.
cmd="if ((\$((\$(date +%s%N)/1000000)) > $stop_ms)); then echo done; fi"
poll_seconds=$(bc <<< "scale=2; $poll_ms/1000")
simple-test-wait-for --sleep $poll_seconds --stdout-equals done "$cmd"
taken_ms="$(($(date +%s%N)/1000000 - $start_ms))"
if (($taken_ms < $expected_minimum_ms)) || (($taken_ms > $expected_maximum_ms)); then
print-fail "wait-for should have blocked between ${expected_minimum_ms}ms and ${expected_maximum_ms}ms but it actually blocked for ${taken_ms}ms"
simple-test-wait-for /bin/true
simple-test-wait-for --exitcode 0 /bin/true
simple-test-wait-for --exitcode 1 /bin/false
simple-test-wait-for --not --exitcode 0 /bin/false
simple-test-wait-for --exitcode 2 "exit 2"
simple-test-wait-for --not --exitcode 1 "exit 2"
simple-test-wait-for --stdout-equals foo "echo foo"
simple-test-wait-for --not --stdout-equals bar "echo foo"
simple-test-wait-for --stdout-contains foo "echo foobar"
simple-test-wait-for --stdout-contains foo "echo barfoo"
simple-test-wait-for --stdout-contains foo "echo barfoobar"
simple-test-wait-for --not --stdout-contains foo "echo bar"
simple-test-wait-for --not --stdout-contains foo "echo -e f\noo"
# wait-for something that takes 200 ms should take
# 400ms - 600ms given a poll interval of 400ms.
test-wait-for 400 200 400 600
# wait-for something that takes 200 ms should take
# 200ms - 300ms given a poll interval of 20ms.
test-wait-for 20 200 200 300