-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbreaker_test.go
More file actions
202 lines (192 loc) · 6.63 KB
/
breaker_test.go
File metadata and controls
202 lines (192 loc) · 6.63 KB
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
package hoglet
import (
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestEWMABreaker_zero_value_does_not_open(t *testing.T) {
b := &EWMABreaker{}
s := b.observe(false, true)
assert.NotEqual(t, stateChangeOpen, s)
}
func TestEWMABreaker_zero_value_does_not_panic(t *testing.T) {
b := &EWMABreaker{}
assert.NotPanics(t, func() {
b.observe(false, true) // nolint: errcheck // we are just interested in the panic
})
}
func TestBreaker_Observe_State(t *testing.T) {
// helper functions to make tests stages more readable
alwaysFailure := func(int) bool { return true }
alwaysSuccessful := func(int) bool { return false }
type stages struct {
calls int
failureFunc func(int) bool
waitForHalfOpen bool // whether to put circuit in half-open BEFORE observing the call's result
wantStateChange stateChange // expected state change at the END of the stage
}
tests := []struct {
name string
breakers map[string]Breaker
stages []stages
}{
{
name: "start closed",
breakers: map[string]Breaker{
"ewma": NewEWMABreaker(10, 0.3),
"slidingwindow": NewSlidingWindowBreaker(10*time.Second, 0.3),
},
stages: []stages{
{calls: 1, failureFunc: alwaysSuccessful, wantStateChange: stateChangeClose},
},
},
{
name: "always success",
breakers: map[string]Breaker{
"ewma": NewEWMABreaker(10, 0.3),
"slidingwindow": NewSlidingWindowBreaker(10*time.Second, 0.3),
},
stages: []stages{
{calls: 100, failureFunc: alwaysSuccessful, wantStateChange: stateChangeClose},
},
},
{
name: "always failure",
breakers: map[string]Breaker{
"ewma": NewEWMABreaker(10, 0.9),
"slidingwindow": NewSlidingWindowBreaker(10*time.Second, 0.9),
},
stages: []stages{
{calls: 100, failureFunc: alwaysFailure, wantStateChange: stateChangeOpen},
},
},
{
name: "start open; finish closed",
breakers: map[string]Breaker{
"ewma": NewEWMABreaker(10, 0.2),
// sliding window is not affected by ordering
},
stages: []stages{
{calls: 100, failureFunc: alwaysFailure, wantStateChange: stateChangeOpen},
{calls: 100, failureFunc: alwaysSuccessful, wantStateChange: stateChangeClose},
},
},
{
name: "start closed; finish open",
breakers: map[string]Breaker{
"ewma": NewEWMABreaker(50, 0.4),
// sliding window is not affected by ordering
},
stages: []stages{
{calls: 100, failureFunc: alwaysSuccessful, wantStateChange: stateChangeClose},
{calls: 100, failureFunc: alwaysFailure, wantStateChange: stateChangeOpen},
},
},
{
name: "just above threshold opens",
breakers: map[string]Breaker{
"slidingwindow": NewSlidingWindowBreaker(10*time.Second, 0.5),
},
stages: []stages{
{calls: 100, failureFunc: alwaysSuccessful, wantStateChange: stateChangeClose},
{calls: 101, failureFunc: alwaysFailure, wantStateChange: stateChangeOpen},
},
},
{
name: "just below threshold stays closed",
breakers: map[string]Breaker{
"slidingwindow": NewSlidingWindowBreaker(10*time.Second, 0.5),
},
stages: []stages{
{calls: 101, failureFunc: alwaysSuccessful, wantStateChange: stateChangeClose},
{calls: 100, failureFunc: alwaysFailure, wantStateChange: stateChangeClose},
},
},
{
name: "constant low failure rate stays mostly closed (EWMA flaky)",
breakers: map[string]Breaker{
"ewma": NewEWMABreaker(50, 0.2),
"slidingwindow": NewSlidingWindowBreaker(10*time.Second, 0.2),
},
stages: []stages{
{calls: 100, failureFunc: func(int) bool { return rand.Float64() < 0.1 }, wantStateChange: stateChangeClose},
},
},
{
name: "constant high failure rate stays mostly open (EWMA flaky)",
breakers: map[string]Breaker{
"ewma": NewEWMABreaker(50, 0.2),
"slidingwindow": NewSlidingWindowBreaker(10*time.Second, 0.2),
},
stages: []stages{
{calls: 100, failureFunc: func(int) bool { return rand.Float64() < 0.4 }, wantStateChange: stateChangeOpen},
},
},
{
name: "single success at half-open enough to close",
breakers: map[string]Breaker{
"ewma": NewEWMABreaker(50, 0.1),
"slidingwindow": NewSlidingWindowBreaker(10*time.Second, 0.1),
},
stages: []stages{
{calls: 100, failureFunc: alwaysFailure, wantStateChange: stateChangeOpen},
{calls: 1, failureFunc: alwaysSuccessful, waitForHalfOpen: true, wantStateChange: stateChangeClose},
},
},
{
name: "single failure at half-open keeps open",
breakers: map[string]Breaker{
"ewma": NewEWMABreaker(50, 0.1),
"slidingwindow": NewSlidingWindowBreaker(10*time.Second, 0.1),
},
stages: []stages{
{calls: 100, failureFunc: alwaysFailure, wantStateChange: stateChangeOpen},
{calls: 1, failureFunc: alwaysFailure, waitForHalfOpen: true, wantStateChange: stateChangeOpen},
},
},
{
// we want to re-open fast if we closed on a fluke (to avoid thundering herd agains a service that might be
// close to capacity and therefore failing intermittently)
name: "single failure after reopen closes",
breakers: map[string]Breaker{
"ewma": NewEWMABreaker(50, 0.1),
"slidingwindow": NewSlidingWindowBreaker(10*time.Second, 0.1),
},
stages: []stages{
{calls: 100, failureFunc: alwaysFailure, wantStateChange: stateChangeOpen},
{calls: 1, failureFunc: alwaysSuccessful, waitForHalfOpen: true, wantStateChange: stateChangeClose},
{calls: 1, failureFunc: alwaysFailure, wantStateChange: stateChangeOpen},
},
},
}
for _, tt := range tests {
for bName, b := range tt.breakers {
t.Run(bName+": "+tt.name, func(t *testing.T) {
t.Parallel()
for _, s := range tt.stages {
var lastStateChange stateChange
for i := 1; i <= s.calls; i++ {
failure := s.failureFunc(i)
switch b := b.(type) {
case *EWMABreaker:
lastStateChange = ignoreNone(lastStateChange, b.observe(s.waitForHalfOpen && i == s.calls, failure))
// t.Logf("%s: sample %d: failure %v: failureRate %f => %v", tt.name, i, failure, b.failureRate.Load(), b.circuit.State())
case *SlidingWindowBreaker:
lastStateChange = ignoreNone(lastStateChange, b.observe(s.waitForHalfOpen && i == s.calls, failure))
// t.Logf("%s: sample %d: failure %v: => %v", tt.name, i, failure, b.circuit.State())
}
}
assert.Equal(t, s.wantStateChange, lastStateChange, "expected %q, got %q", s.wantStateChange, lastStateChange)
}
})
}
}
}
// ignoreNone is a small helper to skip the "none" state change and only record the last "effective" state change.
func ignoreNone(old, new stateChange) stateChange {
if new == stateChangeNone {
return old
}
return new
}