Skip to content

fix(iteration): Pointless waiting before a known timeout #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions wait-for
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ wait_for() {
exit 1
fi

for i in `seq $TIMEOUT` ; do
while :; do
nc -z "$HOST" "$PORT" > /dev/null 2>&1

result=$?
Expand All @@ -35,6 +35,12 @@ wait_for() {
fi
exit 0
fi

if [ "$TIMEOUT" -le 0 ]; then
break
fi
TIMEOUT=$((TIMEOUT - 1))

sleep 1
done
echo "Operation timed out" >&2
Expand All @@ -53,11 +59,25 @@ do
QUIET=1
shift 1
;;
-t)
-q-*)
QUIET=0
echoerr "Unknown option: $1"
usage 1
;;
-q*)
QUIET=1
result=$1
shift 1
set -- -"${result#-q}" "$@"
;;
-t | --timeout)
TIMEOUT="$2"
if [ "$TIMEOUT" = "" ]; then break; fi
shift 2
;;
-t*)
TIMEOUT="${1#-t}"
shift 1
;;
--timeout=*)
TIMEOUT="${1#*=}"
shift 1
Expand All @@ -69,13 +89,24 @@ do
--help)
usage 0
;;
-*)
QUIET=0
echoerr "Unknown option: $1"
usage 1
;;
*)
QUIET=0
echoerr "Unknown argument: $1"
usage 1
;;
esac
done

if ! [ "$TIMEOUT" -ge 0 ] 2>/dev/null; then
echoerr "Error: invalid timeout '$TIMEOUT'"
usage 3
fi

if [ "$HOST" = "" -o "$PORT" = "" ]; then
echoerr "Error: you need to provide a host and port to test."
usage 2
Expand Down
20 changes: 20 additions & 0 deletions wait-for.bats
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,23 @@
[ "$status" -ne 0 ]
[ "$output" != "success" ]
}

@test "support condensed option style" {
run ./wait-for -qt1 google.com:80 -- echo 'success'

[ "$output" = "success" ]
}

@test "timeout cannot be negative" {
run ./wait-for -t -1 google.com:80 -- echo 'success'

[ "$status" -ne 0 ]
[ "$output" != "success" ]
}

@test "timeout cannot be empty" {
run ./wait-for -t -- google.com:80 -- echo 'success'

[ "$status" -ne 0 ]
[ "$output" != "success" ]
}