-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsmartcb_test.go
302 lines (272 loc) · 7.61 KB
/
smartcb_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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package smartcb_test
import (
"errors"
"log"
"math/rand"
"sync"
"testing"
"time"
"github.com/codemartial/smartcb"
"github.com/rubyist/circuitbreaker"
)
const minFail = 0.001 // Must match smartcb.minFail (which is unexported)
func Example() {
// Initialise policies and set max. tolerable failure rate
// to 15% (= 0.15)
policies := smartcb.NewPolicies()
policies.MaxFail = 0.15
// Create a SmartTripper Generator for a 10k QPS task
var st = smartcb.NewSmartTripper(100000, policies)
// Create a Circuit Breaker from the SmartTripper Generator
var scb = smartcb.NewSmartCircuitBreaker(st)
// The task to be wrapped with the circuit breaker
protectedTask := func(errRate float64) (err error) {
if rand.Float64() < errRate {
return errors.New("forced error")
}
return nil
}
breakerEvents := scb.Subscribe()
// Let's run the example for 200 ms
stop := time.After(time.Millisecond * 200)
loop := true
for loop {
select {
case <-stop: // Stop execution now
loop = false
case e := <-breakerEvents: // Something changed with the circuit breaker
if e == circuit.BreakerTripped {
log.Println("Circuit Breaker tripped.", scb.ErrorRate(), st.State(), st.LearnedRate())
return
}
default: // Execute the task using circuit.Breaker.Call() method
_ = scb.Call(func() error { return protectedTask(0.02) }, time.Second)
}
}
}
func protectedTask(errRate float64) (err error) {
if rand.Float64() < errRate {
return errors.New("forced error")
}
return nil
}
func TestSmartCB(t *testing.T) {
st := smartcb.NewSmartTripper(10000, smartcb.NewPolicies())
scb := smartcb.NewSmartCircuitBreaker(st)
t.Run("Learning", func(t *testing.T) {
scb.Reset()
testStop := time.After(time.Millisecond * 1100)
bEvents := scb.Subscribe()
loop := true
for loop {
select {
case <-testStop:
loop = false
case e := <-bEvents:
if e == circuit.BreakerTripped {
t.Error("Circuit Breaker tripped in Learning Phase.", scb.ErrorRate(), st.State(), st.LearnedRate())
return
}
default:
_ = scb.Call(func() error { return protectedTask(0.02) }, time.Second)
}
}
})
t.Run("OperationFail", func(t *testing.T) {
scb.ResetCounters()
if st.State() != smartcb.Learned {
t.Error("Circuit Breaker is still learning")
}
for i := 0; i < 1000; i++ {
err := scb.Call(func() error { return protectedTask(0.2) }, time.Second)
if err != nil && scb.Tripped() {
break
}
}
if !scb.Tripped() {
t.Error("Circuit Breaker did not trip beyond learned failure rate", scb.ErrorRate(), st.LearnedRate())
}
})
t.Run("OperationSuccess", func(t *testing.T) {
scb.Reset()
for {
_ = scb.Call(func() error { return protectedTask(0) }, time.Second)
if scb.Ready() && scb.Successes() > 50 {
break
}
}
for j := 0; j < 100; j++ {
err := scb.Call(func() error { return protectedTask(0.018) }, time.Second)
if err != nil && scb.Tripped() {
t.Error(j, "Circuit breaker tripped unexpectedly", scb.ErrorRate())
return
}
}
})
t.Run("MaxFail", func(t *testing.T) {
for i := 0; i < 1000; i++ {
err := scb.Call(func() error { return protectedTask(smartcb.NewPolicies().MaxFail) }, time.Second)
if err != nil && scb.Tripped() {
break
}
}
if !scb.Tripped() {
t.Error("Circuit Breaker did not trip at max failure rate", scb.ErrorRate(), st.LearnedRate())
}
})
}
func TestInvalidDuration(t *testing.T) {
defer func() {
if recover() == nil {
t.Error("No panic despite invalid response duration")
}
}()
_ = smartcb.NewSmartTripper(0, smartcb.NewPolicies())
}
// Test that the breaker doesn't learn a high failure rate
func TestLearnGuard(t *testing.T) {
st := smartcb.NewSmartTripper(100000, smartcb.NewPolicies())
scb := smartcb.NewSmartCircuitBreaker(st)
loop := true
testStop := time.After(time.Millisecond * 110)
for loop {
select {
case <-testStop:
loop = false
default:
scb.Call(func() error { return protectedTask(smartcb.NewPolicies().MaxFail * 1.1) }, 0)
}
}
if st.LearnedRate() > smartcb.NewPolicies().MaxFail {
t.Error("Circuit Breaker learned a dangerous failure rate ", st.LearnedRate())
}
}
func TestLowLiquidity(t *testing.T) {
p := smartcb.NewPolicies()
scb := smartcb.NewSmartCircuitBreaker(smartcb.NewSmartTripper(10000, p))
for i := 1; i < int(p.SamplesPerWindow/10); i++ {
scb.Call(func() error { return protectedTask(1.0) }, time.Second)
if scb.Tripped() {
t.Error("Circuit Breaker tripped unreasonably ", scb.ErrorRate(), scb.Failures()+scb.Successes())
}
}
}
func concurrentCaller(errRate float64, scb *circuit.Breaker) {
var wg sync.WaitGroup
wg.Add(32) //MAGIC
for i := 0; i < 32; i++ {
go func() {
_ = scb.Call(func() error { return protectedTask(errRate) }, time.Second)
wg.Done()
}()
}
wg.Wait()
}
func TestConcurrency(t *testing.T) {
st := smartcb.NewSmartTripper(10000, smartcb.NewPolicies())
scb := smartcb.NewSmartCircuitBreaker(st)
testStop := time.After(time.Second * 2)
loop := true
bEvents := scb.Subscribe()
concurrentCall := func(errRate float64) { concurrentCaller(errRate, scb) }
for loop {
select {
case <-testStop:
loop = false
case e := <-bEvents:
if e == circuit.BreakerTripped {
loop = false
t.Error("Circuit Breaker tripped in Learning Phase.", scb.ErrorRate(), st.State(), st.LearnedRate())
}
default:
concurrentCall(0.02)
}
}
tripped := false
testStop = time.After(time.Second * 1)
loop = true
for loop {
select {
case <-testStop:
loop = false
case e := <-bEvents:
if e == circuit.BreakerTripped {
tripped = true
}
default:
concurrentCall(0.2)
}
}
if !tripped {
t.Error("Circuit Breaker didn't trip above learned rate", scb.ErrorRate(), st.LearnedRate())
}
testStop = time.After(time.Second * 2)
loop = true
for loop {
select {
case <-testStop:
loop = false
default:
concurrentCall(0.02)
}
}
}
func TestStateLabels(t *testing.T) {
tests := map[smartcb.State]string{smartcb.Learning: "Learning", smartcb.Learned: "Learned"}
for k, v := range tests {
t.Run(v, func(t *testing.T) {
if k.String() != v {
t.Error("Invalid Label for Learning State. Expected ", v, ", got ", k.String())
}
})
}
}
func TestZeroErrorLearning(t *testing.T) {
policies := smartcb.NewPolicies()
st := smartcb.NewSmartTripper(100000, policies)
scb := smartcb.NewSmartCircuitBreaker(st)
loop := true
testStop := time.After(time.Millisecond * 200)
for loop {
var err error
select {
case <-testStop:
err = scb.Call(func() error { return protectedTask(100) }, time.Second)
loop = false
default:
if err = scb.Call(func() error { return protectedTask(minFail / 2.0) }, time.Second); err != nil && scb.Tripped() {
t.Error("Circuit breaker tripped in Learning Phase.", scb.ErrorRate(), st.State(), st.LearnedRate())
return
}
}
}
if st.State() != smartcb.Learned {
t.Error("Circuit breaker is still learning")
}
if st.LearnedRate() < minFail {
t.Error("Circuit breaker learned abnormally low error rate", st.LearnedRate(), "Expected rate was >=", minFail)
}
}
func TestInitState(t *testing.T) {
st := smartcb.NewSmartTripper(1000, smartcb.NewPolicies())
if st.State() == smartcb.Learning {
t.Error("Circuit breaker initialised in Learning state")
}
}
func BenchmarkCB(b *testing.B) {
st := smartcb.NewSmartTripper(100000, smartcb.NewPolicies())
scb := smartcb.NewSmartCircuitBreaker(st)
bEvents := scb.Subscribe()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
scb.Call(func() error { return protectedTask(0.0) }, 0)
select {
case e := <-bEvents:
if e == circuit.BreakerTripped || e == circuit.BreakerFail {
b.Error("Circuit breaker failed")
}
default:
}
}
})
}