| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package retry |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"os" |
| 21 |
"testing" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/googleapis/gax-go/v2" |
| 25 |
) |
| 26 |
|
| 27 |
// Errors to distinguish retryable and non-retryable cases. |
| 28 |
var ( |
| 29 |
errRetry = errors.New("retry") |
| 30 |
errNoRetry = errors.New("no retry") |
| 31 |
) |
| 32 |
|
| 33 |
func retryable(err error) bool { |
| 34 |
return err == errRetry |
| 35 |
} |
| 36 |
|
| 37 |
func TestCall(t *testing.T) { |
| 38 |
for _, test := range []struct { |
| 39 |
desc string |
| 40 |
isRetryable func(error) bool |
| 41 |
f func(int) error // passed the number of calls so far |
| 42 |
wantErr error // the return value of call |
| 43 |
wantCount int // number of times f is called |
| 44 |
}{ |
| 45 |
{ |
| 46 |
desc: "f returns nil", |
| 47 |
isRetryable: retryable, |
| 48 |
f: func(int) error { return nil }, |
| 49 |
wantCount: 1, |
| 50 |
wantErr: nil, |
| 51 |
}, |
| 52 |
{ |
| 53 |
desc: "f returns non-retryable error", |
| 54 |
isRetryable: retryable, |
| 55 |
f: func(int) error { return errNoRetry }, |
| 56 |
wantCount: 1, |
| 57 |
wantErr: errNoRetry, |
| 58 |
}, |
| 59 |
{ |
| 60 |
desc: "f returns retryable error", |
| 61 |
isRetryable: retryable, |
| 62 |
f: func(n int) error { |
| 63 |
if n < 2 { |
| 64 |
return errRetry |
| 65 |
} |
| 66 |
return errNoRetry |
| 67 |
}, |
| 68 |
wantCount: 3, |
| 69 |
wantErr: errNoRetry, |
| 70 |
}, |
| 71 |
{ |
| 72 |
desc: "f returns context error", // same as non-retryable |
| 73 |
isRetryable: retryable, |
| 74 |
f: func(int) error { return context.Canceled }, |
| 75 |
wantCount: 1, |
| 76 |
wantErr: context.Canceled, |
| 77 |
}, |
| 78 |
} { |
| 79 |
t.Run(test.desc, func(t *testing.T) { |
| 80 |
sleep := func(context.Context, time.Duration) error { return nil } |
| 81 |
gotCount := 0 |
| 82 |
f := func() error { gotCount++; return test.f(gotCount - 1) } |
| 83 |
gotErr := call(context.Background(), gax.Backoff{}, test.isRetryable, f, sleep) |
| 84 |
if gotErr != test.wantErr { |
| 85 |
t.Errorf("error: got %v, want %v", gotErr, test.wantErr) |
| 86 |
} |
| 87 |
if gotCount != test.wantCount { |
| 88 |
t.Errorf("retry count: got %d, want %d", gotCount, test.wantCount) |
| 89 |
} |
| 90 |
}) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestCallCancel(t *testing.T) { |
| 95 |
t.Run("done on entry", func(t *testing.T) { |
| 96 |
// If the context is done on entry, f is never called. |
| 97 |
ctx, cancel := context.WithCancel(context.Background()) |
| 98 |
cancel() |
| 99 |
gotCount := 0 |
| 100 |
f := func() error { gotCount++; return nil } |
| 101 |
gotErr := call(ctx, gax.Backoff{}, retryable, f, nil) |
| 102 |
if gotCount != 0 { |
| 103 |
t.Errorf("retry count: got %d, want 0", gotCount) |
| 104 |
} |
| 105 |
wantErr := &ContextError{CtxErr: context.Canceled} |
| 106 |
if !equalContextError(gotErr, wantErr) { |
| 107 |
t.Errorf("error: got %v, want %v", gotErr, wantErr) |
| 108 |
} |
| 109 |
}) |
| 110 |
t.Run("done in sleep", func(t *testing.T) { |
| 111 |
// If the context is done during sleep, we get a ContextError. |
| 112 |
gotCount := 0 |
| 113 |
f := func() error { gotCount++; return errRetry } |
| 114 |
sleep := func(context.Context, time.Duration) error { return context.Canceled } |
| 115 |
gotErr := call(context.Background(), gax.Backoff{}, retryable, f, sleep) |
| 116 |
if gotCount != 1 { |
| 117 |
t.Errorf("retry count: got %d, want 1", gotCount) |
| 118 |
} |
| 119 |
wantErr := &ContextError{CtxErr: context.Canceled, FuncErr: errRetry} |
| 120 |
if !equalContextError(gotErr, wantErr) { |
| 121 |
t.Errorf("error: got %v, want %v", gotErr, wantErr) |
| 122 |
} |
| 123 |
}) |
| 124 |
} |
| 125 |
|
| 126 |
func equalContextError(got error, want *ContextError) bool { |
| 127 |
cerr, ok := got.(*ContextError) |
| 128 |
if !ok { |
| 129 |
return false |
| 130 |
} |
| 131 |
return cerr.CtxErr == want.CtxErr && cerr.FuncErr == want.FuncErr |
| 132 |
} |
| 133 |
|
| 134 |
func TestErrorsIs(t *testing.T) { |
| 135 |
err := &ContextError{ |
| 136 |
CtxErr: context.Canceled, |
| 137 |
FuncErr: os.ErrExist, |
| 138 |
} |
| 139 |
for _, target := range []error{err, context.Canceled, os.ErrExist} { |
| 140 |
if !errors.Is(err, target) { |
| 141 |
t.Errorf("xerrors.Is(%v) == false, want true", target) |
| 142 |
} |
| 143 |
} |
| 144 |
} |