-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhoglet_test.go
217 lines (189 loc) · 5.17 KB
/
hoglet_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
package hoglet
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var sentinel = errors.New("sentinel error")
type noopIn int
const (
noopInSuccess noopIn = iota
noopInFailure
noopInPanic
)
// noop is just a simple breakable function for tests.
func noop(ctx context.Context, in noopIn) (struct{}, error) {
switch in {
case noopInSuccess:
return struct{}{}, nil
case noopInFailure:
return struct{}{}, sentinel
default: // noopInPanic
panic("boom")
}
}
func BenchmarkHoglet_Do_EWMA(b *testing.B) {
h, err := NewCircuit(
func(context.Context, struct{}) (out struct{}, err error) { return },
NewEWMABreaker(10, 0.9),
WithHalfOpenDelay(time.Second),
// WithBreakerMiddleware(ConcurrencyLimiter(1, true)),
)
require.NoError(b, err)
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = h.Call(ctx, struct{}{})
}
})
}
func BenchmarkHoglet_Do_SlidingWindow(b *testing.B) {
h, err := NewCircuit(
func(context.Context, struct{}) (out struct{}, err error) { return },
NewSlidingWindowBreaker(10*time.Second, 0.9),
// WithBreakerMiddleware(ConcurrencyLimiter(1, true)),
)
require.NoError(b, err)
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = h.Call(ctx, struct{}{})
}
})
}
func TestBreaker_nil_breaker_does_not_open(t *testing.T) {
b, err := NewCircuit(noop, nil)
require.NoError(t, err)
_, err = b.Call(context.Background(), noopInFailure)
assert.Equal(t, sentinel, err)
_, err = b.Call(context.Background(), noopInFailure)
assert.Equal(t, sentinel, err)
}
func TestBreaker_ctx_parameter_not_cancelled(t *testing.T) {
b, err := NewCircuit(func(ctx context.Context, _ any) (context.Context, error) {
return ctx, nil
}, nil)
require.NoError(t, err)
ctx, err := b.Call(context.Background(), noopInSuccess)
require.NoError(t, err)
assert.NoError(t, ctx.Err())
}
func TestCircuit_ignored_context_cancellation_still_returned(t *testing.T) {
b, err := NewCircuit(
func(ctx context.Context, _ any) (string, error) {
return "expected", ctx.Err()
},
nil,
WithFailureCondition(IgnoreContextCanceled))
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
cancel()
out, err := b.Call(ctx, nil)
assert.ErrorIs(t, err, context.Canceled)
assert.Equal(t, "expected", out)
}
// mockBreaker is a mock implementation of the [Breaker] interface that opens or closes depending on the last observed
// failure.
type mockBreaker struct{}
// observer implements [Breaker]
func (mt *mockBreaker) observe(halfOpen, failure bool) stateChange {
if failure {
return stateChangeOpen
}
return stateChangeClose
}
func (mt *mockBreaker) apply(o *options) error {
return nil
}
func TestHoglet_Do(t *testing.T) {
type calls struct {
arg noopIn
halfOpen bool // put the breaker in the half-open state BEFORE calling
wantErr error
wantPanic any
}
tests := []struct {
name string
calls []calls
}{
{
name: "no errors; always closed",
calls: []calls{
{arg: noopInSuccess, wantErr: nil},
{arg: noopInSuccess, wantErr: nil},
{arg: noopInSuccess, wantErr: nil},
},
},
{
name: "error opens",
calls: []calls{
{arg: noopInSuccess, wantErr: nil},
{arg: noopInFailure, wantErr: sentinel},
{arg: noopInSuccess, wantErr: ErrCircuitOpen},
},
},
{
name: "panic opens",
calls: []calls{
{arg: noopInSuccess, wantErr: nil},
{arg: noopInPanic, wantErr: nil, wantPanic: "boom"},
{arg: noopInSuccess, wantErr: ErrCircuitOpen},
},
},
{
name: "success on half-open closes",
calls: []calls{
{arg: noopInSuccess, wantErr: nil},
{arg: noopInFailure, wantErr: sentinel},
{arg: noopInSuccess, wantErr: nil, halfOpen: true},
{arg: noopInSuccess, wantErr: nil},
},
},
{
name: "failure on half-open keeps open",
calls: []calls{
{arg: noopInSuccess, wantErr: nil},
{arg: noopInFailure, wantErr: sentinel},
{arg: noopInFailure, wantErr: sentinel, halfOpen: true},
{arg: noopInSuccess, wantErr: ErrCircuitOpen},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
mt := &mockBreaker{}
h, err := NewCircuit(noop, mt, WithHalfOpenDelay(time.Minute))
require.NoError(t, err)
for i, call := range tt.calls {
if call.halfOpen {
// simulate passage of time
h.openedAt.Store(int64(time.Now().Add(-h.halfOpenDelay).UnixMicro()))
}
var err error
maybeAssertPanic(t, func() {
_, err = h.Call(context.Background(), call.arg)
}, call.wantPanic)
assert.Equal(t, call.wantErr, err, "unexpected error on call %d: %v", i, err)
}
})
}
}
// maybeAssertPanic is a test-table helper to assert that a function panics or not, depending on the value of wantPanic.
func maybeAssertPanic(t *testing.T, f func(), wantPanic any) {
wrapped := assert.NotPanics
if wantPanic != nil {
wrapped = func(t assert.TestingT, f assert.PanicTestFunc, msgAndArgs ...interface{}) bool {
return assert.PanicsWithValue(t, wantPanic, f, msgAndArgs...)
}
}
wrapped(t, f)
}