2014-07-01 03:03:27 +02:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
2014-10-08 06:44:51 +02:00
|
|
|
"fmt"
|
2014-07-01 03:03:27 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
func TestRetry(t *testing.T) {
|
|
|
|
t.Parallel()
|
2014-07-01 03:03:27 +02:00
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
tries := 0
|
2016-03-09 23:53:32 +01:00
|
|
|
f := func() *RetryError {
|
2014-10-08 06:44:51 +02:00
|
|
|
tries++
|
2016-03-10 15:10:34 +01:00
|
|
|
if tries == 3 {
|
2014-10-08 06:44:51 +02:00
|
|
|
return nil
|
|
|
|
}
|
2014-07-01 03:03:27 +02:00
|
|
|
|
2016-03-09 23:53:32 +01:00
|
|
|
return RetryableError(fmt.Errorf("error"))
|
2014-07-01 03:03:27 +02:00
|
|
|
}
|
|
|
|
|
2016-03-10 15:10:34 +01:00
|
|
|
err := Retry(10*time.Second, f)
|
2014-07-01 03:03:27 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-07-08 00:21:54 +02:00
|
|
|
}
|
|
|
|
|
2017-04-19 18:14:23 +02:00
|
|
|
// make sure a slow StateRefreshFunc is allowed to complete after timeout
|
|
|
|
func TestRetry_grace(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := func() *RetryError {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := Retry(10*time.Millisecond, f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
func TestRetry_timeout(t *testing.T) {
|
|
|
|
t.Parallel()
|
2014-07-01 03:18:36 +02:00
|
|
|
|
2016-03-09 23:53:32 +01:00
|
|
|
f := func() *RetryError {
|
|
|
|
return RetryableError(fmt.Errorf("always"))
|
2014-07-01 03:03:27 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:50:35 +02:00
|
|
|
err := Retry(1*time.Second, f)
|
2014-10-08 06:44:51 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
2014-07-01 03:18:36 +02:00
|
|
|
}
|
2014-07-01 03:03:27 +02:00
|
|
|
}
|
2014-10-18 03:28:03 +02:00
|
|
|
|
2016-07-01 03:07:56 +02:00
|
|
|
func TestRetry_hang(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
f := func() *RetryError {
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := Retry(1*time.Second, f)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-18 03:28:03 +02:00
|
|
|
func TestRetry_error(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
expected := fmt.Errorf("nope")
|
2016-03-09 23:53:32 +01:00
|
|
|
f := func() *RetryError {
|
|
|
|
return NonRetryableError(expected)
|
2014-10-18 03:28:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
errCh <- Retry(1*time.Second, f)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != expected {
|
|
|
|
t.Fatalf("bad: %#v", err)
|
|
|
|
}
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Fatal("timeout")
|
|
|
|
}
|
|
|
|
}
|