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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
package retry
import (
"fmt"
"net"
"net/url"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
)
type mockTemporaryError struct{ b bool }
func (m mockTemporaryError) Temporary() bool { return m.b }
func (m mockTemporaryError) Error() string {
return fmt.Sprintf("mock temporary %t", m.b)
}
type mockTimeoutError struct{ b bool }
func (m mockTimeoutError) Timeout() bool { return m.b }
func (m mockTimeoutError) Error() string {
return fmt.Sprintf("mock timeout %t", m.b)
}
type mockRetryableError struct{ b bool }
func (m mockRetryableError) RetryableError() bool { return m.b }
func (m mockRetryableError) Error() string {
return fmt.Sprintf("mock retryable %t", m.b)
}
type mockCanceledError struct{ b bool }
func (m mockCanceledError) CanceledError() bool { return m.b }
func (m mockCanceledError) Error() string {
return fmt.Sprintf("mock canceled %t", m.b)
}
type mockStatusCodeError struct{ code int }
func (m mockStatusCodeError) HTTPStatusCode() int { return m.code }
func (m mockStatusCodeError) Error() string {
return fmt.Sprintf("status code error, %v", m.code)
}
type mockConnectionError struct{ err error }
func (m *mockConnectionError) ConnectionError() bool {
return true
}
func (m *mockConnectionError) Error() string {
return fmt.Sprintf("request error: %v", m.err)
}
func (m *mockConnectionError) Unwrap() error {
return m.err
}
type mockErrorCodeError struct {
code string
err error
}
func (m *mockErrorCodeError) ErrorCode() string { return m.code }
func (m *mockErrorCodeError) Error() string {
return fmt.Sprintf("%v: mock error", m.code)
}
func (m *mockErrorCodeError) Unwrap() error {
return m.err
}
func TestRetryConnectionErrors(t *testing.T) {
cases := map[string]struct {
Err error
Retryable aws.Ternary
}{
"nested connection reset": {
Retryable: aws.TrueTernary,
Err: fmt.Errorf("serialization error, %w",
fmt.Errorf("connection reset")),
},
"top level connection reset": {
Retryable: aws.TrueTernary,
Err: fmt.Errorf("connection reset"),
},
"wrapped connection reset": {
Retryable: aws.TrueTernary,
Err: fmt.Errorf("some error: %w", fmt.Errorf("connection reset")),
},
"url.Error connection refused": {
Retryable: aws.TrueTernary,
Err: fmt.Errorf("some error, %w", &url.Error{
Err: fmt.Errorf("connection refused"),
}),
},
"other connection refused": {
Retryable: aws.UnknownTernary,
Err: fmt.Errorf("connection refused"),
},
"nil error connection reset": {
Retryable: aws.UnknownTernary,
},
"some other error": {
Retryable: aws.UnknownTernary,
Err: fmt.Errorf("some error: %w", fmt.Errorf("something bad")),
},
"request send error": {
Retryable: aws.TrueTernary,
Err: fmt.Errorf("some error: %w", &mockConnectionError{err: &url.Error{
Err: fmt.Errorf("another error"),
}}),
},
"temporary error": {
Retryable: aws.TrueTernary,
Err: &mockErrorCodeError{code: "SomeCode", err: mockTemporaryError{b: true}},
},
"timeout error": {
Retryable: aws.TrueTernary,
Err: fmt.Errorf("some error: %w", mockTimeoutError{b: true}),
},
"timeout false error": {
Retryable: aws.UnknownTernary,
Err: fmt.Errorf("some error: %w", mockTimeoutError{b: false}),
},
"net.OpError dial": {
Retryable: aws.TrueTernary,
Err: &net.OpError{
Op: "dial",
Err: mockTimeoutError{b: false},
},
},
"net.OpError nested": {
Retryable: aws.TrueTernary,
Err: &net.OpError{
Op: "read",
Err: fmt.Errorf("some error %w", mockTimeoutError{b: true}),
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
var r RetryableConnectionError
retryable := r.IsErrorRetryable(c.Err)
if e, a := c.Retryable, retryable; e != a {
t.Errorf("expect %v retryable, got %v", e, a)
}
})
}
}
func TestRetryHTTPStatusCodes(t *testing.T) {
cases := map[string]struct {
Err error
Expect aws.Ternary
}{
"top level": {
Err: &mockStatusCodeError{code: 500},
Expect: aws.TrueTernary,
},
"nested": {
Err: fmt.Errorf("some error, %w", &mockStatusCodeError{code: 500}),
Expect: aws.TrueTernary,
},
"response error": {
Err: fmt.Errorf("some error, %w", &mockErrorCodeError{
code: "SomeCode",
err: &mockStatusCodeError{code: 502},
}),
Expect: aws.TrueTernary,
},
}
r := RetryableHTTPStatusCode{Codes: map[int]struct{}{
500: {},
502: {},
}}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
if e, a := c.Expect, r.IsErrorRetryable(c.Err); e != a {
t.Errorf("expect %v, got %v", e, a)
}
})
}
}
func TestRetryErrorCodes(t *testing.T) {
cases := map[string]struct {
Err error
Expect aws.Ternary
}{
"retryable code": {
Err: &MaxAttemptsError{
Err: &mockErrorCodeError{code: "ErrorCode1"},
},
Expect: aws.TrueTernary,
},
"not retryable code": {
Err: &MaxAttemptsError{
Err: &mockErrorCodeError{code: "SomeErroCode"},
},
Expect: aws.UnknownTernary,
},
"other error": {
Err: fmt.Errorf("some other error"),
Expect: aws.UnknownTernary,
},
}
r := RetryableErrorCode{Codes: map[string]struct{}{
"ErrorCode1": {},
"ErrorCode2": {},
}}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
if e, a := c.Expect, r.IsErrorRetryable(c.Err); e != a {
t.Errorf("expect %v, got %v", e, a)
}
})
}
}
func TestCanceledError(t *testing.T) {
cases := map[string]struct {
Err error
Expect aws.Ternary
}{
"canceled error": {
Err: fmt.Errorf("some error, %w", &aws.RequestCanceledError{
Err: fmt.Errorf(":("),
}),
Expect: aws.FalseTernary,
},
"canceled retryable error": {
Err: fmt.Errorf("some error, %w", &aws.RequestCanceledError{
Err: mockRetryableError{b: true},
}),
Expect: aws.FalseTernary,
},
"not canceled error": {
Err: fmt.Errorf("some error, %w", mockCanceledError{b: false}),
Expect: aws.UnknownTernary,
},
"retryable error": {
Err: fmt.Errorf("some error, %w", mockRetryableError{b: true}),
Expect: aws.TrueTernary,
},
}
r := IsErrorRetryables{
NoRetryCanceledError{},
RetryableError{},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
if e, a := c.Expect, r.IsErrorRetryable(c.Err); e != a {
t.Errorf("Expect %v retryable, got %v", e, a)
}
})
}
}
func TestDNSError(t *testing.T) {
cases := map[string]struct {
Err error
Expect aws.Ternary
}{
"IsNotFound": {
Err: &net.DNSError{
IsNotFound: true,
},
Expect: aws.FalseTernary,
},
"Temporary (IsTimeout)": {
Err: &net.DNSError{
IsTimeout: true,
},
Expect: aws.TrueTernary,
},
"Temporary (IsTemporary)": {
Err: &net.DNSError{
IsTemporary: true,
},
Expect: aws.TrueTernary,
},
"Temporary() == false but it falls through": {
Err: &net.OpError{
Op: "dial",
Err: &net.DNSError{},
},
Expect: aws.TrueTernary,
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
var r RetryableConnectionError
retryable := r.IsErrorRetryable(c.Err)
if e, a := c.Expect, retryable; e != a {
t.Errorf("expect %v retryable, got %v", e, a)
}
})
}
}
|