MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestRetryLoop

Function TestRetryLoop

base/util_test.go:182–213  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

180}
181
182func TestRetryLoop(t *testing.T) {
183
184 // Make sure that the worker retries if an error is returned and shouldRetry == true
185
186 numTimesInvoked := 0
187 worker := func() (shouldRetry bool, err error, value interface{}) {
188 log.Printf("Worker invoked")
189 numTimesInvoked += 1
190 if numTimesInvoked <= 3 {
191 log.Printf("Worker returning shouldRetry true, fake error")
192 return true, fmt.Errorf("Fake error"), nil
193 }
194 return false, nil, "result"
195 }
196
197 sleeper := func(numAttempts int) (bool, int) {
198 if numAttempts > 10 {
199 return false, -1
200 }
201 return true, 0
202 }
203
204 // Kick off retry loop
205 description := fmt.Sprintf("TestRetryLoop")
206 err, result := RetryLoop(TestCtx(t), description, worker, sleeper)
207
208 // We shouldn't get an error, because it will retry a few times and then succeed
209 assert.True(t, err == nil)
210 assert.Equal(t, "result", result)
211 assert.True(t, numTimesInvoked == 4)
212
213}
214
215func TestRetryLoopFastFail(t *testing.T) {
216 numTimesInvoked := 0

Callers

nothing calls this directly

Calls 4

RetryLoopFunction · 0.85
TestCtxFunction · 0.85
ErrorfMethod · 0.80
EqualMethod · 0.45

Tested by

no test coverage detected