Skip to content

Commit 206b38d

Browse files
fix: wget timeout does not double
1 parent 92fca0a commit 206b38d

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

wait-for

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ wait_for() {
6363
;;
6464
esac
6565

66+
TIMEOUT_END=$(($(date +%s) + TIMEOUT))
67+
6668
while :; do
6769
case "$PROTOCOL" in
6870
tcp)
@@ -94,15 +96,13 @@ wait_for() {
9496
exit 0
9597
fi
9698

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

102104
sleep 1
103105
done
104-
echo "Operation timed out" >&2
105-
exit 1
106106
}
107107

108108
while :; do

wait-for.bats

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
@test "google should be immediately found" {
44
run ./wait-for google.com:80 -- echo 'success'
5-
5+
66
[ "$output" = "success" ]
77
}
88

@@ -33,6 +33,28 @@
3333
[ "$output" != "success" ]
3434
}
3535

36+
@test "wget timeout does not double" {
37+
timeout=10
38+
cat >delay <<-EOF
39+
#!/usr/bin/env bash
40+
sleep $((timeout + 1))
41+
EOF
42+
chmod +x delay
43+
nc -lk -p 80 -e $(pwd)/delay & ncpid=$!
44+
start_time=$(date +%s)
45+
run ./wait-for -t ${timeout} http://localhost/
46+
end_time=$(date +%s)
47+
kill $ncpid
48+
rm -f delay
49+
50+
[ "$status" != 0 ]
51+
[ "$output" = "Operation timed out" ]
52+
elapsed=$((end_time - start_time))
53+
[ ${elapsed} -ge ${timeout} ]
54+
limit=$((timeout * 3 / 2))
55+
[ ${elapsed} -lt ${limit} ]
56+
}
57+
3658
@test "environment variable HOST should be restored for command invocation" {
3759
HOST=success run ./wait-for -t 1 google.com:80 -- sh -c 'echo "$HOST"'
3860

0 commit comments

Comments
 (0)