Skip to content

Commit 16750d0

Browse files
committed
t/lib-git-daemon: try harder to find a port
As with the previous commit, try harder to find an open port to avoid intermittent failures on busy/shared build systems. By default, we make 3 attempts. This may be overridden by setting GIT_TEST_START_GIT_DAEMON_TRIES to a different value. Signed-off-by: Todd Zullinger <tmz@pobox.com>
1 parent aedeaaf commit 16750d0

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

t/lib-git-daemon.sh

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,44 @@ start_git_daemon() {
5151
registered_stop_git_daemon_atexit_handler=AlreadyDone
5252
fi
5353

54-
say >&3 "Starting git daemon ..."
55-
mkfifo git_daemon_output
56-
${LIB_GIT_DAEMON_COMMAND:-git daemon} \
57-
--listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \
58-
--reuseaddr --verbose --pid-file="$GIT_DAEMON_PIDFILE" \
59-
--base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
60-
"$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
61-
>&3 2>git_daemon_output &
62-
GIT_DAEMON_PID=$!
63-
{
64-
read -r line <&7
65-
printf "%s\n" "$line" >&4
66-
cat <&7 >&4 &
67-
} 7<git_daemon_output &&
54+
i=0
55+
while test $i -lt ${GIT_TEST_START_GIT_DAEMON_TRIES:-3}
56+
do
57+
say >&3 "Starting git daemon on port $LIB_GIT_DAEMON_PORT ..."
58+
mkfifo git_daemon_output
59+
${LIB_GIT_DAEMON_COMMAND:-git daemon} \
60+
--listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \
61+
--reuseaddr --verbose --pid-file="$GIT_DAEMON_PIDFILE" \
62+
--base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
63+
"$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
64+
>&3 2>git_daemon_output &
65+
GIT_DAEMON_PID=$!
66+
{
67+
read -r line <&7
68+
printf "%s\n" "$line" >&4
69+
cat <&7 >&4 &
70+
} 7<git_daemon_output &&
6871

69-
# Check expected output
70-
if test x"$(expr "$line" : "\[[0-9]*\] \(.*\)")" != x"Ready to rumble"
71-
then
72-
kill "$GIT_DAEMON_PID"
73-
wait "$GIT_DAEMON_PID"
74-
unset GIT_DAEMON_PID
75-
test_skip_or_die GIT_TEST_GIT_DAEMON \
76-
"git daemon failed to start"
77-
fi
72+
# Check expected output
73+
output="$(expr "$line" : "\[[0-9]*\] \(.*\)")"
74+
# Return if found
75+
test x"$output" = x"Ready to rumble" && return
76+
# Increment port for retry if not found
77+
LIB_GIT_DAEMON_PORT=$(($LIB_GIT_DAEMON_PORT + 1))
78+
export LIB_GIT_DAEMON_PORT
79+
GIT_DAEMON_HOST_PORT=127.0.0.1:$LIB_GIT_DAEMON_PORT
80+
GIT_DAEMON_URL=git://$GIT_DAEMON_HOST_PORT
81+
# unset GIT_DAEMON_PID; remove the fifo & pid file
82+
GIT_DAEMON_PID=
83+
rm -f git_daemon_output "$GIT_DAEMON_PIDFILE"
84+
done
85+
86+
# Clean up and return failure
87+
kill "$GIT_DAEMON_PID"
88+
wait "$GIT_DAEMON_PID"
89+
unset GIT_DAEMON_PID
90+
test_skip_or_die GIT_TEST_GIT_DAEMON \
91+
"git daemon failed to start"
7892
}
7993

8094
stop_git_daemon() {

0 commit comments

Comments
 (0)