Skip to content

Commit 029d178

Browse files
committed
Simplify WithSurfaceWorkErrors tests, add inverse behavior test
1 parent 88f4803 commit 029d178

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

retrier/retrier_test.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,40 +153,48 @@ func TestRetrierRunFnWithInfinite(t *testing.T) {
153153
}
154154
}
155155

156-
func TestRetrierCtxSurfaceWorkErrors(t *testing.T) {
157-
// Will timeout before getting to errBar.
158-
ctx, cancel := context.WithTimeout(context.Background(), 7*time.Millisecond)
156+
func TestRetrierRunFnWithSurfaceWorkErrors(t *testing.T) {
157+
ctx, cancel := context.WithCancel(context.Background())
159158
defer cancel()
160159
r := New([]time.Duration{0, 10 * time.Millisecond}, nil).WithSurfaceWorkErrors()
161-
errExpected := []error{errFoo, errFoo, errBar, errBaz}
162-
retries := 0
163-
err := r.RunCtx(ctx, func(ctx context.Context) error {
160+
errExpected := []error{errFoo, errBar, errBaz}
161+
162+
err := r.RunFn(ctx, func(ctx context.Context, retries int) error {
164163
if retries >= len(errExpected) {
165164
return nil
166165
}
166+
if retries == 1 {
167+
// Context canceled inside second call to work function.
168+
cancel()
169+
}
167170
err := errExpected[retries]
168171
retries++
169172
return err
170173
})
171-
if err != errFoo {
174+
if err != errBar {
172175
t.Error(err)
173176
}
174177
}
175178

176-
func TestRetrierRunFnWithSurfaceWorkErrors(t *testing.T) {
177-
// Will timeout before getting to errBar.
178-
ctx, cancel := context.WithTimeout(context.Background(), 7*time.Millisecond)
179+
func TestRetrierRunFnWithoutSurfaceWorkErrors(t *testing.T) {
180+
ctx, cancel := context.WithCancel(context.Background())
179181
defer cancel()
180-
r := New([]time.Duration{0, 10 * time.Millisecond}, nil).WithSurfaceWorkErrors()
181-
errExpected := []error{errFoo, errFoo, errFoo, errBar, errBaz}
182+
r := New([]time.Duration{0, 10 * time.Millisecond}, nil)
183+
errExpected := []error{errFoo, errBar, errBaz}
182184

183185
err := r.RunFn(ctx, func(ctx context.Context, retries int) error {
184186
if retries >= len(errExpected) {
185187
return nil
186188
}
187-
return errExpected[retries]
189+
if retries == 1 {
190+
// Context canceled inside second call to work function.
191+
cancel()
192+
}
193+
err := errExpected[retries]
194+
retries++
195+
return err
188196
})
189-
if err != errFoo {
197+
if err != context.Canceled {
190198
t.Error(err)
191199
}
192200
}

0 commit comments

Comments
 (0)