-
Notifications
You must be signed in to change notification settings - Fork 146
/
breaker_test.go
232 lines (207 loc) · 5.02 KB
/
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
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
package breaker
import (
"errors"
"testing"
"time"
)
var errSomeError = errors.New("errSomeError")
func alwaysPanics() error {
panic("foo")
}
func returnsError() error {
return errSomeError
}
func returnsSuccess() error {
return nil
}
func TestBreakerErrorExpiry(t *testing.T) {
breaker := New(2, 1, 10*time.Millisecond)
if breaker.GetState() != Closed {
t.Error("incorrect state")
}
for i := 0; i < 3; i++ {
if err := breaker.Run(returnsError); err != errSomeError {
t.Error(err)
}
time.Sleep(10 * time.Millisecond)
}
if breaker.GetState() != Closed {
t.Error("incorrect state")
}
for i := 0; i < 3; i++ {
if err := breaker.Go(returnsError); err != nil {
t.Error(err)
}
time.Sleep(10 * time.Millisecond)
}
if breaker.GetState() != Closed {
t.Error("incorrect state")
}
}
func TestBreakerPanicsCountAsErrors(t *testing.T) {
breaker := New(3, 2, 1*time.Second)
if breaker.GetState() != Closed {
t.Error("incorrect state")
}
// three errors opens the breaker
for i := 0; i < 3; i++ {
func() {
defer func() {
val := recover()
if val.(string) != "foo" {
t.Error("incorrect panic")
}
}()
if err := breaker.Run(alwaysPanics); err != nil {
t.Error(err)
}
t.Error("shouldn't get here")
}()
}
// breaker is open
if breaker.GetState() != Open {
t.Error("incorrect state")
}
for i := 0; i < 5; i++ {
if err := breaker.Run(returnsError); err != ErrBreakerOpen {
t.Error(err)
}
}
}
func TestBreakerStateTransitions(t *testing.T) {
breaker := New(3, 2, 10*time.Millisecond)
if breaker.GetState() != Closed {
t.Error("incorrect state")
}
// three errors opens the breaker
for i := 0; i < 3; i++ {
if err := breaker.Run(returnsError); err != errSomeError {
t.Error(err)
}
}
// breaker is open
if breaker.GetState() != Open {
t.Error("incorrect state")
}
for i := 0; i < 5; i++ {
if err := breaker.Run(returnsError); err != ErrBreakerOpen {
t.Error(err)
}
}
// wait for it to half-close
time.Sleep(20 * time.Millisecond)
if breaker.GetState() != HalfOpen {
t.Error("incorrect state")
}
// one success works, but is not enough to fully close
if err := breaker.Run(returnsSuccess); err != nil {
t.Error(err)
}
// error works, but re-opens immediately
if err := breaker.Run(returnsError); err != errSomeError {
t.Error(err)
}
// breaker is open
if breaker.GetState() != Open {
t.Error("incorrect state")
}
if err := breaker.Run(returnsError); err != ErrBreakerOpen {
t.Error(err)
}
// wait for it to half-close
time.Sleep(20 * time.Millisecond)
if breaker.GetState() != HalfOpen {
t.Error("incorrect state")
}
// two successes is enough to close it for good
for i := 0; i < 2; i++ {
if err := breaker.Run(returnsSuccess); err != nil {
t.Error(err)
}
}
if breaker.GetState() != Closed {
t.Error("incorrect state")
}
// error works
if err := breaker.Run(returnsError); err != errSomeError {
t.Error(err)
}
// breaker is still closed
if breaker.GetState() != Closed {
t.Error("incorrect state")
}
if err := breaker.Run(returnsSuccess); err != nil {
t.Error(err)
}
}
func TestBreakerAsyncStateTransitions(t *testing.T) {
breaker := New(3, 2, 10*time.Millisecond)
// three errors opens the breaker
for i := 0; i < 3; i++ {
if err := breaker.Go(returnsError); err != nil {
t.Error(err)
}
}
// just enough to yield the scheduler and let the goroutines work off
time.Sleep(1 * time.Millisecond)
// breaker is open
for i := 0; i < 5; i++ {
if err := breaker.Go(returnsError); err != ErrBreakerOpen {
t.Error(err)
}
}
// wait for it to half-close
time.Sleep(20 * time.Millisecond)
// one success works, but is not enough to fully close
if err := breaker.Go(returnsSuccess); err != nil {
t.Error(err)
}
// error works, but re-opens immediately
if err := breaker.Go(returnsError); err != nil {
t.Error(err)
}
// just enough to yield the scheduler and let the goroutines work off
time.Sleep(1 * time.Millisecond)
// breaker is open
if err := breaker.Go(returnsError); err != ErrBreakerOpen {
t.Error(err)
}
// wait for it to half-close
time.Sleep(20 * time.Millisecond)
// two successes is enough to close it for good
for i := 0; i < 2; i++ {
if err := breaker.Go(returnsSuccess); err != nil {
t.Error(err)
}
}
// just enough to yield the scheduler and let the goroutines work off
time.Sleep(1 * time.Millisecond)
// error works
if err := breaker.Go(returnsError); err != nil {
t.Error(err)
}
// just enough to yield the scheduler and let the goroutines work off
time.Sleep(1 * time.Millisecond)
// breaker is still closed
if err := breaker.Go(returnsSuccess); err != nil {
t.Error(err)
}
}
func ExampleBreaker() {
breaker := New(3, 1, 5*time.Second)
for {
result := breaker.Run(func() error {
// communicate with some external service and
// return an error if the communication failed
return nil
})
switch result {
case nil:
// success!
case ErrBreakerOpen:
// our function wasn't run because the breaker was open
default:
// some other error
}
}
}