| 1 |
// Copyright 2016, Google Inc. |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are |
| 6 |
// met: |
| 7 |
// |
| 8 |
// * Redistributions of source code must retain the above copyright |
| 9 |
// notice, this list of conditions and the following disclaimer. |
| 10 |
// * Redistributions in binary form must reproduce the above |
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
| 12 |
// in the documentation and/or other materials provided with the |
| 13 |
// distribution. |
| 14 |
// * Neither the name of Google Inc. nor the names of its |
| 15 |
// contributors may be used to endorse or promote products derived from |
| 16 |
// this software without specific prior written permission. |
| 17 |
// |
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
package gax |
| 31 |
|
| 32 |
import ( |
| 33 |
"context" |
| 34 |
"net/http" |
| 35 |
"testing" |
| 36 |
"time" |
| 37 |
|
| 38 |
"google.golang.org/api/googleapi" |
| 39 |
"google.golang.org/grpc/codes" |
| 40 |
"google.golang.org/grpc/status" |
| 41 |
) |
| 42 |
|
| 43 |
var _ Retryer = &boRetryer{} |
| 44 |
|
| 45 |
func TestBackofDefault(t *testing.T) { |
| 46 |
backoff := Backoff{} |
| 47 |
|
| 48 |
max := []time.Duration{1, 2, 4, 8, 16, 30, 30, 30, 30, 30} |
| 49 |
for i, m := range max { |
| 50 |
max[i] = m * time.Second |
| 51 |
} |
| 52 |
|
| 53 |
for i, w := range max { |
| 54 |
if d := backoff.Pause(); d > w { |
| 55 |
t.Errorf("Backoff duration should be at most %s, got %s", w, d) |
| 56 |
} else if i < len(max)-1 && backoff.cur != max[i+1] { |
| 57 |
t.Errorf("current envelope is %s, want %s", backoff.cur, max[i+1]) |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func TestBackoffExponential(t *testing.T) { |
| 63 |
backoff := Backoff{Initial: 1, Max: 20, Multiplier: 2} |
| 64 |
want := []time.Duration{1, 2, 4, 8, 16, 20, 20, 20, 20, 20} |
| 65 |
for _, w := range want { |
| 66 |
if d := backoff.Pause(); d > w { |
| 67 |
t.Errorf("Backoff duration should be at most %s, got %s", w, d) |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func TestOnCodes(t *testing.T) { |
| 73 |
// Lint errors grpc.Errorf in 1.6. It mistakenly expects the first arg to Errorf to be a string. |
| 74 |
errf := status.Errorf |
| 75 |
apiErr := errf(codes.Unavailable, "") |
| 76 |
tests := []struct { |
| 77 |
c []codes.Code |
| 78 |
retry bool |
| 79 |
}{ |
| 80 |
{nil, false}, |
| 81 |
{[]codes.Code{codes.DeadlineExceeded}, false}, |
| 82 |
{[]codes.Code{codes.DeadlineExceeded, codes.Unavailable}, true}, |
| 83 |
{[]codes.Code{codes.Unavailable}, true}, |
| 84 |
} |
| 85 |
for _, tst := range tests { |
| 86 |
b := OnCodes(tst.c, Backoff{}) |
| 87 |
if _, retry := b.Retry(apiErr); retry != tst.retry { |
| 88 |
t.Errorf("retriable codes: %v, error: %s, retry: %t, want %t", tst.c, apiErr, retry, tst.retry) |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestOnErrorFunc(t *testing.T) { |
| 94 |
// Use errors.Is if on go 1.13 or higher. |
| 95 |
is := func(err, target error) bool { |
| 96 |
return err == target |
| 97 |
} |
| 98 |
tests := []struct { |
| 99 |
e error |
| 100 |
shouldRetry func(err error) bool |
| 101 |
retry bool |
| 102 |
}{ |
| 103 |
{context.DeadlineExceeded, func(err error) bool { return false }, false}, |
| 104 |
{context.DeadlineExceeded, func(err error) bool { return is(err, context.DeadlineExceeded) }, true}, |
| 105 |
} |
| 106 |
for _, tst := range tests { |
| 107 |
b := OnErrorFunc(Backoff{}, tst.shouldRetry) |
| 108 |
if _, retry := b.Retry(tst.e); retry != tst.retry { |
| 109 |
t.Errorf("retriable func: error: %s, retry: %t, want %t", tst.e, retry, tst.retry) |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
func TestOnHTTPCodes(t *testing.T) { |
| 115 |
apiErr := &googleapi.Error{Code: http.StatusBadGateway} |
| 116 |
tests := []struct { |
| 117 |
c []int |
| 118 |
retry bool |
| 119 |
}{ |
| 120 |
{nil, false}, |
| 121 |
{[]int{http.StatusConflict}, false}, |
| 122 |
{[]int{http.StatusConflict, http.StatusBadGateway}, true}, |
| 123 |
{[]int{http.StatusBadGateway}, true}, |
| 124 |
} |
| 125 |
for _, tst := range tests { |
| 126 |
b := OnHTTPCodes(Backoff{}, tst.c...) |
| 127 |
if _, retry := b.Retry(apiErr); retry != tst.retry { |
| 128 |
t.Errorf("retriable codes: %v, error: %s, retry: %t, want %t", tst.c, apiErr, retry, tst.retry) |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func TestWithTimeout(t *testing.T) { |
| 134 |
settings := CallSettings{} |
| 135 |
to := 10 * time.Second |
| 136 |
|
| 137 |
WithTimeout(to).Resolve(&settings) |
| 138 |
|
| 139 |
if settings.timeout != to { |
| 140 |
t.Errorf("got %v, want %v", settings.timeout, to) |
| 141 |
} |
| 142 |
} |