forked from bazelbuild/bazelisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttputil_test.go
249 lines (198 loc) · 5.84 KB
/
httputil_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
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
package httputil
import (
"errors"
"net/http"
"strconv"
"testing"
"time"
)
var (
seed = time.Now()
)
type fakeClock struct {
now time.Time
SleepPeriods []time.Duration
}
func newFakeClock() *fakeClock {
return &fakeClock{now: seed}
}
func (fc *fakeClock) Sleep(d time.Duration) {
fc.now = fc.now.Add(d)
fc.SleepPeriods = append(fc.SleepPeriods, d)
}
func (fc *fakeClock) Now() time.Time {
return fc.now
}
func (fc *fakeClock) TimesSlept() int {
return len(fc.SleepPeriods)
}
func setUp() (*FakeTransport, *fakeClock) {
transport := NewFakeTransport()
DefaultTransport = transport
clock := newFakeClock()
RetryClock = clock
return transport, clock
}
func setUpAllFailures(url string, status, retries int, headers map[string]string) (*FakeTransport, *fakeClock) {
MaxRetries = retries
transport, clock := setUp()
for i := 0; i <= retries; i++ {
transport.AddResponse(url, status, "", headers)
}
return transport, clock
}
func TestSuccessOnFirstTry(t *testing.T) {
transport, _ := setUp()
url := "http://foo"
want := "the_body"
transport.AddResponse(url, 200, want, nil)
body, _, err := ReadRemoteFile(url, "")
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
got := string(body)
if got != want {
t.Fatalf("Expected body %q, but got %q", want, got)
}
}
func TestSuccessOnRetry(t *testing.T) {
transport, clock := setUp()
url := "http://foo"
want := "the_body"
transport.AddResponse(url, 503, "", nil)
transport.AddResponse(url, 200, want, nil)
body, _, err := ReadRemoteFile(url, "")
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
got := string(body)
if got != want {
t.Fatalf("Expected body %q, but got %q", want, got)
}
if clock.TimesSlept() != 1 {
t.Fatalf("Expected a single retry, not %d", clock.TimesSlept())
}
}
func TestSuccessOnRetryNonHTTPError(t *testing.T) {
transport, clock := setUp()
url := "http://foo"
want := "the_body"
transport.AddError(url, errors.New("boom!"))
transport.AddResponse(url, 200, want, nil)
body, _, err := ReadRemoteFile(url, "")
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
got := string(body)
if got != want {
t.Fatalf("Expected body %q, but got %q", want, got)
}
if clock.TimesSlept() != 1 {
t.Fatalf("Expected a single retry, not %d", clock.TimesSlept())
}
}
func TestAllTriesFail(t *testing.T) {
MaxRequestDuration = 100 * time.Second
url := "http://bar"
retries := 5
_, clock := setUpAllFailures(url, 502, retries, nil)
_, _, err := ReadRemoteFile(url, "")
if err == nil {
t.Fatal("Expected request to fail with code 502")
}
reason := err.Error()
expected := "could not fetch http://bar: unable to complete request to http://bar after 5 retries. Most recent failure: HTTP 502"
if reason != expected {
t.Fatalf("Expected request to fail with %q, but got %q", expected, reason)
}
if clock.TimesSlept() != retries {
t.Fatalf("Expected %d retries, but got %d", retries, clock.TimesSlept())
}
}
func TestShouldObeyRetryHeaders(t *testing.T) {
MaxRequestDuration = time.Hour * 24
headerGens := []func(time.Duration) string{
func(d time.Duration) string {
// Value == earliest date to retry
return seed.Add(d).UTC().Format(http.TimeFormat)
},
func(d time.Duration) string {
// Value == seconds to wait
return strconv.Itoa(int(d.Seconds()))
},
}
for _, header := range retryHeaders {
for _, gen := range headerGens {
url := "http://bar"
wanted := 5 * time.Hour
_, clock := setUpAllFailures(url, 501, 1, map[string]string{header: gen(wanted)})
_, _, err := ReadRemoteFile(url, "")
if err == nil {
t.Fatal("Expected request to fail with code 502")
}
if clock.TimesSlept() != 1 {
t.Fatalf("Expected a single retry, but got %d", clock.TimesSlept())
}
got := clock.SleepPeriods[0]
delta := time.Minute
if got < wanted-delta || wanted+delta < got {
t.Errorf("Expected a retry after roughly %s, but actually waited for %s", wanted, got)
}
}
}
}
func TestShouldUseExponentialBackoffIfNoRetryHeader(t *testing.T) {
MaxRequestDuration = time.Hour * 24
url := "http://bar"
retries := 5
_, clock := setUpAllFailures(url, 501, retries, nil)
_, _, err := ReadRemoteFile(url, "")
if err == nil {
t.Fatal("Expected request to fail with code 501")
}
if clock.TimesSlept() != retries {
t.Fatalf("Expected %d retries, but got %d", retries, clock.TimesSlept())
}
var total time.Duration
for _, p := range clock.SleepPeriods {
total += p
}
delta := time.Millisecond * 100
// exponential backoff: 1s + d1, 2s + d2, 4s + d3, 8s + d4 with dx being a random value in [0ms, 500ms]
lower := (1<<uint(retries))*time.Second - time.Second
upper := lower + time.Duration(retries*500)*time.Millisecond
if total < lower-delta || upper+delta < total {
t.Fatalf("Expected a total sleep time between %s and %s (%d retries, exponential backoff with fuzzing), but waited %s instead", lower, upper, retries, total)
}
}
func TestDeadlineExceeded(t *testing.T) {
MaxRequestDuration = time.Second * 8
url := "http://bar"
setUpAllFailures(url, 500, 10, nil)
_, _, err := ReadRemoteFile(url, "")
if err == nil {
t.Fatal("Expected request to fail with code 500")
}
wanted := "could not fetch http://bar: unable to complete request to http://bar within 8s"
got := err.Error()
if wanted != got {
t.Fatalf("Expected error %q, but got %q", wanted, got)
}
}
func TestNoRetryOnPermanentError(t *testing.T) {
MaxRequestDuration = time.Hour
url := "http://xyz"
_, clock := setUpAllFailures(url, 404, 3, nil)
_, _, err := ReadRemoteFile(url, "")
if err == nil {
t.Fatal("Expected request to fail with code 404")
}
wanted := "unexpected status code while reading http://xyz: 404"
got := err.Error()
if wanted != got {
t.Fatalf("Expected error %q, but got %q", wanted, got)
}
if clock.TimesSlept() > 0 {
t.Fatalf("Expected no retries for permanent error, but got %d", clock.TimesSlept())
}
}