-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuit_breaker_test.go
147 lines (123 loc) · 2.86 KB
/
circuit_breaker_test.go
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
package gcb
import (
"bufio"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/calvernaz/gcb/testutil"
)
func TestCircuit_FailedAllAttempts(t *testing.T) {
client, _, _, teardown := newRoundTripper(WithMaxRetries(2))
defer teardown()
var i int
for i < 2 {
request, _ := http.NewRequest(http.MethodPost, "http://localhost", strings.NewReader("Hello Server!"))
resp, err := client.Do(request)
if err == nil {
t.Fatal(err)
}
if resp != nil {
if _, err = io.Copy(ioutil.Discard, resp.Body); err != nil {
t.Error(err)
}
}
i++
}
}
func TestCircuit_DefaultRetryAttempts(t *testing.T) {
// table tests
tt := []struct {
shouldRetry int
statusCode int
}{
{4, 200},
{7, 500},
}
client, baseURL, mux, teardown := newRoundTripper()
defer teardown()
// setup mock handler
var maxRetries int
var reqNum int
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
reqNum++
if reqNum <= maxRetries {
w.WriteHeader(500)
return
}
w.WriteHeader(200)
w.Write([]byte("Hello Client!"))
}))
// tests
for _, ts := range tt {
maxRetries = ts.shouldRetry
request, _ := http.NewRequest(http.MethodPost, baseURL, strings.NewReader("Hi Server!"))
resp, err := client.Do(request)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != ts.statusCode {
t.Errorf("Expected %d, got %d", ts.statusCode, resp.StatusCode)
}
// reset request counter
reqNum = 0
if _, err = io.Copy(bufio.NewWriter(os.Stdout), resp.Body); err != nil {
t.Error(err)
}
}
}
func TestCircuit_WithConfiguredRetryAttempts(t *testing.T) {
// table tests
tt := []struct {
shouldRetry int
statusCode int
}{
{3, 500},
{4, 500},
}
client, baseURL, mux, teardown := newRoundTripper(WithMaxRetries(2))
defer teardown()
// setup mock handler
var maxRetries int
var reqNum int
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
reqNum++
if reqNum <= maxRetries {
w.WriteHeader(500)
return
}
w.WriteHeader(200)
}))
// tests
for _, ts := range tt {
maxRetries = ts.shouldRetry
request, _ := http.NewRequest("GET", baseURL, nil)
resp, err := client.Do(request)
if err != nil {
t.Error(err)
}
if resp.StatusCode != ts.statusCode {
t.Errorf("Expected %d, got %d", ts.statusCode, resp.StatusCode)
}
// reset request counter
reqNum = 0
if _, err = io.Copy(ioutil.Discard, resp.Body); err != nil {
t.Error(err)
}
}
}
func newRoundTripper(opts ...Option) (http.Client, string, *http.ServeMux, func()) {
// setup http client with our round tripper
// the default number of shouldRetry is 4.
transport := NewRoundTripper(opts...)
client := http.Client{
Transport: transport,
Timeout: 30 * time.Second,
}
// setup mock server
baseURL, mux, teardown := testutil.ServerMock()
return client, baseURL, mux, teardown
}