2016-09-28 21:06:42 -04:00
|
|
|
package common
|
2016-05-24 20:13:36 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRetry(t *testing.T) {
|
|
|
|
numTries := uint(0)
|
|
|
|
// Test that a passing function only gets called once.
|
|
|
|
err := Retry(0, 0, 0, func() (bool, error) {
|
|
|
|
numTries++
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
if numTries != 1 {
|
|
|
|
t.Fatal("Passing function should not have been retried.")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Passing function should not have returned a retry error. Error: %s", err)
|
|
|
|
}
|
2016-09-28 21:06:42 -04:00
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
// Test that a failing function gets retried (once in this example).
|
|
|
|
numTries = 0
|
|
|
|
results := []bool{false, true}
|
|
|
|
err = Retry(0, 0, 0, func() (bool, error) {
|
|
|
|
result := results[numTries]
|
|
|
|
numTries++
|
|
|
|
return result, nil
|
|
|
|
})
|
|
|
|
if numTries != 2 {
|
|
|
|
t.Fatalf("Retried function should have been tried twice. Tried %d times.", numTries)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Successful retried function should not have returned a retry error. Error: %s", err)
|
|
|
|
}
|
2016-09-28 21:06:42 -04:00
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
// Test that a function error gets returned, and the function does not get called again.
|
|
|
|
numTries = 0
|
|
|
|
funcErr := fmt.Errorf("This function had an error!")
|
|
|
|
err = Retry(0, 0, 0, func() (bool, error) {
|
|
|
|
numTries++
|
|
|
|
return false, funcErr
|
|
|
|
})
|
|
|
|
if numTries != 1 {
|
|
|
|
t.Fatal("Errant function should not have been retried.")
|
|
|
|
}
|
|
|
|
if err != funcErr {
|
|
|
|
t.Fatalf("Errant function did not return the right error %s. Error: %s", funcErr, err)
|
|
|
|
}
|
2016-09-28 21:06:42 -04:00
|
|
|
|
2016-05-24 20:13:36 -04:00
|
|
|
// Test when a function exhausts its retries.
|
|
|
|
numTries = 0
|
|
|
|
expectedTries := uint(3)
|
|
|
|
err = Retry(0, 0, expectedTries, func() (bool, error) {
|
|
|
|
numTries++
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
if numTries != expectedTries {
|
2017-03-28 20:45:01 -04:00
|
|
|
t.Fatalf("Unsuccessful retry function should have been called %d times. Only called %d times.", expectedTries, numTries)
|
2016-05-24 20:13:36 -04:00
|
|
|
}
|
|
|
|
if err != RetryExhaustedError {
|
|
|
|
t.Fatalf("Unsuccessful retry function should have returned a retry exhausted error. Actual error: %s", err)
|
|
|
|
}
|
2016-09-28 21:06:42 -04:00
|
|
|
}
|