Skip to content

Commit b45e76c

Browse files
committed
fix(timeout): resolve degredation which prevented setting indefinite timeout
The help text suggests that one should be able to configure an indefinite timeout by setting it to zero. However, the current implementation would immediately timeout while doing so. This change ignores the timeout behaviour when the timeout argument is set to 0. In addition, a test is added which tests whether the timeout at least doesn't immediately time out.
1 parent be264ee commit b45e76c

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

wait-for

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ wait_for() {
9696
exit 0
9797
fi
9898

99-
if [ $(date +%s) -ge $TIMEOUT_END ]; then
99+
if [ $TIMEOUT -ne 0 -a $(date +%s) -ge $TIMEOUT_END ]; then
100100
echo "Operation timed out" >&2
101101
exit 1
102102
fi

wait-for.bats

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@
3333
[ "$output" != "success" ]
3434
}
3535

36+
@test "wget timeout zero does not immediately timeout" {
37+
# Arrange
38+
timeout=0 # The timeout we will use to invoke the wait-for command with
39+
delay=5 # The amount of seconds to wait to see if our indefinite timeout works
40+
41+
# Act
42+
# - Invoke non-existing local webserver and record duration
43+
start_time=$(date +%s)
44+
run timeout $delay ./wait-for -t ${timeout} http://localhost/
45+
end_time=$(date +%s)
46+
47+
# Assert
48+
# - Assert that the script should't have printed that it timed-out
49+
[ "$output" != "Operation timed out" ]
50+
51+
# - Expect a non-zero exit code
52+
[ "$status" != 0 ]
53+
54+
# - Assert that wait-for waited at least as long as we we're going to test delaying for.
55+
elapsed=$((end_time - start_time))
56+
[ ${elapsed} -ge ${delay} ]
57+
}
58+
3659
@test "wget timeout does not double" {
3760
timeout=10
3861
cat >delay <<-EOF

0 commit comments

Comments
 (0)