-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathretry_test.go
193 lines (182 loc) · 5.76 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package squalor
import (
"github.com/go-mysql/conn"
"github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/assert"
"net"
"testing"
"time"
)
func TestRetryWithParameters(t *testing.T) {
testCases := []struct {
name string
retryConfiguration Retryable
results []error
expectedCalls int
expectedSleptTime time.Duration
expectedError error
}{
{
name: "No Error NoOpRetry",
retryConfiguration: &NoOpConfiguration{},
results: []error{nil},
expectedCalls: 1,
expectedSleptTime: 0,
expectedError: nil,
},
{
name: "An Error NoOpRetry not retried.",
retryConfiguration: &NoOpConfiguration{},
results: []error{conn.ErrConnLost, nil},
expectedCalls: 1,
expectedSleptTime: 0,
expectedError: conn.ErrConnLost,
},
{
name: "No Error",
retryConfiguration: &BasicRetryConfiguration,
results: []error{nil},
expectedCalls: 1,
expectedSleptTime: 0,
expectedError: nil,
},
{
name: "Retriable Error and Success after first retry",
retryConfiguration: &BasicRetryConfiguration,
results: []error{conn.ErrConnLost, nil},
expectedCalls: 2,
expectedSleptTime: time.Duration(BasicRetryConfiguration.SleepBetweenRetriesInMillis),
expectedError: nil,
},
{
name: "Retriable Error and Success after second retry",
retryConfiguration: &BasicRetryConfiguration,
results: []error{conn.ErrConnLost, conn.ErrReadOnly, nil},
expectedCalls: 3,
expectedSleptTime: 2 * time.Duration(BasicRetryConfiguration.SleepBetweenRetriesInMillis),
expectedError: nil,
},
{
name: "Retriable Error and exhausted retries",
retryConfiguration: &BasicRetryConfiguration,
results: []error{conn.ErrConnLost, conn.ErrReadOnly, conn.ErrConnLost},
expectedCalls: 3,
expectedSleptTime: 2 * time.Duration(BasicRetryConfiguration.SleepBetweenRetriesInMillis),
expectedError: retriesError(3, conn.ErrConnLost),
},
{
name: "Retriable Error and Non Retriable",
retryConfiguration: &BasicRetryConfiguration,
results: []error{conn.ErrConnLost, conn.ErrDupeKey},
expectedCalls: 2,
expectedSleptTime: time.Duration(BasicRetryConfiguration.SleepBetweenRetriesInMillis),
expectedError: conn.ErrDupeKey,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
sleptTime := time.Duration(0)
sleeper = func(d time.Duration) {
sleptTime = sleptTime + d
}
calls := 0
fn := func() error {
value := tc.results[calls]
calls = calls + 1
return value
}
err := tc.retryConfiguration.Retry(fn)
assert.Equal(t, tc.expectedCalls, calls)
assert.Equal(t, tc.expectedSleptTime, sleptTime)
assert.Equal(t, tc.expectedError, err)
})
}
}
type mockTimeoutErr struct{}
func (e *mockTimeoutErr) Error() string { return "i/o timeout" }
func (e *mockTimeoutErr) Timeout() bool { return true }
type mockTemporaryErr struct{}
func (e *mockTemporaryErr) Error() string { return "i/o temporary" }
func (e *mockTemporaryErr) Temporary() bool { return true }
func TestIsIsRetriable(t *testing.T) {
testCases := []struct {
name string
error error
retryableErrors map[error]bool
retriable bool
}{
{
name: "ErrReadOnly",
error: conn.ErrReadOnly,
retryableErrors: BasicRetryConfiguration.RetryableExceptions,
retriable: true,
},
{
name: "ErrConnLost",
error: conn.ErrConnLost,
retryableErrors: BasicRetryConfiguration.RetryableExceptions,
retriable: true,
},
{
name: "ErrConnCannotConnect",
error: conn.ErrConnCannotConnect,
retryableErrors: BasicRetryConfiguration.RetryableExceptions,
retriable: true,
},
{
name: "ErrDupeKey",
error: conn.ErrDupeKey,
retryableErrors: BasicRetryConfiguration.RetryableExceptions,
retriable: false,
},
{
name: "ErrTimeout",
error: conn.ErrTimeout,
retryableErrors: BasicRetryConfiguration.RetryableExceptions,
retriable: true,
},
{
name: "net.OpError",
error: &net.OpError{},
retryableErrors: BasicRetryConfiguration.RetryableExceptions,
retriable: false,
},
{
name: "net.OpError Timeout",
error: &net.OpError{Err: &mockTimeoutErr{}},
retryableErrors: BasicRetryConfiguration.RetryableExceptions,
retriable: true,
},
{
name: "net.OpError Temporary",
error: &net.OpError{Err: &mockTemporaryErr{}},
retryableErrors: BasicRetryConfiguration.RetryableExceptions,
retriable: true,
},
{
name: "mysql.ErrInvalidConn",
error: mysql.ErrInvalidConn,
retryableErrors: BasicRetryConfiguration.RetryableExceptions,
retriable: true,
},
{
name: "mysql.MySQLError with code other than ER_OPTION_PREVENTS_STATEMENT(1290)",
error: &mysql.MySQLError{Number: 1200},
retryableErrors: BasicRetryConfiguration.RetryableExceptions,
retriable: false,
},
{
// https://dev.mysql.com/doc/refman/5.5/en/server-error-reference.html#error_er_option_prevents_statement
name: "mysql.MySQLError with code ER_OPTION_PREVENTS_STATEMENT(1290)",
error: &mysql.MySQLError{Number: 1290},
retryableErrors: BasicRetryConfiguration.RetryableExceptions,
retriable: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := isRetryable(tc.error, tc.retryableErrors)
assert.Equal(t, tc.retriable, result)
})
}
}