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 <larsxschneider@gmail.com> Signed-off-by: Jeff King <peff@peff.net>

Lars Schneider committed Nov 19, 2015 at 09:58 UTC 23aee4199a69fae861f9d173ee0ef1c2a7fab0d5
1 file changed +34 -8
t/lib-git-p4.sh
+34 -8
@@ -6,6 +6,10 @@
6 # a subdirectory called "$git"
7 TEST_NO_CREATE_REPO=NoThanks
8
9 +# Some operations require multiple attempts to be successful. Define
10 +# here the maximal retry timeout in seconds.
11 +RETRY_TIMEOUT=60
12 +
13 . ./test-lib.sh
14
15 if ! test_have_prereq PYTHON
@@ -36,6 +40,15 @@ native_path() {
40 echo "$path"
41 }
42
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 +
52 # Try to pick a unique port: guess a large number, then hope
53 # no more than one of each test is running.
54 #
@@ -121,22 +134,35 @@ p4_add_user() {
134 EOF
135 }
136
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 +
153 kill_p4d() {
154 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
157 # complain if it would not die
158 test_must_fail kill $pid >/dev/null 2>&1 &&
159 rm -rf "$db" "$cli" "$pidfile"
160 }
161
162 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"
166 }
167
168 marshal_dump() {