|
0 |
package circuitbreaker_test
|
|
1 |
|
|
2 |
import (
|
|
3 |
"errors"
|
|
4 |
"testing"
|
|
5 |
"time"
|
|
6 |
|
|
7 |
"github.com/sony/gobreaker"
|
|
8 |
"golang.org/x/net/context"
|
|
9 |
|
|
10 |
"github.com/go-kit/kit/circuitbreaker"
|
|
11 |
"github.com/go-kit/kit/endpoint"
|
|
12 |
)
|
|
13 |
|
|
14 |
func TestSonyCircuitBreaker(t *testing.T) {
|
|
15 |
var (
|
|
16 |
thru int
|
|
17 |
last gobreaker.State
|
|
18 |
myError = errors.New("❤️")
|
|
19 |
timeout = time.Millisecond
|
|
20 |
stateChange = func(_ string, from, to gobreaker.State) { last = to }
|
|
21 |
)
|
|
22 |
|
|
23 |
var e endpoint.Endpoint
|
|
24 |
e = func(context.Context, interface{}) (interface{}, error) { thru++; return struct{}{}, myError }
|
|
25 |
e = circuitbreaker.NewSonyCircuitBreaker(gobreaker.Settings{
|
|
26 |
Timeout: timeout,
|
|
27 |
OnStateChange: stateChange,
|
|
28 |
})(e)
|
|
29 |
|
|
30 |
// "Default ReadyToTrip returns true when the number of consecutive
|
|
31 |
// failures is more than 5."
|
|
32 |
// https://github.com/sony/gobreaker/blob/bfa846d/gobreaker.go#L76
|
|
33 |
for i := 0; i < 5; i++ {
|
|
34 |
if _, err := e(context.Background(), struct{}{}); err != myError {
|
|
35 |
t.Errorf("want %v, have %v", myError, err)
|
|
36 |
}
|
|
37 |
}
|
|
38 |
|
|
39 |
if want, have := 5, thru; want != have {
|
|
40 |
t.Errorf("want %d, have %d", want, have)
|
|
41 |
}
|
|
42 |
|
|
43 |
e(context.Background(), struct{}{})
|
|
44 |
if want, have := 6, thru; want != have { // got thru
|
|
45 |
t.Errorf("want %d, have %d", want, have)
|
|
46 |
}
|
|
47 |
if want, have := gobreaker.StateOpen, last; want != have { // tripped
|
|
48 |
t.Errorf("want %v, have %v", want, have)
|
|
49 |
}
|
|
50 |
|
|
51 |
e(context.Background(), struct{}{})
|
|
52 |
if want, have := 6, thru; want != have { // didn't get thru
|
|
53 |
t.Errorf("want %d, have %d", want, have)
|
|
54 |
}
|
|
55 |
|
|
56 |
time.Sleep(2 * timeout)
|
|
57 |
|
|
58 |
e(context.Background(), struct{}{})
|
|
59 |
if want, have := 7, thru; want != have { // got thru via halfopen
|
|
60 |
t.Errorf("want %d, have %d", want, have)
|
|
61 |
}
|
|
62 |
if want, have := gobreaker.StateOpen, last; want != have { // re-tripped
|
|
63 |
t.Errorf("want %v, have %v", want, have)
|
|
64 |
}
|
|
65 |
|
|
66 |
time.Sleep(2 * timeout)
|
|
67 |
|
|
68 |
myError = nil
|
|
69 |
e(context.Background(), struct{}{})
|
|
70 |
if want, have := 8, thru; want != have { // got thru via halfopen
|
|
71 |
t.Errorf("want %d, have %d", want, have)
|
|
72 |
}
|
|
73 |
if want, have := gobreaker.StateClosed, last; want != have { // now it's good
|
|
74 |
t.Errorf("want %v, have %v", want, have)
|
|
75 |
}
|
|
76 |
}
|