-
Notifications
You must be signed in to change notification settings - Fork 7
/
retry_test.go
91 lines (83 loc) · 1.83 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
package httpretry
import (
"crypto/x509"
"github.com/stretchr/testify/assert"
"net/http"
"net/url"
"testing"
)
type MyTemporaryError struct {
IsTemp bool
}
func (e *MyTemporaryError) Error() string {
return "my error"
}
func (e *MyTemporaryError) Temporary() bool {
return e.IsTemp
}
func TestDefaultRetryPolicy(t *testing.T) {
check := assert.New(t)
tests := []struct {
Description string
StatusCodeIn int
ErrorIn error
Expect bool
}{
{
Description: "Should not retry on OK requests",
StatusCodeIn: http.StatusOK,
ErrorIn: nil,
Expect: false,
},
{
Description: "Should not retry on BadRequest",
StatusCodeIn: http.StatusBadRequest,
ErrorIn: nil,
Expect: false,
},
{
Description: "Should retry on InternalError",
StatusCodeIn: http.StatusInternalServerError,
ErrorIn: nil,
Expect: true,
},
{
Description: "Should retry on Temporary error",
StatusCodeIn: http.StatusInternalServerError,
ErrorIn: &MyTemporaryError{IsTemp: true},
Expect: true,
},
{
Description: "Should not retry on URL parse error",
StatusCodeIn: 0,
ErrorIn: &url.Error{
Op: "parse",
URL: "",
},
Expect: false,
},
{
Description: "Should not retry on Certificate error",
StatusCodeIn: 0,
ErrorIn: &url.Error{
Op: "Get",
URL: "https://some-non-existing-url.com",
Err: x509.UnknownAuthorityError{},
},
Expect: false,
},
{
// should not happen
Description: "Should retry if there was no error but also no response",
StatusCodeIn: 0,
ErrorIn: nil,
Expect: true,
},
}
for _, test := range tests {
t.Run(test.Description, func(t *testing.T) {
shouldRetry := defaultRetryPolicy(test.StatusCodeIn, test.ErrorIn)
check.Equal(test.Expect, shouldRetry)
})
}
}