Skip to content

Commit

Permalink
fix: wget timeout does not double
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp-kunz-mimacom authored Dec 3, 2021
1 parent 92fca0a commit 206b38d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
10 changes: 5 additions & 5 deletions wait-for
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ wait_for() {
;;
esac

TIMEOUT_END=$(($(date +%s) + TIMEOUT))

while :; do
case "$PROTOCOL" in
tcp)
Expand Down Expand Up @@ -94,15 +96,13 @@ wait_for() {
exit 0
fi

if [ "$TIMEOUT" -le 0 ]; then
break
if [ $(date +%s) -ge $TIMEOUT_END ]; then
echo "Operation timed out" >&2
exit 1
fi
TIMEOUT=$((TIMEOUT - 1))

sleep 1
done
echo "Operation timed out" >&2
exit 1
}

while :; do
Expand Down
24 changes: 23 additions & 1 deletion wait-for.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@test "google should be immediately found" {
run ./wait-for google.com:80 -- echo 'success'

[ "$output" = "success" ]
}

Expand Down Expand Up @@ -33,6 +33,28 @@
[ "$output" != "success" ]
}

@test "wget timeout does not double" {
timeout=10
cat >delay <<-EOF
#!/usr/bin/env bash
sleep $((timeout + 1))
EOF
chmod +x delay
nc -lk -p 80 -e $(pwd)/delay & ncpid=$!
start_time=$(date +%s)
run ./wait-for -t ${timeout} http://localhost/
end_time=$(date +%s)
kill $ncpid
rm -f delay

[ "$status" != 0 ]
[ "$output" = "Operation timed out" ]
elapsed=$((end_time - start_time))
[ ${elapsed} -ge ${timeout} ]
limit=$((timeout * 3 / 2))
[ ${elapsed} -lt ${limit} ]
}

@test "environment variable HOST should be restored for command invocation" {
HOST=success run ./wait-for -t 1 google.com:80 -- sh -c 'echo "$HOST"'

Expand Down

0 comments on commit 206b38d

Please sign in to comment.