Skip to content

Commit 23aee41

Browse files
larsxschneiderpeff
authored andcommitted
git-p4: retry kill/cleanup operations in tests with timeout
In rare cases kill/cleanup operations in tests fail. Retry these operations with a timeout to make the test less flaky. Signed-off-by: Lars Schneider <[email protected]> Signed-off-by: Jeff King <[email protected]>
1 parent 0c83680 commit 23aee41

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

t/lib-git-p4.sh

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
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+
913
. ./test-lib.sh
1014

1115
if ! test_have_prereq PYTHON
@@ -36,6 +40,15 @@ native_path() {
3640
echo "$path"
3741
}
3842

43+
# On Solaris the 'date +%s' function is not supported and therefore we
44+
# need this replacement.
45+
# Attention: This function is not safe again against time offset updates
46+
# at runtime (e.g. via NTP). The 'clock_gettime(CLOCK_MONOTONIC)'
47+
# function could fix that but it is not in Python until 3.3.
48+
time_in_seconds() {
49+
python -c 'import time; print int(time.time())'
50+
}
51+
3952
# Try to pick a unique port: guess a large number, then hope
4053
# no more than one of each test is running.
4154
#
@@ -121,22 +134,35 @@ p4_add_user() {
121134
EOF
122135
}
123136

137+
retry_until_success() {
138+
timeout=$(($(time_in_seconds) + $RETRY_TIMEOUT))
139+
until "$@" 2>/dev/null || test $(time_in_seconds) -gt $timeout
140+
do
141+
sleep 1
142+
done
143+
}
144+
145+
retry_until_fail() {
146+
timeout=$(($(time_in_seconds) + $RETRY_TIMEOUT))
147+
until ! "$@" 2>/dev/null || test $(time_in_seconds) -gt $timeout
148+
do
149+
sleep 1
150+
done
151+
}
152+
124153
kill_p4d() {
125154
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 &&
155+
retry_until_fail kill $pid
156+
retry_until_fail kill -9 $pid
132157
# complain if it would not die
133158
test_must_fail kill $pid >/dev/null 2>&1 &&
134159
rm -rf "$db" "$cli" "$pidfile"
135160
}
136161

137162
cleanup_git() {
138-
rm -rf "$git" &&
139-
mkdir "$git"
163+
retry_until_success rm -r "$git"
164+
test_must_fail test -d "$git" &&
165+
retry_until_success mkdir "$git"
140166
}
141167

142168
marshal_dump() {

0 commit comments

Comments
 (0)