Compare commits
1 Commits
25482352b4
...
526c0b8ac3
Author | SHA1 | Date |
---|---|---|
p4u1 | 526c0b8ac3 |
|
@ -148,6 +148,9 @@ flag.
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// retryFunc retries the given function for the given retries. After the nth
|
||||||
|
// retry it waits (n + 1)^2 seconds before the next retry (starting with n=0).
|
||||||
|
// It returns an error if the function still failed after the last retry.
|
||||||
func retryFunc(retries int, fn func() error) error {
|
func retryFunc(retries int, fn func() error) error {
|
||||||
for i := 0; i < retries; i++ {
|
for i := 0; i < retries; i++ {
|
||||||
err := fn()
|
err := fn()
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRetryFunc(t *testing.T) {
|
||||||
|
err := retryFunc(1, func() error { return nil })
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("should not return an error: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
i := 0
|
||||||
|
fn := func() error {
|
||||||
|
i++
|
||||||
|
return fmt.Errorf("oh no, something went wrong!")
|
||||||
|
}
|
||||||
|
err = retryFunc(2, fn)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("should return an error")
|
||||||
|
}
|
||||||
|
if i != 2 {
|
||||||
|
t.Errorf("The function should have been called 1 times, got %d", i)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue