-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathretry_test.go
156 lines (147 loc) · 3.81 KB
/
retry_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package aviation
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
)
func TestMakeUnaryClientInterceptor(t *testing.T) {
for _, test := range []struct {
name string
ctxTimeout time.Duration
grpcCodes []codes.Code
errorUntil int
retries int
expectedAttempts int
hasErr bool
expectedMinTime time.Duration
expectedMaxTime time.Duration
}{
{
name: "NoRetryFail",
grpcCodes: []codes.Code{
codes.Canceled,
codes.InvalidArgument,
codes.DeadlineExceeded,
codes.NotFound,
codes.AlreadyExists,
codes.PermissionDenied,
codes.FailedPrecondition,
codes.OutOfRange,
codes.Unimplemented,
codes.DataLoss,
codes.Unauthenticated,
},
errorUntil: 1,
retries: 10,
expectedAttempts: 1,
hasErr: true,
expectedMaxTime: 100 * time.Millisecond,
},
{
name: "NoRetrySuccess",
grpcCodes: []codes.Code{codes.OK},
errorUntil: 0,
retries: 10,
expectedAttempts: 1,
expectedMaxTime: 100 * time.Millisecond,
},
{
name: "RetryFail",
grpcCodes: []codes.Code{
codes.Unknown,
codes.ResourceExhausted,
codes.Aborted,
codes.Internal,
codes.Unavailable,
},
errorUntil: 3,
retries: 3,
expectedAttempts: 3,
hasErr: true,
expectedMinTime: 300 * time.Millisecond,
expectedMaxTime: 1000 * time.Millisecond,
},
{
name: "RetrySuccess",
grpcCodes: []codes.Code{
codes.Unknown,
codes.ResourceExhausted,
codes.Aborted,
codes.Internal,
codes.Unavailable,
},
errorUntil: 3,
retries: 10,
expectedAttempts: 4,
expectedMinTime: 700 * time.Millisecond,
expectedMaxTime: 1000 * time.Millisecond,
},
{
name: "ParentContextInterrupt",
ctxTimeout: 150 * time.Millisecond,
grpcCodes: []codes.Code{codes.Unknown},
errorUntil: 3,
retries: 10,
expectedAttempts: 2,
hasErr: true,
expectedMinTime: 100 * time.Millisecond,
expectedMaxTime: 200 * time.Millisecond,
},
} {
t.Run(test.name, func(t *testing.T) {
for _, code := range test.grpcCodes {
// unary
opts := &mockClientOptions{
code: code,
errorUntil: test.errorUntil,
}
invoker := mockUnaryInvokerFactory(opts)
interceptor := MakeRetryUnaryClientInterceptor(test.retries)
ctx := context.Background()
if test.ctxTimeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, test.ctxTimeout)
defer cancel()
}
start := time.Now()
err := interceptor(ctx, "", nil, nil, nil, invoker)
end := time.Now()
actualTime := end.Sub(start)
if test.hasErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, test.expectedAttempts, opts.attempts)
assert.True(t, test.expectedMinTime <= actualTime)
assert.True(t, test.expectedMaxTime >= actualTime)
// stream
opts = &mockClientOptions{
code: code,
errorUntil: test.errorUntil,
}
streamer := mockStreamerFactory(opts)
streamInterceptor := MakeRetryStreamClientInterceptor(test.retries)
if test.ctxTimeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), test.ctxTimeout)
defer cancel()
}
start = time.Now()
_, err = streamInterceptor(ctx, nil, nil, "", streamer)
end = time.Now()
actualTime = end.Sub(start)
if test.hasErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, test.expectedAttempts, opts.attempts)
assert.True(t, test.expectedMinTime <= actualTime)
assert.True(t, test.expectedMaxTime >= actualTime)
}
})
}
}