Skip to content

Commit

Permalink
Merge pull request #6 from avast/fix_default_units
Browse files Browse the repository at this point in the history
fix default unit
  • Loading branch information
JaSei authored Mar 19, 2018
2 parents a203817 + 0a80bef commit dc6fb01
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ nonintuitive interface (for me)
* `retry.Retry` function are changed to `retry.Do` function

* `retry.RetryCustom` (OnRetry) and `retry.RetryCustomWithOpts` functions are
now implement via functions produces Options (aka `retry.OnRetryFunction`)
now implement via functions produces Options (aka `retry.OnRetry`)

## Usage

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
1.0.1
1 change: 1 addition & 0 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error {
config := &config{
attempts: 10,
delay: 1e5,
units: time.Microsecond,
onRetry: func(n uint, err error) {},
retryIf: func(err error) bool { return true },
}
Expand Down
26 changes: 21 additions & 5 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestDo(t *testing.T) {
func TestDoAllFailed(t *testing.T) {
var retrySum uint
err := Do(
func() error { return errors.New("test") },
Expand All @@ -30,17 +30,22 @@ func TestDo(t *testing.T) {
#10: test`
assert.Equal(t, expectedErrorFormat, err.Error(), "retry error format")
assert.Equal(t, uint(45), retrySum, "right count of retry")
}

retrySum = 0
err = Do(
func TestDoFirstOk(t *testing.T) {
var retrySum uint
err := Do(
func() error { return nil },
OnRetry(func(n uint, err error) { retrySum += n }),
)
assert.NoError(t, err)
assert.Equal(t, uint(0), retrySum, "no retry")

}

func TestRetryIf(t *testing.T) {
var retryCount uint
err = Do(
err := Do(
func() error {
if retryCount >= 2 {
return errors.New("special")
Expand All @@ -56,11 +61,22 @@ func TestDo(t *testing.T) {
)
assert.Error(t, err)

expectedErrorFormat = `All attempts fail:
expectedErrorFormat := `All attempts fail:
#1: test
#2: test
#3: special`
assert.Equal(t, expectedErrorFormat, err.Error(), "retry error format")
assert.Equal(t, uint(3), retryCount, "right count of retry")

}

func TestDefaultSleep(t *testing.T) {
start := time.Now()
err := Do(
func() error { return errors.New("test") },
Attempts(3),
)
dur := time.Since(start)
assert.Error(t, err)
assert.True(t, dur > 10*time.Millisecond, "3 times default retry is longer then 10ms")
}

0 comments on commit dc6fb01

Please sign in to comment.