(t *testing.T)
| 14 | ) |
| 15 | |
| 16 | func TestRetry(t *testing.T) { |
| 17 | var try atomic.Int32 |
| 18 | |
| 19 | for _, tc := range []struct { |
| 20 | name string |
| 21 | handler RoundTripper |
| 22 | maxRetries int |
| 23 | expectedTries int32 |
| 24 | expectedRes *http.Response |
| 25 | expectedErr error |
| 26 | }{ |
| 27 | { |
| 28 | name: "retry errors until success", |
| 29 | handler: RoundTripperFunc(func(_ Request) (*http.Response, error) { |
| 30 | if try.Inc() == 5 { |
| 31 | return &http.Response{StatusCode: 200}, nil |
| 32 | } |
| 33 | return nil, errors.New("this request failed") |
| 34 | }), |
| 35 | maxRetries: 5, |
| 36 | expectedTries: 5, |
| 37 | expectedRes: &http.Response{StatusCode: 200}, |
| 38 | expectedErr: nil, |
| 39 | }, |
| 40 | { |
| 41 | name: "don't retry 400's", |
| 42 | handler: RoundTripperFunc(func(_ Request) (*http.Response, error) { |
| 43 | try.Inc() |
| 44 | return &http.Response{StatusCode: 400}, nil |
| 45 | }), |
| 46 | maxRetries: 5, |
| 47 | expectedTries: 1, |
| 48 | expectedRes: &http.Response{StatusCode: 400}, |
| 49 | expectedErr: nil, |
| 50 | }, |
| 51 | { |
| 52 | name: "don't retry GRPC request with HTTP 400's", |
| 53 | handler: RoundTripperFunc(func(_ Request) (*http.Response, error) { |
| 54 | try.Inc() |
| 55 | return nil, httpgrpc.ErrorFromHTTPResponse(&httpgrpc.HTTPResponse{Code: 400}) |
| 56 | }), |
| 57 | maxRetries: 5, |
| 58 | expectedTries: 1, |
| 59 | expectedRes: nil, |
| 60 | expectedErr: httpgrpc.ErrorFromHTTPResponse(&httpgrpc.HTTPResponse{Code: 400}), |
| 61 | }, |
| 62 | { |
| 63 | name: "retry 500s", |
| 64 | handler: RoundTripperFunc(func(_ Request) (*http.Response, error) { |
| 65 | try.Inc() |
| 66 | return &http.Response{StatusCode: 503}, nil |
| 67 | }), |
| 68 | maxRetries: 5, |
| 69 | expectedTries: 5, |
| 70 | expectedRes: &http.Response{StatusCode: 503}, |
| 71 | expectedErr: nil, |
| 72 | }, |
| 73 | { |
nothing calls this directly
no test coverage detected