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
|
|
|
|
f := func() error {
|
|
|
|
tries++
|
|
|
|
if tries == 1 {
|
|
|
|
return nil
|
|
|
|
}
|
2014-07-01 03:03:27 +02:00
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
return fmt.Errorf("error")
|
2014-07-01 03:03:27 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 23:50:35 +02:00
|
|
|
err := Retry(2*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
|
|
|
}
|
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
func TestRetry_timeout(t *testing.T) {
|
|
|
|
t.Parallel()
|
2014-07-01 03:18:36 +02:00
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
f := func() error {
|
|
|
|
return 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
|
|
|
}
|