Skip to content

Commit fa7095e

Browse files
committed
Merge branch 'ls/p4-test-timeouts'
Work around some test flakiness with p4d. * ls/p4-test-timeouts: git-p4: add trap to kill p4d on test exit git-p4: add p4d timeout in tests git-p4: retry kill/cleanup operations in tests with timeout
2 parents e0dd81b + dfe90e8 commit fa7095e

File tree

1 file changed

+62
-9
lines changed

1 file changed

+62
-9
lines changed

t/lib-git-p4.sh

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
# a subdirectory called "$git"
77
TEST_NO_CREATE_REPO=NoThanks
88

9+
# Some operations require multiple attempts to be successful. Define
10+
# here the maximal retry timeout in seconds.
11+
RETRY_TIMEOUT=60
12+
13+
# Sometimes p4d seems to hang. Terminate the p4d process automatically after
14+
# the defined timeout in seconds.
15+
P4D_TIMEOUT=300
16+
917
. ./test-lib.sh
1018

1119
if ! test_have_prereq PYTHON
@@ -36,6 +44,15 @@ native_path() {
3644
echo "$path"
3745
}
3846

47+
# On Solaris the 'date +%s' function is not supported and therefore we
48+
# need this replacement.
49+
# Attention: This function is not safe again against time offset updates
50+
# at runtime (e.g. via NTP). The 'clock_gettime(CLOCK_MONOTONIC)'
51+
# function could fix that but it is not in Python until 3.3.
52+
time_in_seconds() {
53+
python -c 'import time; print int(time.time())'
54+
}
55+
3956
# Try to pick a unique port: guess a large number, then hope
4057
# no more than one of each test is running.
4158
#
@@ -57,6 +74,15 @@ cli="$TRASH_DIRECTORY/cli"
5774
git="$TRASH_DIRECTORY/git"
5875
pidfile="$TRASH_DIRECTORY/p4d.pid"
5976

77+
# Sometimes "prove" seems to hang on exit because p4d is still running
78+
cleanup() {
79+
if test -f "$pidfile"
80+
then
81+
kill -9 $(cat "$pidfile") 2>/dev/null && exit 255
82+
fi
83+
}
84+
trap cleanup EXIT
85+
6086
# git p4 submit generates a temp file, which will
6187
# not get cleaned up if the submission fails. Don't
6288
# clutter up /tmp on the test machine.
@@ -81,6 +107,19 @@ start_p4d() {
81107
# will be caught with the "kill -0" check below.
82108
i=${P4D_START_PATIENCE:-300}
83109
pid=$(cat "$pidfile")
110+
111+
timeout=$(($(time_in_seconds) + $P4D_TIMEOUT))
112+
while true
113+
do
114+
if test $(time_in_seconds) -gt $timeout
115+
then
116+
kill -9 $pid
117+
exit 1
118+
fi
119+
sleep 1
120+
done &
121+
watchdog_pid=$!
122+
84123
ready=
85124
while test $i -gt 0
86125
do
@@ -121,22 +160,36 @@ p4_add_user() {
121160
EOF
122161
}
123162

163+
retry_until_success() {
164+
timeout=$(($(time_in_seconds) + $RETRY_TIMEOUT))
165+
until "$@" 2>/dev/null || test $(time_in_seconds) -gt $timeout
166+
do
167+
sleep 1
168+
done
169+
}
170+
171+
retry_until_fail() {
172+
timeout=$(($(time_in_seconds) + $RETRY_TIMEOUT))
173+
until ! "$@" 2>/dev/null || test $(time_in_seconds) -gt $timeout
174+
do
175+
sleep 1
176+
done
177+
}
178+
124179
kill_p4d() {
125180
pid=$(cat "$pidfile")
126-
# it had better exist for the first kill
127-
kill $pid &&
128-
for i in 1 2 3 4 5 ; do
129-
kill $pid >/dev/null 2>&1 || break
130-
sleep 1
131-
done &&
181+
retry_until_fail kill $pid
182+
retry_until_fail kill -9 $pid
132183
# complain if it would not die
133184
test_must_fail kill $pid >/dev/null 2>&1 &&
134-
rm -rf "$db" "$cli" "$pidfile"
185+
rm -rf "$db" "$cli" "$pidfile" &&
186+
retry_until_fail kill -9 $watchdog_pid
135187
}
136188

137189
cleanup_git() {
138-
rm -rf "$git" &&
139-
mkdir "$git"
190+
retry_until_success rm -r "$git"
191+
test_must_fail test -d "$git" &&
192+
retry_until_success mkdir "$git"
140193
}
141194

142195
marshal_dump() {

0 commit comments

Comments
 (0)