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 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
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
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 }
143
View as plain text