-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package retry | ||
|
||
type RetryOperation struct { | ||
BeforeRetryCallback func(error) | ||
AfterRetryCallback func(error) | ||
FinalError error | ||
} | ||
|
||
// Builds new retryable operation | ||
func New() *RetryOperation { | ||
return &RetryOperation{dummyCallback, dummyCallback, nil} | ||
} | ||
|
||
// Setup custom action before | ||
func (rop *RetryOperation) BeforeRetry(before func(error)) { | ||
rop.BeforeRetryCallback = before | ||
} | ||
|
||
// Setup custom action after | ||
func (rop *RetryOperation) AfterRetry(after func(error)) { | ||
rop.AfterRetryCallback = after | ||
} | ||
|
||
// Execute operation with retry | ||
func (rop *RetryOperation) Do(mainFunc func() error) { | ||
if err := mainFunc(); err != nil { | ||
rop.BeforeRetryCallback(err) | ||
rop.FinalError = mainFunc() | ||
rop.AfterRetryCallback(err) | ||
} | ||
} | ||
|
||
func dummyCallback(err error) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package retry_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"testing" | ||
) | ||
|
||
func TestRetry(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Retry Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package retry_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
. "github.com/cthulhu/go-steun/retry" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Retry", func() { | ||
operation := New() | ||
var Retried bool | ||
var RetriedWithError error | ||
BeforeEach(func() { | ||
RetriedWithError = nil | ||
Retried = false | ||
}) | ||
Context("No error", func() { | ||
It("excutes only main function", func() { | ||
operation.BeforeRetry(func(err error) { | ||
Retried = true | ||
RetriedWithError = err | ||
}) | ||
operation.Do(func() error { | ||
return nil | ||
}) | ||
Expect(RetriedWithError).To(BeNil()) | ||
Expect(Retried).To(BeFalse()) | ||
}) | ||
}) | ||
Context("With error", func() { | ||
It("excutes only main function", func() { | ||
operation.BeforeRetry(func(err error) { | ||
Retried = true | ||
RetriedWithError = err | ||
}) | ||
operation.Do(func() error { | ||
return fmt.Errorf("Generic Error") | ||
}) | ||
Expect(RetriedWithError).To(HaveOccurred()) | ||
Expect(Retried).To(BeTrue()) | ||
}) | ||
}) | ||
Context("With error no retry", func() { | ||
It("excutes only main function", func() { | ||
operation.Do(func() error { | ||
return fmt.Errorf("Generic Error") | ||
}) | ||
Expect(RetriedWithError).To(HaveOccurred()) | ||
Expect(Retried).To(BeTrue()) | ||
}) | ||
}) | ||
}) |