2016-12-08 12:32:12 +00:00
|
|
|
package safe
|
|
|
|
|
|
|
|
import (
|
2016-12-08 12:32:52 +00:00
|
|
|
"fmt"
|
2016-12-08 12:32:12 +00:00
|
|
|
"github.com/cenk/backoff"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestOperationWithRecover(t *testing.T) {
|
2016-12-08 12:32:52 +00:00
|
|
|
operation := func() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := backoff.Retry(OperationWithRecover(operation), &backoff.StopBackOff{})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error in OperationWithRecover: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperationWithRecoverPanic(t *testing.T) {
|
2016-12-08 12:32:12 +00:00
|
|
|
operation := func() error {
|
|
|
|
panic("BOOM")
|
|
|
|
}
|
|
|
|
err := backoff.Retry(OperationWithRecover(operation), &backoff.StopBackOff{})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Error in OperationWithRecover: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2016-12-08 12:32:52 +00:00
|
|
|
|
|
|
|
func TestOperationWithRecoverError(t *testing.T) {
|
|
|
|
operation := func() error {
|
|
|
|
return fmt.Errorf("ERROR")
|
|
|
|
}
|
|
|
|
err := backoff.Retry(OperationWithRecover(operation), &backoff.StopBackOff{})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Error in OperationWithRecover: %s", err)
|
|
|
|
}
|
|
|
|
}
|