forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgooglebreaker_test.go
235 lines (211 loc) · 5.17 KB
/
googlebreaker_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
package breaker
import (
"errors"
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/core/collection"
"github.com/tal-tech/go-zero/core/mathx"
"github.com/tal-tech/go-zero/core/stat"
)
const (
testBuckets = 10
testInterval = time.Millisecond * 10
)
func init() {
stat.SetReporter(nil)
}
func getGoogleBreaker() *googleBreaker {
st := collection.NewRollingWindow(testBuckets, testInterval)
return &googleBreaker{
stat: st,
k: 5,
proba: mathx.NewProba(),
}
}
func markSuccessWithDuration(b *googleBreaker, count int, sleep time.Duration) {
for i := 0; i < count; i++ {
b.markSuccess()
time.Sleep(sleep)
}
}
func markFailedWithDuration(b *googleBreaker, count int, sleep time.Duration) {
for i := 0; i < count; i++ {
b.markFailure()
time.Sleep(sleep)
}
}
func TestGoogleBreakerClose(t *testing.T) {
b := getGoogleBreaker()
markSuccess(b, 80)
assert.Nil(t, b.accept())
markSuccess(b, 120)
assert.Nil(t, b.accept())
}
func TestGoogleBreakerOpen(t *testing.T) {
b := getGoogleBreaker()
markSuccess(b, 10)
assert.Nil(t, b.accept())
markFailed(b, 100000)
time.Sleep(testInterval * 2)
verify(t, func() bool {
return b.accept() != nil
})
}
func TestGoogleBreakerFallback(t *testing.T) {
b := getGoogleBreaker()
markSuccess(b, 1)
assert.Nil(t, b.accept())
markFailed(b, 10000)
time.Sleep(testInterval * 2)
verify(t, func() bool {
return b.doReq(func() error {
return errors.New("any")
}, func(err error) error {
return nil
}, defaultAcceptable) == nil
})
}
func TestGoogleBreakerReject(t *testing.T) {
b := getGoogleBreaker()
markSuccess(b, 100)
assert.Nil(t, b.accept())
markFailed(b, 10000)
time.Sleep(testInterval)
assert.Equal(t, ErrServiceUnavailable, b.doReq(func() error {
return ErrServiceUnavailable
}, nil, defaultAcceptable))
}
func TestGoogleBreakerAcceptable(t *testing.T) {
b := getGoogleBreaker()
errAcceptable := errors.New("any")
assert.Equal(t, errAcceptable, b.doReq(func() error {
return errAcceptable
}, nil, func(err error) bool {
return err == errAcceptable
}))
}
func TestGoogleBreakerNotAcceptable(t *testing.T) {
b := getGoogleBreaker()
errAcceptable := errors.New("any")
assert.Equal(t, errAcceptable, b.doReq(func() error {
return errAcceptable
}, nil, func(err error) bool {
return err != errAcceptable
}))
}
func TestGoogleBreakerPanic(t *testing.T) {
b := getGoogleBreaker()
assert.Panics(t, func() {
_ = b.doReq(func() error {
panic("fail")
}, nil, defaultAcceptable)
})
}
func TestGoogleBreakerHalfOpen(t *testing.T) {
b := getGoogleBreaker()
assert.Nil(t, b.accept())
t.Run("accept single failed/accept", func(t *testing.T) {
markFailed(b, 10000)
time.Sleep(testInterval * 2)
verify(t, func() bool {
return b.accept() != nil
})
})
t.Run("accept single failed/allow", func(t *testing.T) {
markFailed(b, 10000)
time.Sleep(testInterval * 2)
verify(t, func() bool {
_, err := b.allow()
return err != nil
})
})
time.Sleep(testInterval * testBuckets)
t.Run("accept single succeed", func(t *testing.T) {
assert.Nil(t, b.accept())
markSuccess(b, 10000)
verify(t, func() bool {
return b.accept() == nil
})
})
}
func TestGoogleBreakerSelfProtection(t *testing.T) {
t.Run("total request < 100", func(t *testing.T) {
b := getGoogleBreaker()
markFailed(b, 4)
time.Sleep(testInterval)
assert.Nil(t, b.accept())
})
t.Run("total request > 100, total < 2 * success", func(t *testing.T) {
b := getGoogleBreaker()
size := rand.Intn(10000)
accepts := size + 1
markSuccess(b, accepts)
markFailed(b, size-accepts)
assert.Nil(t, b.accept())
})
}
func TestGoogleBreakerHistory(t *testing.T) {
var b *googleBreaker
var accepts, total int64
sleep := testInterval
t.Run("accepts == total", func(t *testing.T) {
b = getGoogleBreaker()
markSuccessWithDuration(b, 10, sleep/2)
accepts, total = b.history()
assert.Equal(t, int64(10), accepts)
assert.Equal(t, int64(10), total)
})
t.Run("fail == total", func(t *testing.T) {
b = getGoogleBreaker()
markFailedWithDuration(b, 10, sleep/2)
accepts, total = b.history()
assert.Equal(t, int64(0), accepts)
assert.Equal(t, int64(10), total)
})
t.Run("accepts = 1/2 * total, fail = 1/2 * total", func(t *testing.T) {
b = getGoogleBreaker()
markFailedWithDuration(b, 5, sleep/2)
markSuccessWithDuration(b, 5, sleep/2)
accepts, total = b.history()
assert.Equal(t, int64(5), accepts)
assert.Equal(t, int64(10), total)
})
t.Run("auto reset rolling counter", func(t *testing.T) {
b = getGoogleBreaker()
time.Sleep(testInterval * testBuckets)
accepts, total = b.history()
assert.Equal(t, int64(0), accepts)
assert.Equal(t, int64(0), total)
})
}
func BenchmarkGoogleBreakerAllow(b *testing.B) {
breaker := getGoogleBreaker()
b.ResetTimer()
for i := 0; i <= b.N; i++ {
breaker.accept()
if i%2 == 0 {
breaker.markSuccess()
} else {
breaker.markFailure()
}
}
}
func markSuccess(b *googleBreaker, count int) {
for i := 0; i < count; i++ {
p, err := b.allow()
if err != nil {
break
}
p.Accept()
}
}
func markFailed(b *googleBreaker, count int) {
for i := 0; i < count; i++ {
p, err := b.allow()
if err == nil {
p.Reject()
}
}
}