diff --git a/README.md b/README.md index e9925a4..6e8ae82 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/VERSION b/VERSION index 3eefcb9..7dea76e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0.0 +1.0.1 diff --git a/retry.go b/retry.go index f2d3389..4f2543b 100644 --- a/retry.go +++ b/retry.go @@ -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 }, } diff --git a/retry_test.go b/retry_test.go index b8dc74f..fe2bbcb 100644 --- a/retry_test.go +++ b/retry_test.go @@ -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") }, @@ -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") @@ -56,7 +61,7 @@ func TestDo(t *testing.T) { ) assert.Error(t, err) - expectedErrorFormat = `All attempts fail: + expectedErrorFormat := `All attempts fail: #1: test #2: test #3: special` @@ -64,3 +69,14 @@ func TestDo(t *testing.T) { 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") +}