-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcircuitbreaker_test.go
280 lines (247 loc) · 7.48 KB
/
circuitbreaker_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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package circuitbreaker_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/slok/goresilience"
"github.com/slok/goresilience/circuitbreaker"
"github.com/slok/goresilience/errors"
)
var err = fmt.Errorf("wanted error")
var okf = func(ctx context.Context) error { return nil }
var errf = func(ctx context.Context) error { return err }
func TestCircuitBreaker(t *testing.T) {
tests := []struct {
name string
cfg circuitbreaker.Config
f func(cb goresilience.Runner) goresilience.Func // Receives the circuit to set the sate in the way we want.
expErr error
}{
{
name: "The circuit should start in closed state.",
cfg: circuitbreaker.Config{},
f: func(cb goresilience.Runner) goresilience.Func {
return okf
},
expErr: nil,
},
{
name: "After some errors the circuit should be open.",
cfg: circuitbreaker.Config{
ErrorPercentThresholdToOpen: 30,
MinimumRequestToOpen: 10,
},
f: func(cb goresilience.Runner) goresilience.Func {
// Close the circuit first.
for i := 0; i < 10; i++ {
cb.Run(context.TODO(), errf)
}
return okf
},
expErr: errors.ErrCircuitOpen,
},
{
name: "After some errors the circuit should be open, but only if the the maximum of request have been made.",
cfg: circuitbreaker.Config{
ErrorPercentThresholdToOpen: 30,
MinimumRequestToOpen: 11,
},
f: func(cb goresilience.Runner) goresilience.Func {
for i := 0; i < 10; i++ {
cb.Run(context.TODO(), errf)
}
return okf
},
expErr: nil,
},
{
name: "A circuit in half open state should admit new requests.",
cfg: circuitbreaker.Config{
ErrorPercentThresholdToOpen: 30,
MinimumRequestToOpen: 10,
WaitDurationInOpenState: 5 * time.Millisecond,
},
f: func(cb goresilience.Runner) goresilience.Func {
// Close the circuit first.
for i := 0; i < 10; i++ {
cb.Run(context.TODO(), errf)
}
// Wait the circuit in open state to go in half open state.
time.Sleep(6 * time.Millisecond)
return okf
},
expErr: nil,
},
{
name: "A circuit in half open state should return the execution result while it's on this phase.",
cfg: circuitbreaker.Config{
ErrorPercentThresholdToOpen: 30,
MinimumRequestToOpen: 10,
WaitDurationInOpenState: 5 * time.Millisecond,
},
f: func(cb goresilience.Runner) goresilience.Func {
// Close the circuit first.
for i := 0; i < 10; i++ {
cb.Run(context.TODO(), errf)
}
// Wait the circuit in open state to go in half open state.
time.Sleep(6 * time.Millisecond)
return errf
},
expErr: err,
},
{
name: "A circuit in half open state should open the circuit if the execution errors.",
cfg: circuitbreaker.Config{
ErrorPercentThresholdToOpen: 30,
MinimumRequestToOpen: 10,
WaitDurationInOpenState: 5 * time.Millisecond,
},
f: func(cb goresilience.Runner) goresilience.Func {
// Close the circuit first.
for i := 0; i < 10; i++ {
cb.Run(context.TODO(), errf)
}
// Wait the circuit in open state to go in half open state.
time.Sleep(6 * time.Millisecond)
// Trigger from half open to open again.
cb.Run(context.TODO(), errf)
return errf
},
expErr: errors.ErrCircuitOpen,
},
{
name: "A circuit in half open state should close the circuit if the execution success.",
cfg: circuitbreaker.Config{
ErrorPercentThresholdToOpen: 30,
MinimumRequestToOpen: 10,
WaitDurationInOpenState: 5 * time.Millisecond,
},
f: func(cb goresilience.Runner) goresilience.Func {
// Close the circuit first.
for i := 0; i < 10; i++ {
cb.Run(context.TODO(), errf)
}
// Wait the circuit in open state to go in half open state.
time.Sleep(6 * time.Millisecond)
// Trigger from half open to close.
cb.Run(context.TODO(), okf)
return okf
},
expErr: nil,
},
{
name: "The sliding window should forget the old recorded settings (short window enought to forget).",
cfg: circuitbreaker.Config{
ErrorPercentThresholdToOpen: 30,
MinimumRequestToOpen: 10,
SuccessfulRequiredOnHalfOpen: 1,
WaitDurationInOpenState: 5 * time.Millisecond,
MetricsSlidingWindowBucketQuantity: 5,
MetricsBucketDuration: 5 * time.Millisecond,
},
f: func(cb goresilience.Runner) goresilience.Func {
// Close the circuit first.
for i := 0; i < 10; i++ {
cb.Run(context.TODO(), errf)
}
// Wait the circuit in open state to go in half open state.
time.Sleep(6 * time.Millisecond)
// Open the circuit with lots of good requests.
for i := 0; i < 100; i++ {
cb.Run(context.TODO(), okf)
}
// Wait to reset the window.
time.Sleep(30 * time.Millisecond)
// Try to close again with the same number of errors.
// This should closed, this means that the 100 good
// request that we made before don't count for the
// percent rate threshold. So we can assure that the
// sliding window has worked and it forgot the previous
// metrics.
for i := 0; i < 10; i++ {
cb.Run(context.TODO(), errf)
}
cb.Run(context.TODO(), okf)
return okf
},
expErr: errors.ErrCircuitOpen,
},
{
name: "The sliding window should forget the old recorded settings (big window not enought to forget).",
cfg: circuitbreaker.Config{
ErrorPercentThresholdToOpen: 30,
MinimumRequestToOpen: 10,
SuccessfulRequiredOnHalfOpen: 1,
WaitDurationInOpenState: 5 * time.Millisecond,
MetricsSlidingWindowBucketQuantity: 100,
MetricsBucketDuration: 5 * time.Millisecond,
},
f: func(cb goresilience.Runner) goresilience.Func {
// Close the circuit first.
for i := 0; i < 10; i++ {
cb.Run(context.TODO(), errf)
}
// Wait the circuit in open state to go in half open state.
time.Sleep(6 * time.Millisecond)
// Open the circuit with lots of good requests.
for i := 0; i < 100; i++ {
cb.Run(context.TODO(), okf)
}
// Wait to reset the window
time.Sleep(30 * time.Millisecond)
// Try to close again with the same number of errors.
// This should not close, this means that the 100 good
// request that we made before are on the record regsitry
// and count for the percent threshold. So we can assure
// that the sliding window has worked and it dind't forgot
// the previous metrics because it didn't slide yet.
for i := 0; i < 10; i++ {
cb.Run(context.TODO(), errf)
}
cb.Run(context.TODO(), okf)
return okf
},
expErr: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert := assert.New(t)
cb := circuitbreaker.New(test.cfg)
err := cb.Run(context.TODO(), test.f(cb))
assert.Equal(test.expErr, err)
})
}
}
func BenchmarkCircuitBreaker(b *testing.B) {
b.StopTimer()
benchs := []struct {
name string
f func(cb goresilience.Runner) goresilience.Func
cfg circuitbreaker.Config
}{
{
name: "benchmark with default settings.",
f: func(cb goresilience.Runner) goresilience.Func {
return errf
},
cfg: circuitbreaker.Config{},
},
}
for _, bench := range benchs {
b.Run(bench.name, func(b *testing.B) {
// Prepare.
cb := circuitbreaker.New(bench.cfg)
f := bench.f(cb)
// execute the benhmark.
for n := 0; n < b.N; n++ {
b.StartTimer()
cb.Run(context.TODO(), f)
b.StopTimer()
}
})
}
}