From 206b38d01ddf0fe76ea35efadc4ebdfb6c877895 Mon Sep 17 00:00:00 2001 From: philipp-kunz-mimacom <83705223+philipp-kunz-mimacom@users.noreply.github.com> Date: Fri, 3 Dec 2021 08:43:51 +0100 Subject: [PATCH] fix: wget timeout does not double --- wait-for | 10 +++++----- wait-for.bats | 24 +++++++++++++++++++++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/wait-for b/wait-for index b03faed..5664256 100755 --- a/wait-for +++ b/wait-for @@ -63,6 +63,8 @@ wait_for() { ;; esac + TIMEOUT_END=$(($(date +%s) + TIMEOUT)) + while :; do case "$PROTOCOL" in tcp) @@ -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 diff --git a/wait-for.bats b/wait-for.bats index 2a79edc..4607ee5 100644 --- a/wait-for.bats +++ b/wait-for.bats @@ -2,7 +2,7 @@ @test "google should be immediately found" { run ./wait-for google.com:80 -- echo 'success' - + [ "$output" = "success" ] } @@ -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"'