Skip to content

Commit 3840d5d

Browse files
committed
Change strict option to loose option to avoid breaking existing behavior
1 parent 67a77ef commit 3840d5d

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ When using this tool, you only need to pick the `wait-for` file as part of your
99
## Usage
1010

1111
```
12-
./wait-for host:port [-t timeout] [-- command args]
12+
wait-for host:port [-t timeout] [-- command args]
1313
-q | --quiet Do not output any status messages
14-
-s | --strict Only execute subcommand if the test succeeds
14+
-l | --loose Execute subcommand even if the test times out
1515
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
1616
-- COMMAND ARGS Execute command with args after the test finishes
1717
```
@@ -22,18 +22,17 @@ To check if [eficode.com](https://eficode.com) is available:
2222

2323
```
2424
$ ./wait-for www.eficode.com:80 -- echo "Eficode site is up"
25-
26-
Connection to www.eficode.com port 80 [tcp/http] succeeded!
2725
Eficode site is up
2826
```
2927

3028
The subcommand will be executed regardless if the service is up or not. If you wish to execute the subcommand only if the service is up, add the --strict argument. In this example, we will test port 81 on www.google.com which will fail:
3129

3230
```
33-
$ ./wait-for www.eficode.com:80 -- echo "Eficode site is up"
34-
$ ./wait-for www.google.com:81 --timeout=1 --strict -- echo "google is up"
31+
$ ./wait-for www.google.com:81 --timeout=1 -- echo "google is up"
32+
Operation timed out
33+
$ ./wait-for www.google.com:81 --timeout=1 --loose -- echo "waited for google"
3534
Operation timed out
36-
google is up
35+
waited for google
3736
```
3837

3938
To wait for database container to become available:

wait-for

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ OLD_TIMEOUT=$TIMEOUT
44
OLD_QUIET=$QUIET
55
OLD_PORT=$PORT
66
OLD_HOST=$HOST
7+
OLD_LOOSE=$LOOSE
78

89
TIMEOUT=15
910
QUIET=0
11+
LOOSE=0
1012

1113
if ! which nc >/dev/null; then
1214
echo "Netcat is not installed. This script requires netcat to work correctly."
@@ -23,7 +25,7 @@ usage() {
2325
Usage:
2426
$(basename $0) host:port [-t timeout] [-- command args]
2527
-q | --quiet Do not output any status messages
26-
-s | --strict Only execute subcommand if the test succeeds
28+
-l | --loose Execute subcommand even if the test times out
2729
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
2830
-- COMMAND ARGS Execute command with args after the test finishes
2931
USAGE
@@ -40,27 +42,19 @@ test_connection() {
4042
}
4143

4244
wait_for() {
45+
local result
4346
for i in `seq $TIMEOUT` ; do
4447
# use a 1-second timeout, but still sleep 0.1 seconds after just to be safe
4548
test_connection "$HOST" "$PORT"
46-
4749
result=$?
48-
if [ $result -eq 0 ] ; then
49-
if [ $# -gt 0 ] ; then
50-
TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST exec "$@"
51-
fi
52-
exit 0
53-
fi
50+
if [ $result -eq 0 ] ; then break ; fi
5451
sleep 1
5552
done
56-
echo "Operation timed out" >&2
57-
if [ $result -ne 0 ] && [ $STRICT -ne 1 ] ; then
58-
if [ $# -gt 0 ] ; then
59-
exec "$@"
60-
fi
61-
exit 0
53+
[ $result -ne 0 ] && echo "Operation timed out" >&2
54+
if [ $result -eq 0 -o $LOOSE -eq 1 -a $# -gt 0 ] ; then
55+
TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST LOOSE=$OLD_LOOSE exec "$@"
6256
fi
63-
exit 1
57+
exit $result
6458
}
6559

6660
while [ $# -gt 0 ]
@@ -75,8 +69,8 @@ do
7569
QUIET=1
7670
shift 1
7771
;;
78-
-s | --strict)
79-
STRICT=1
72+
-l | --loose)
73+
LOOSE=1
8074
shift 1
8175
;;
8276
-t)
@@ -107,6 +101,4 @@ if [ "$HOST" = "" -o "$PORT" = "" ]; then
107101
usage 2
108102
fi
109103

110-
STRICT=${STRICT:-0}
111-
112104
wait_for "$@"

wait-for.bats

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,27 @@
1313
[ "$output" != "success" ]
1414
}
1515

16-
@test "preserve existing environment variable" {
17-
HOST=myweb.com
18-
PORT=8080
16+
@test "nonexistent server should start command if loose option is specified" {
17+
run ./wait-for -t 1 -l noserver:9999 -- echo 'passable' 2>&1
18+
19+
[ "$status" -eq 0 ]
20+
21+
[ "${lines[0]}" = "Operation timed out" ]
22+
[ "${lines[1]}" = "passable" ]
23+
}
24+
25+
@test "preserve existing environment variables" {
26+
TIMEOUT=mytimeout
27+
QUIET=myquiet
28+
HOST=myhost
29+
PORT=myport
30+
LOOSE=myloose
31+
1932
run ./wait-for google.com:80 -- echo 'success'
2033

21-
[ "$(echo $HOST)" = 'myweb.com' ]
22-
[ "$(echo $PORT)" = '8080' ]
34+
[ "$(echo $TIMEOUT)" = 'mytimeout' ]
35+
[ "$(echo $QUIET)" = 'myquiet' ]
36+
[ "$(echo $HOST)" = 'myhost' ]
37+
[ "$(echo $PORT)" = 'myport' ]
38+
[ "$(echo $LOOSE)" = 'myloose' ]
2339
}

0 commit comments

Comments
 (0)