Skip to content

Commit

Permalink
Simplify WithSurfaceWorkErrors tests, add inverse behavior test
Browse files Browse the repository at this point in the history
  • Loading branch information
slizco committed Jul 18, 2024
1 parent 88f4803 commit 029d178
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions retrier/retrier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,40 +153,48 @@ func TestRetrierRunFnWithInfinite(t *testing.T) {
}
}

func TestRetrierCtxSurfaceWorkErrors(t *testing.T) {
// Will timeout before getting to errBar.
ctx, cancel := context.WithTimeout(context.Background(), 7*time.Millisecond)
func TestRetrierRunFnWithSurfaceWorkErrors(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r := New([]time.Duration{0, 10 * time.Millisecond}, nil).WithSurfaceWorkErrors()
errExpected := []error{errFoo, errFoo, errBar, errBaz}
retries := 0
err := r.RunCtx(ctx, func(ctx context.Context) error {
errExpected := []error{errFoo, errBar, errBaz}

err := r.RunFn(ctx, func(ctx context.Context, retries int) error {
if retries >= len(errExpected) {
return nil
}
if retries == 1 {
// Context canceled inside second call to work function.
cancel()
}
err := errExpected[retries]
retries++
return err
})
if err != errFoo {
if err != errBar {
t.Error(err)
}
}

func TestRetrierRunFnWithSurfaceWorkErrors(t *testing.T) {
// Will timeout before getting to errBar.
ctx, cancel := context.WithTimeout(context.Background(), 7*time.Millisecond)
func TestRetrierRunFnWithoutSurfaceWorkErrors(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r := New([]time.Duration{0, 10 * time.Millisecond}, nil).WithSurfaceWorkErrors()
errExpected := []error{errFoo, errFoo, errFoo, errBar, errBaz}
r := New([]time.Duration{0, 10 * time.Millisecond}, nil)
errExpected := []error{errFoo, errBar, errBaz}

err := r.RunFn(ctx, func(ctx context.Context, retries int) error {
if retries >= len(errExpected) {
return nil
}
return errExpected[retries]
if retries == 1 {
// Context canceled inside second call to work function.
cancel()
}
err := errExpected[retries]
retries++
return err
})
if err != errFoo {
if err != context.Canceled {
t.Error(err)
}
}
Expand Down

0 comments on commit 029d178

Please sign in to comment.