Skip to content

Commit

Permalink
wrapped error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Синкевич Михаил Михайлович authored and umputun committed Jun 16, 2023
1 parent 27dfe6c commit 8ed8651
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
8 changes: 4 additions & 4 deletions repeater.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package repeater

import (
"context"
"errors"
"time"

"github.com/go-pkgz/repeater/strategy"
Expand Down Expand Up @@ -35,14 +36,13 @@ func NewDefault(repeats int, delay time.Duration) *Repeater {
}

// Do repeats fun till no error. Predefined (optional) errors terminate immediately
func (r Repeater) Do(ctx context.Context, fun func() error, errors ...error) (err error) {

func (r Repeater) Do(ctx context.Context, fun func() error, errs ...error) (err error) {
ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc() // ensure strategy's channel termination

inErrors := func(err error) bool {
for _, e := range errors {
if e == err {
for _, e := range errs {
if errors.Is(err, e) {
return true
}
}
Expand Down
30 changes: 23 additions & 7 deletions repeater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,24 @@ func TestRepeaterFixedCriticalError(t *testing.T) {
}

err := NewDefault(10, time.Millisecond).Do(context.Background(), fun, criticalErr)
assert.Equal(t, criticalErr, err)
assert.ErrorIs(t, err, criticalErr)
assert.Equal(t, 5, called, "called 5 times")
}

func TestRepeaterFixedCriticalErrorWrap(t *testing.T) {
criticalErr := errors.New("critical error")

called := 0
fun := func() error {
called++
if called == 5 {
return fmt.Errorf("wrap err: %w", criticalErr)
}
return errors.New("some error")
}

err := NewDefault(10, time.Millisecond).Do(context.Background(), fun, criticalErr)
assert.ErrorIs(t, err, criticalErr)
assert.Equal(t, 5, called, "called 5 times")
}

Expand Down Expand Up @@ -160,7 +177,7 @@ func TestRepeaterBackoffFailed(t *testing.T) {
}
called = 0
err = New(&strtg).Do(context.Background(), fun)
assert.Equal(t, e, err)
assert.ErrorIs(t, err, e)
assert.Equal(t, 1, called, "called 1 times")
}

Expand All @@ -183,10 +200,11 @@ func TestRepeaterBackoffCanceled(t *testing.T) {
}

err := New(&strtg).Do(ctx, fun)
require.NotNil(t, err)
require.Error(t, err)
assert.True(t, err.Error() == "context deadline exceeded" || err.Error() == "some error")
assert.Equal(t, 6, called)
}

func TestRepeaterOnce(t *testing.T) {
e := errors.New("some error")
called := 0
Expand All @@ -196,13 +214,13 @@ func TestRepeaterOnce(t *testing.T) {
}

err := New(&strategy.Once{}).Do(context.Background(), fun)
assert.Equal(t, e, err)
assert.ErrorIs(t, err, e)
assert.Equal(t, 1, called, "called 1 time")

called = 0
e = nil
err = New(&strategy.Once{}).Do(context.Background(), fun)
assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, 1, called, "called 1 time")
}

Expand All @@ -225,7 +243,6 @@ func TestRepeaterNil(t *testing.T) {
}

func TestRepeaterMemoryLeakFixed(t *testing.T) {

rep := func() {
called := 0
fun := func() error {
Expand Down Expand Up @@ -254,7 +271,6 @@ func TestRepeaterMemoryLeakFixed(t *testing.T) {
}

func TestRepeaterMemoryLeakBackOff(t *testing.T) {

rep := func() {
called := 0
fun := func() error {
Expand Down

0 comments on commit 8ed8651

Please sign in to comment.