From 7885e16b2cb0267bcc8b07cdd0eced14e8005864 Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Fri, 14 May 2021 13:44:45 +0300 Subject: [PATCH] feat: add ExpectedErrorf Convenient. Signed-off-by: Alexey Palazhchenko --- retry/constant_test.go | 10 +++++----- retry/exponential_test.go | 10 +++++----- retry/linear_test.go | 10 +++++----- retry/retry.go | 5 +++++ 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/retry/constant_test.go b/retry/constant_test.go index 5bbe508..a40f016 100644 --- a/retry/constant_test.go +++ b/retry/constant_test.go @@ -42,7 +42,7 @@ func Test_constantRetryer_Retry(t *testing.T) { f: func() error { count++ - return ExpectedError(fmt.Errorf("expected")) + return ExpectedErrorf("expected") }, }, expectedCount: 3, @@ -60,7 +60,7 @@ func Test_constantRetryer_Retry(t *testing.T) { f: func() error { count++ - return ExpectedError(fmt.Errorf("expected")) + return ExpectedErrorf("expected") }, }, expectedCount: 5, @@ -99,7 +99,7 @@ func Test_constantRetryer_Retry(t *testing.T) { return fmt.Errorf("unexpected") } - return ExpectedError(fmt.Errorf("unexpected")) + return ExpectedErrorf("unexpected") }, }, expectedCount: 2, @@ -120,7 +120,7 @@ func Test_constantRetryer_Retry(t *testing.T) { return nil } - return ExpectedError(fmt.Errorf("unexpected")) + return ExpectedErrorf("unexpected") }, }, expectedCount: 2, @@ -154,7 +154,7 @@ func Test_constantRetryer_Retry(t *testing.T) { f: func() error { count++ - return ExpectedError(fmt.Errorf("expected")) + return ExpectedErrorf("expected") }, }, expectedCount: 1, diff --git a/retry/exponential_test.go b/retry/exponential_test.go index c05c97f..ee2fca5 100644 --- a/retry/exponential_test.go +++ b/retry/exponential_test.go @@ -42,7 +42,7 @@ func Test_exponentialRetryer_Retry(t *testing.T) { f: func() error { count++ - return ExpectedError(fmt.Errorf("expected")) + return ExpectedErrorf("expected") }, }, expectedCount: 4, @@ -60,7 +60,7 @@ func Test_exponentialRetryer_Retry(t *testing.T) { f: func() error { count++ - return ExpectedError(fmt.Errorf("expected")) + return ExpectedErrorf("expected") }, }, expectedCount: 5, @@ -99,7 +99,7 @@ func Test_exponentialRetryer_Retry(t *testing.T) { return fmt.Errorf("unexpected") } - return ExpectedError(fmt.Errorf("unexpected")) + return ExpectedErrorf("unexpected") }, }, expectedCount: 2, @@ -120,7 +120,7 @@ func Test_exponentialRetryer_Retry(t *testing.T) { return nil } - return ExpectedError(fmt.Errorf("unexpected")) + return ExpectedErrorf("unexpected") }, }, expectedCount: 2, @@ -154,7 +154,7 @@ func Test_exponentialRetryer_Retry(t *testing.T) { f: func() error { count++ - return ExpectedError(fmt.Errorf("expected")) + return ExpectedErrorf("expected") }, }, expectedCount: 2, diff --git a/retry/linear_test.go b/retry/linear_test.go index 1f608ca..21c66b3 100644 --- a/retry/linear_test.go +++ b/retry/linear_test.go @@ -42,7 +42,7 @@ func Test_linearRetryer_Retry(t *testing.T) { f: func() error { count++ - return ExpectedError(fmt.Errorf("expected")) + return ExpectedErrorf("expected") }, }, expectedCount: 5, @@ -60,7 +60,7 @@ func Test_linearRetryer_Retry(t *testing.T) { f: func() error { count++ - return ExpectedError(fmt.Errorf("expected")) + return ExpectedErrorf("expected") }, }, expectedCount: 9, @@ -99,7 +99,7 @@ func Test_linearRetryer_Retry(t *testing.T) { return fmt.Errorf("unexpected") } - return ExpectedError(fmt.Errorf("unexpected")) + return ExpectedErrorf("unexpected") }, }, expectedCount: 1, @@ -120,7 +120,7 @@ func Test_linearRetryer_Retry(t *testing.T) { return nil } - return ExpectedError(fmt.Errorf("unexpected")) + return ExpectedErrorf("unexpected") }, }, expectedCount: 2, @@ -154,7 +154,7 @@ func Test_linearRetryer_Retry(t *testing.T) { f: func() error { count++ - return ExpectedError(fmt.Errorf("expected")) + return ExpectedErrorf("expected") }, }, expectedCount: 1, diff --git a/retry/retry.go b/retry/retry.go index 60c81f4..788d72e 100644 --- a/retry/retry.go +++ b/retry/retry.go @@ -159,6 +159,11 @@ func ExpectedError(err error) error { return expectedError{err} } +// ExpectedErrorf makes an expected error from given format and arguments. +func ExpectedErrorf(format string, a ...interface{}) error { + return ExpectedError(fmt.Errorf(format, a...)) +} + // UnexpectedError error represents an error that is unexpected by the retrying // function. This error is fatal. //