-
Notifications
You must be signed in to change notification settings - Fork 0
/
breaker_test.go
305 lines (281 loc) · 7.28 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package breaker
import (
"fmt"
"os"
"runtime"
"strconv"
"strings"
"sync"
"testing"
"time"
)
func Test_is_ok(t *testing.T) {
fmt.Println("Testing Test_is_ok")
b := New("name", time.Second, 0)
if !b.isOk {
t.Errorf("Circuite should have been ok")
}
}
func Test_is_not_ok(t *testing.T) {
f, err := os.OpenFile("test.log", os.O_RDWR|os.O_CREATE, 0666)
fmt.Println(f)
fmt.Println(err)
fmt.Println("Testing Test_is_ok")
b := New("name", time.Second, 0)
b.Shutdown()
if b.isOk {
//t.Errorf("Circuite should not have been ok")
}
}
func Test_Shutdown(t *testing.T) {
fmt.Println("Testing Test_Shutdown")
b := New("name", time.Second, 0)
b.isOk = false
b.HealthCheckInterval = 5
b.Shutdown()
if b.status != iShutdown {
t.Errorf("Shutdown should have initiated")
}
}
func Test_scanner_circuit_repaired(t *testing.T) {
b := New("name", time.Second, 1)
b.isOk = false
b.HealthCheckInterval = 10
fmt.Println("starting Test_scanner_circuit_repaired")
time.Sleep(150 * time.Millisecond)
fmt.Println("Return = ", b.status)
if b.status != iCircuitGood {
t.Errorf("Circuit should have been repaired")
}
b.Shutdown()
fmt.Println("Return = ", b.status)
}
func Test_Execute_t(t *testing.T) {
commands := &wrapper{}
b := New("name", 10*time.Millisecond, 3)
b.HealthCheckInterval = 1000
var wg sync.WaitGroup
wg.Add(5)
for i := 0; i < 5; i++ {
go func(i int) {
defer wg.Done()
err := b.Execute(commands)
fmt.Println("Err = ", i, "___", err, "___")
}(i)
}
fmt.Println("Number of go routines = ", runtime.NumGoroutine())
wg.Wait()
fmt.Printf("isOk 1 = %v\n", b.isOk)
time.Sleep(200 * time.Millisecond)
fmt.Printf("isOk 2 = %v\n", b.isOk)
b.Shutdown()
fmt.Println("Done!!")
}
func Test_Execute_1(t *testing.T) {
fmt.Println("Throttle demo....")
b := New("name", 10*time.Millisecond, 3)
if !b.isOk {
t.Errorf("Circuit should be ok")
}
if b.isShutdown {
t.Errorf("Circuit should NOT be Shutdown")
}
b.Shutdown()
}
func Test_Execute_2(t *testing.T) {
fmt.Println("Running Test_Execute_2 demo....")
b := New("name", 10*time.Millisecond, 3)
w := &wrapper{exec: false}
b.Execute(w)
time.Sleep(5 * time.Millisecond)
if !w.exec {
t.Errorf("Service should have executed")
}
b.Shutdown()
}
func Test_Execute_exceed_limit_wait_till_circuit_ok(t *testing.T) {
fmt.Println("Running Test_Execute_exceed_limit_wait_till_circuit_ok demo....")
b := New("name", 2000*time.Millisecond, 3)
b.HealthCheckInterval = 1000
w1 := &wrapper2{"Service 1", false}
b.Execute(w1)
w2 := &wrapper2{"Service 2", false}
b.Execute(w2)
w3 := &wrapper2{"Service 3", false}
b.Execute(w3)
w4 := &wrapper2{"Service 4", false}
b.Execute(w4)
w5 := &wrapper2{"Service 5", false}
b.Execute(w5)
time.Sleep(2020 * time.Millisecond)
if !b.isOk {
t.Errorf("Circuit should have been repaired")
}
b.Shutdown()
}
func Test_execute_exceed_limit_wait_tillok_submit_more(t *testing.T) {
fmt.Println("Running Test_execute_exceed_limit_wait_tillok_submit_more demo....")
b := New("name", 10*time.Millisecond, 3)
b.HealthCheckInterval = 1000
w1 := &wrapper2{"Service 1", false}
b.Execute(w1)
w2 := &wrapper2{"Service 2", false}
b.Execute(w2)
w3 := &wrapper2{"Service 3", false}
b.Execute(w3)
w4 := &wrapper2{"Service 4", false}
b.Execute(w4)
w5 := &wrapper2{"Service 5", false}
b.Execute(w5)
time.Sleep(2020 * time.Millisecond)
if !b.isOk {
t.Errorf("Circuit should have been repaired")
}
b.Shutdown()
}
func Test_scanner_circuit_multipl_Shutdown(t *testing.T) {
b := New("name", time.Second, 1)
b.isOk = false
b.HealthCheckInterval = 10
fmt.Println("starting Test_scanner_circuit_multipl_Shutdown")
time.Sleep(15 * time.Millisecond)
fmt.Println("Return = ", b.status)
if b.status != iCircuitGood {
t.Errorf("Circuit should have been repaired")
}
b.Shutdown()
fmt.Println("Return = ", b.status)
b.Shutdown()
}
func Test_timeout_default(t *testing.T) {
b := New("name", time.Second, 1)
to := b.commandTimeout(nil)
if to != time.Second {
t.Errorf("Was expecting a time.Second, instead got %v", to)
}
to = b.commandTimeout(&wrapper3{})
if to != time.Millisecond {
t.Errorf("Was expecting a time.Second, instead got %v", to)
}
}
func Test_exeute_after_shutdown(t *testing.T) {
fmt.Println("Running Test_exeute_after_shutdown demo....")
b := New("name", 10*time.Millisecond, 3)
b.Shutdown()
w1 := &wrapper2{"Service 1", false}
ch := b.Execute(w1)
err := <-ch
fmt.Println(err)
if !strings.Contains(err.Error(), "cicuit has been permanently shutdown") {
t.Errorf("Should contain %s %s'", "cicuit has been permanently shutdown", "'")
}
}
// Demonstrates use of init of the circuit breaker
func ExampleBreaker_newBreaker() {
fmt.Println("Testing Test_is_ok")
b := New("name", time.Second, 10)
// Initializes the circuit
// Shuts down the circuit completely
b.Shutdown()
}
// Demonstrates a simple use of the circuit breaker, with an override of the HealthCheckInterval
func ExampleBreaker_Execute_simple() {
b := New("name", 1000*time.Millisecond, 3)
// Override the HealthCheckInterval
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
commands := &wrapperE1{"Task1"}
<-b.Execute(commands)
//<-errch
}()
wg.Wait()
b.Shutdown()
// Output: Executing Task1
}
// Demonstrates a simple use of the circuit breaker, client overrides timeout at the breaker level
func ExampleBreaker_Execute_custom_timeut() {
b := New("name", 10*time.Millisecond, 3)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
commands := &wrapperE2{"Task2"}
errch := b.Execute(commands)
err := <-errch
fmt.Println(err)
fmt.Println(err.Timeout())
}()
wg.Wait()
b.Shutdown()
// Output: task timed out
// true
}
// Demonstrates a simple use of the circuit breaker, multiple clients
func ExampleBreaker_Execute_custoom() {
fmt.Println("running ExampleBreaker_Execute_multiple_clients")
b := New("name", 10*time.Millisecond, 4)
var wg sync.WaitGroup
wg.Add(5)
for i := 0; i < 5; i++ {
go func(j int) {
defer wg.Done()
commands := &wrapperE3{state: "Task" + strconv.Itoa(j), done: false}
errch := b.Execute(commands)
err := <-errch
fmt.Println("2 ", err)
fmt.Println(err.Timeout())
}(i)
}
time.Sleep(1 * time.Second)
wg.Wait()
b.Shutdown()
}
func TestBreaker(t *testing.T) {
f, err := os.OpenFile("testlogrus.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
fmt.Printf("error opening file: %v", err)
}
defer f.Close()
fmt.Println("Running Test_execute_exceed_limit_wait_till_circuit_ok demo....")
//b := &breaker.Breaker{}
b := New("name", 1010*time.Millisecond, 5)
b.HealthCheckInterval = 1000
w1 := &wrapper2{"Service 1", false}
b.Execute(w1)
w2 := &wrapper2{"Service 2", false}
b.Execute(w2)
w3 := &wrapper2{"Service 3", false}
b.Execute(w3)
w4 := &wrapper2{"Service 4", false}
b.Execute(w4)
w5 := &wrapper2{"Service 5", false}
b.Execute(w5)
time.Sleep(2020 * time.Millisecond)
b.Shutdown()
}
func collectErr(chan error) {
}
type wrapper21 struct {
name string
exec bool
}
func (w *wrapper21) Name() string {
return w.name
}
func (w *wrapper21) CommandFunc() {
time.Sleep(1000 * time.Millisecond)
fmt.Println("Success: Executing ", w.name)
w.exec = true
}
func (w *wrapper21) DefaultFunc() {
if !w.exec {
fmt.Println("Failure: Defaulting ", w.name)
}
}
func (w *wrapper21) CleanupFunc() {
if !w.exec {
fmt.Println("Failure: Cleaning ", w.name)
}
}