Skip to content

Commit

Permalink
[+]: Retry wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
cthulhu committed Feb 15, 2018
1 parent d4a57fd commit f8ad89e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Handy misc tools for go development
* Structs - Set/Get helpers to write/read random fields from structs by their string names
* TimeId - a set of helpers to work with Date/Time as a timeID in YYDDMM format
* URI - a set of helpers duplicating functionality of JS's encodeuricomponent/decodeuricomponent and others
* Retry - simple retry wrapper, supports Before and After retry

## Installation

Expand Down
33 changes: 33 additions & 0 deletions retry/retry.go
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) {}
13 changes: 13 additions & 0 deletions retry/retry_suite_test.go
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")
}
55 changes: 55 additions & 0 deletions retry/retry_test.go
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())
})
})
})

0 comments on commit f8ad89e

Please sign in to comment.