| 180 | } |
| 181 | |
| 182 | func 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 | |
| 215 | func TestRetryLoopFastFail(t *testing.T) { |
| 216 | numTimesInvoked := 0 |