Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(564)

Side by Side Diff: common/lhttp/client_test.go

Issue 2568953004: common/lhttp: only retry transient errors (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« common/lhttp/client.go ('K') | « common/lhttp/client.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package lhttp 5 package lhttp
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "encoding/json" 9 "encoding/json"
10 "fmt" 10 "fmt"
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 serverCalls++ 243 serverCalls++
244 })) 244 }))
245 defer ts.Close() 245 defer ts.Close()
246 246
247 status, err := PostJSON(ctx, fast, http.DefaultClient, ts.URL, map[strin g]string{"key": "value"}, nil, nil) 247 status, err := PostJSON(ctx, fast, http.DefaultClient, ts.URL, map[strin g]string{"key": "value"}, nil, nil)
248 ut.AssertEqual(t, nil, err) 248 ut.AssertEqual(t, nil, err)
249 ut.AssertEqual(t, 200, status) 249 ut.AssertEqual(t, 200, status)
250 ut.AssertEqual(t, 1, serverCalls) 250 ut.AssertEqual(t, 1, serverCalls)
251 } 251 }
252 252
253 func TestNewRequestDefaultFactory(t *testing.T) {
254 // Test that the default factory (rFn == nil) only retries for transient
255 // HTTP errors.
256 testCases := []struct {
257 statusCode int // The status code to return (the first 2 time s).
258 path string // Request path, if any.
259 wantErr bool // Whether we want NewRequest to return an err or.
260 wantCalls int // The total number of HTTP requests expected.
261 }{
262 // 200, passes immediately.
263 {statusCode: 200, wantErr: false, wantCalls: 1},
264 // Transient HTTP error codes that will retry.
265 {statusCode: 408, wantErr: false, wantCalls: 3},
266 {statusCode: 500, wantErr: false, wantCalls: 3},
267 {statusCode: 503, wantErr: false, wantCalls: 3},
268 // Immediate failure codes.
269 {statusCode: 403, wantErr: true, wantCalls: 1},
270 {statusCode: 404, wantErr: true, wantCalls: 1},
271 // Special 404 case which will retry.
272 {statusCode: 404, path: "/_ah/api/foo/bar", wantErr: false, want Calls: 3},
273 }
274
275 ctx := context.Background()
276
277 for _, tc := range testCases {
278 tc := tc
279 t.Run(fmt.Sprintf("Status code %d, path %q", tc.statusCode, tc.p ath), func(t *testing.T) {
280 t.Parallel()
281 serverCalls := 0
282 ts := httptest.NewServer(
283 http.HandlerFunc(func(w http.ResponseWriter, r * http.Request) {
284 defer r.Body.Close()
285 serverCalls++
286 if serverCalls <= 2 {
287 w.WriteHeader(tc.statusCode)
288 }
289 fmt.Fprintf(w, "Hello World!\n")
290 }))
291 defer ts.Close()
292
293 httpReq := httpReqGen("GET", ts.URL+tc.path, nil)
294 req := NewRequest(ctx, http.DefaultClient, nil, httpReq, func(resp *http.Response) error {
295 return resp.Body.Close()
296 })
297
298 _, err := req()
299 if err == nil && tc.wantErr {
300 t.Error("req returned nil error, wanted an error ")
301 } else if err != nil && !tc.wantErr {
302 t.Errorf("req returned err %v, wanted nil", err)
303 }
304 if got, want := serverCalls, tc.wantCalls; got != want {
305 t.Errorf("total server calls; got %d, want %d", got, want)
306 }
307 })
308 }
309 }
310
253 func TestNewRequestClosesBody(t *testing.T) { 311 func TestNewRequestClosesBody(t *testing.T) {
254 ctx := context.Background() 312 ctx := context.Background()
255 serverCalls := 0 313 serverCalls := 0
256 314
257 // Return a 500 for the first 2 requests. 315 // Return a 500 for the first 2 requests.
258 ts := httptest.NewServer( 316 ts := httptest.NewServer(
259 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 317 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
260 defer r.Body.Close() 318 defer r.Body.Close()
261 serverCalls++ 319 serverCalls++
262 if serverCalls <= 2 { 320 if serverCalls <= 2 {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 defer r.Body.Close() 413 defer r.Body.Close()
356 out := handler(r.Body) 414 out := handler(r.Body)
357 if out == nil { 415 if out == nil {
358 w.WriteHeader(500) 416 w.WriteHeader(500)
359 } else { 417 } else {
360 w.Header().Set("Content-Type", jsonContentType) 418 w.Header().Set("Content-Type", jsonContentType)
361 ut.ExpectEqual(t, nil, json.NewEncoder(w).Encode(out)) 419 ut.ExpectEqual(t, nil, json.NewEncoder(w).Encode(out))
362 } 420 }
363 }) 421 })
364 } 422 }
OLDNEW
« common/lhttp/client.go ('K') | « common/lhttp/client.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698