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
|
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"context"
"errors"
"net/http"
"sync/atomic"
"testing"
"time"
)
type testRetrier struct {
Retrier
N int64
Err error
}
func (r *testRetrier) Retry(ctx context.Context, retry int, req *http.Request, resp *http.Response, err error) (time.Duration, bool, error) {
atomic.AddInt64(&r.N, 1)
if r.Err != nil {
return 0, false, r.Err
}
return r.Retrier.Retry(ctx, retry, req, resp, err)
}
func TestStopRetrier(t *testing.T) {
r := NewStopRetrier()
wait, ok, err := r.Retry(context.TODO(), 1, nil, nil, nil)
if want, got := 0*time.Second, wait; want != got {
t.Fatalf("expected %v, got %v", want, got)
}
if want, got := false, ok; want != got {
t.Fatalf("expected %v, got %v", want, got)
}
if err != nil {
t.Fatalf("expected nil, got %v", err)
}
}
func TestRetrier(t *testing.T) {
var numFailedReqs int
fail := func(r *http.Request) (*http.Response, error) {
numFailedReqs += 1
//return &http.Response{Request: r, StatusCode: 400}, nil
return nil, errors.New("request failed")
}
tr := &failingTransport{path: "/fail", fail: fail}
httpClient := &http.Client{Transport: tr}
retrier := &testRetrier{
Retrier: NewBackoffRetrier(NewSimpleBackoff(100, 100, 100, 100, 100)),
}
client, err := NewClient(
SetHttpClient(httpClient),
SetMaxRetries(5),
SetHealthcheck(false),
SetRetrier(retrier))
if err != nil {
t.Fatal(err)
}
res, err := client.PerformRequest(context.TODO(), PerformRequestOptions{
Method: "GET",
Path: "/fail",
})
if err == nil {
t.Fatal("expected error")
}
if res != nil {
t.Fatal("expected no response")
}
// Connection should be marked as dead after it failed
if numFailedReqs != 5 {
t.Errorf("expected %d failed requests; got: %d", 5, numFailedReqs)
}
if retrier.N != 5 {
t.Errorf("expected %d Retrier calls; got: %d", 5, retrier.N)
}
}
func TestRetrierWithError(t *testing.T) {
var numFailedReqs int
fail := func(r *http.Request) (*http.Response, error) {
numFailedReqs += 1
//return &http.Response{Request: r, StatusCode: 400}, nil
return nil, errors.New("request failed")
}
tr := &failingTransport{path: "/fail", fail: fail}
httpClient := &http.Client{Transport: tr}
kaboom := errors.New("kaboom")
retrier := &testRetrier{
Err: kaboom,
Retrier: NewBackoffRetrier(NewSimpleBackoff(100, 100, 100, 100, 100)),
}
client, err := NewClient(
SetHttpClient(httpClient),
SetMaxRetries(5),
SetHealthcheck(false),
SetRetrier(retrier))
if err != nil {
t.Fatal(err)
}
res, err := client.PerformRequest(context.TODO(), PerformRequestOptions{
Method: "GET",
Path: "/fail",
})
if err != kaboom {
t.Fatalf("expected %v, got %v", kaboom, err)
}
if res != nil {
t.Fatal("expected no response")
}
if numFailedReqs != 1 {
t.Errorf("expected %d failed requests; got: %d", 1, numFailedReqs)
}
if retrier.N != 1 {
t.Errorf("expected %d Retrier calls; got: %d", 1, retrier.N)
}
}
func TestRetrierOnPerformRequest(t *testing.T) {
var numFailedReqs int
fail := func(r *http.Request) (*http.Response, error) {
numFailedReqs += 1
//return &http.Response{Request: r, StatusCode: 400}, nil
return nil, errors.New("request failed")
}
tr := &failingTransport{path: "/fail", fail: fail}
httpClient := &http.Client{Transport: tr}
defaultRetrier := &testRetrier{
Retrier: NewStopRetrier(),
}
requestRetrier := &testRetrier{
Retrier: NewStopRetrier(),
}
client, err := NewClient(
SetHttpClient(httpClient),
SetHealthcheck(false),
SetRetrier(defaultRetrier))
if err != nil {
t.Fatal(err)
}
res, err := client.PerformRequest(context.TODO(), PerformRequestOptions{
Method: "GET",
Path: "/fail",
Retrier: requestRetrier,
})
if err == nil {
t.Fatal("expected error")
}
if res != nil {
t.Fatal("expected no response")
}
if want, have := int64(0), defaultRetrier.N; want != have {
t.Errorf("defaultRetrier: expected %d calls; got: %d", want, have)
}
if want, have := int64(1), requestRetrier.N; want != have {
t.Errorf("requestRetrier: expected %d calls; got: %d", want, have)
}
}
|