-
Notifications
You must be signed in to change notification settings - Fork 356
/
retry_test.go
198 lines (179 loc) · 5.43 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
194
195
196
197
198
package req
import (
"bytes"
"io"
"math"
"net/http"
"testing"
"time"
"github.com/imroc/req/v3/internal/tests"
)
func TestRetryBackOff(t *testing.T) {
testRetry(t, func(r *Request) {
r.SetRetryBackoffInterval(10*time.Millisecond, 1*time.Second)
})
}
func testRetry(t *testing.T, setFunc func(r *Request)) {
attempt := 0
r := tc().R().
SetRetryCount(3).
SetRetryCondition(func(resp *Response, err error) bool {
return (err != nil) || (resp.StatusCode == http.StatusTooManyRequests)
}).
SetRetryHook(func(resp *Response, err error) {
attempt++
})
setFunc(r)
resp, err := r.Get("/too-many")
tests.AssertNoError(t, err)
tests.AssertEqual(t, 3, resp.Request.RetryAttempt)
tests.AssertEqual(t, 3, attempt)
}
func TestRetryInterval(t *testing.T) {
testRetry(t, func(r *Request) {
r.SetRetryInterval(func(resp *Response, attempt int) time.Duration {
sleep := 0.01 * math.Exp2(float64(attempt))
return time.Duration(math.Min(2, sleep)) * time.Second
})
})
}
func TestRetryFixedInterval(t *testing.T) {
testRetry(t, func(r *Request) {
r.SetRetryFixedInterval(1 * time.Millisecond)
})
}
func TestAddRetryHook(t *testing.T) {
test := "test1"
testRetry(t, func(r *Request) {
r.AddRetryHook(func(resp *Response, err error) {
test = "test2"
})
})
tests.AssertEqual(t, "test2", test)
}
func TestRetryOverride(t *testing.T) {
c := tc().
SetCommonRetryCount(3).
SetCommonRetryHook(func(resp *Response, err error) {}).
AddCommonRetryHook(func(resp *Response, err error) {}).
SetCommonRetryCondition(func(resp *Response, err error) bool {
return false
}).SetCommonRetryBackoffInterval(1*time.Millisecond, 10*time.Millisecond)
test := "test"
resp, err := c.R().SetRetryFixedInterval(2 * time.Millisecond).
SetRetryCount(2).
SetRetryHook(func(resp *Response, err error) {
test = "test1"
}).SetRetryCondition(func(resp *Response, err error) bool {
return err != nil || resp.StatusCode == http.StatusTooManyRequests
}).Get("/too-many")
tests.AssertNoError(t, err)
tests.AssertEqual(t, "test1", test)
tests.AssertEqual(t, 2, resp.Request.RetryAttempt)
}
func TestAddRetryCondition(t *testing.T) {
attempt := 0
resp, err := tc().R().
SetRetryCount(3).
AddRetryCondition(func(resp *Response, err error) bool {
return err != nil
}).
AddRetryCondition(func(resp *Response, err error) bool {
return resp.StatusCode == http.StatusServiceUnavailable
}).
SetRetryHook(func(resp *Response, err error) {
attempt++
}).Get("/too-many")
tests.AssertNoError(t, err)
tests.AssertEqual(t, 0, attempt)
tests.AssertEqual(t, 0, resp.Request.RetryAttempt)
attempt = 0
resp, err = tc().
SetCommonRetryCount(3).
AddCommonRetryCondition(func(resp *Response, err error) bool {
return err != nil
}).
AddCommonRetryCondition(func(resp *Response, err error) bool {
return resp.StatusCode == http.StatusServiceUnavailable
}).
SetCommonRetryHook(func(resp *Response, err error) {
attempt++
}).R().Get("/too-many")
tests.AssertNoError(t, err)
tests.AssertEqual(t, 0, attempt)
tests.AssertEqual(t, 0, resp.Request.RetryAttempt)
}
func TestRetryWithUnreplayableBody(t *testing.T) {
_, err := tc().R().
SetRetryCount(1).
SetBody(bytes.NewBufferString("test")).
Post("/")
tests.AssertEqual(t, errRetryableWithUnReplayableBody, err)
_, err = tc().R().
SetRetryCount(1).
SetBody(io.NopCloser(bytes.NewBufferString("test"))).
Post("/")
tests.AssertEqual(t, errRetryableWithUnReplayableBody, err)
}
func TestRetryWithSetResult(t *testing.T) {
headers := make(http.Header)
resp, err := tc().SetCommonCookies(&http.Cookie{
Name: "test",
Value: "test",
}).R().
SetRetryCount(1).
SetResult(&headers).
Get("/header")
assertSuccess(t, resp, err)
tests.AssertEqual(t, "test=test", headers.Get("Cookie"))
}
func TestRetryWithModify(t *testing.T) {
tokens := []string{"badtoken1", "badtoken2", "goodtoken"}
tokenIndex := 0
c := tc().
SetCommonRetryCount(2).
SetCommonRetryHook(func(resp *Response, err error) {
tokenIndex++
resp.Request.SetBearerAuthToken(tokens[tokenIndex])
}).SetCommonRetryCondition(func(resp *Response, err error) bool {
return err != nil || resp.StatusCode == http.StatusUnauthorized
})
resp, err := c.R().
SetBearerAuthToken(tokens[tokenIndex]).
Get("/protected")
assertSuccess(t, resp, err)
tests.AssertEqual(t, 2, resp.Request.RetryAttempt)
}
func TestRetryFalse(t *testing.T) {
resp, err := tc().SetTimeout(2 * time.Second).R().
SetRetryCount(1).
SetRetryCondition(func(resp *Response, err error) bool {
return false
}).Get("https://non-exists-host.com.cn")
tests.AssertNotNil(t, err)
tests.AssertIsNil(t, resp.Response)
tests.AssertEqual(t, 0, resp.Request.RetryAttempt)
}
func TestRetryTurnedOffWhenRetryCountEqZero(t *testing.T) {
resp, err := tc().SetTimeout(2 * time.Second).R().
SetRetryCount(0).
SetRetryCondition(func(resp *Response, err error) bool {
t.Fatal("retry condition should not be executed")
return true
}).
Get("https://non-exists-host.com.cn")
tests.AssertNotNil(t, err)
tests.AssertIsNil(t, resp.Response)
tests.AssertEqual(t, 0, resp.Request.RetryAttempt)
resp, err = tc().SetTimeout(2 * time.Second).
SetCommonRetryCount(0).
SetCommonRetryCondition(func(resp *Response, err error) bool {
t.Fatal("retry condition should not be executed")
return true
}).
R().
Get("https://non-exists-host.com.cn")
tests.AssertNotNil(t, err)
tests.AssertIsNil(t, resp.Response)
tests.AssertEqual(t, 0, resp.Request.RetryAttempt)
}