-
Notifications
You must be signed in to change notification settings - Fork 0
/
tripper_test.go
395 lines (356 loc) · 11.9 KB
/
tripper_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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package tripper
import (
"math/rand"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestAddMonitor(t *testing.T) {
// Test case 1: Add a new monitor successfully
// Expected output: No error
monitorOptions := CircuitOptions{
Name: "test",
Threshold: 65,
MinimumCount: 20,
IntervalInSeconds: 120,
ThresholdType: ThresholdPercentage,
}
_, err := ConfigureCircuit(monitorOptions)
assert.NoError(t, err)
// Test case 3: Add a monitor with an invalid threshold type
// Expected output: An error
monitorOptions.ThresholdType = "invalid"
monitorOptions.Name = "invalid"
_, err = ConfigureCircuit(monitorOptions)
assert.Error(t, err)
assert.EqualError(t, err, "invalid threshold type invalid")
// Test case 4: Add a monitor with an invalid threshold value for percentage type
// Expected output: An error
monitorOptions.ThresholdType = ThresholdPercentage
monitorOptions.Threshold = -1
_, err = ConfigureCircuit(monitorOptions)
assert.Error(t, err)
assert.EqualError(t, err, "invalid threshold value -1.000000 for percentage type")
// Test case 5: Add a monitor with an invalid threshold value for count type
// Expected output: An error
monitorOptions.ThresholdType = ThresholdCount
monitorOptions.Threshold = 0
_, err = ConfigureCircuit(monitorOptions)
assert.Error(t, err)
assert.EqualError(t, err, "invalid threshold value 0.000000 for count type")
// Test case 6: Add a monitor with an invalid minimum count
// Expected output: An error
monitorOptions.ThresholdType = ThresholdPercentage
monitorOptions.Threshold = 65
monitorOptions.MinimumCount = 0
_, err = ConfigureCircuit(monitorOptions)
assert.Error(t, err)
assert.EqualError(t, err, "invalid minimum count 0")
// Test case 7: Add a monitor with an invalid interval
// Expected output: An error
monitorOptions.MinimumCount = 20
monitorOptions.IntervalInSeconds = 2
_, err = ConfigureCircuit(monitorOptions)
assert.Error(t, err)
assert.EqualError(t, err, "invalid interval 2")
// Test case 8: Add a monitor with an interval that is not a multiple of 60
// Expected output: An error
// Test case 9: Add a monitor with a valid threshold type and threshold value
// Expected output: No error
monitorOptions.IntervalInSeconds = 120
monitorOptions.Name = "test2"
monitorOptions.ThresholdType = ThresholdCount
monitorOptions.Threshold = 10
_, err = ConfigureCircuit(monitorOptions)
assert.NoError(t, err)
// Test case 10: Add a monitor with a threshold type count and minimum value less than threshold
// Expected output: An error
monitorOptions.MinimumCount = 5
monitorOptions.Threshold = 6
monitorOptions.Name = "test10"
_, err = ConfigureCircuit(monitorOptions)
assert.Error(t, err)
assert.EqualError(t, err, "minimum count should be greater than threshold")
}
func TestUpdateStatus(t *testing.T) {
// Test case 1: Update status with success=true
// Expected output: Success count incremented
callBackCalledOpen := false
callBackCalledClosed := false
monitorOptions := CircuitOptions{
Name: "test",
Threshold: 50,
MinimumCount: 4,
IntervalInSeconds: 60,
ThresholdType: ThresholdPercentage,
OnCircuitOpen: func(x CallbackEvent) {
callBackCalledOpen = true
},
OnCircuitClosed: func(x CallbackEvent) {
callBackCalledClosed = true
},
}
m, err := ConfigureCircuit(monitorOptions)
assert.NoError(t, err)
assert.NotNil(t, m)
m.UpdateStatus(true)
assert.Equal(t, int64(1), m.Data().SuccessCount)
assert.Equal(t, int64(0), m.Data().FailureCount)
// Test case 2: Update status with success=false
// Expected output: Failure count incremented
m.UpdateStatus(false)
assert.Equal(t, int64(1), m.Data().SuccessCount)
assert.Equal(t, int64(1), m.Data().FailureCount)
// Test case 3: Update status with success=true and minimum count not reached
// Expected output: Success count incremented, circuit not opened
m.UpdateStatus(true)
assert.Equal(t, int64(2), m.Data().SuccessCount)
assert.Equal(t, int64(1), m.Data().FailureCount)
assert.False(t, m.Data().IsCircuitOpen)
// Test case 4: Update status with success=false and minimum count not reached
// Expected output: Failure count incremented, circuit opened
m.UpdateStatus(false)
assert.Equal(t, int64(2), m.Data().SuccessCount)
assert.Equal(t, int64(2), m.Data().FailureCount)
assert.True(t, m.Data().IsCircuitOpen)
assert.True(t, callBackCalledOpen)
assert.NotZero(t, m.Data().CircuitOpenedSince)
// Test case 5: Update status with success=false and failure count greater than threshold (ThresholdType=ThresholdCount)
// Expected output: Failure count incremented, circuit opened
m.UpdateStatus(false)
assert.Equal(t, int64(2), m.Data().SuccessCount)
assert.Equal(t, int64(3), m.Data().FailureCount)
assert.True(t, m.Data().IsCircuitOpen)
assert.NotZero(t, m.Data().CircuitOpenedSince)
m.UpdateStatus(true)
m.UpdateStatus(true)
m.UpdateStatus(true)
m.UpdateStatus(true)
m.UpdateStatus(true)
assert.False(t, m.Data().IsCircuitOpen)
assert.True(t, callBackCalledClosed)
// Test case 6: Update status with success=false and failure percentage greater than threshold (ThresholdType=ThresholdPercentage)
// Expected output: Failure count incremented, circuit opened
monitorOptions.ThresholdType = ThresholdCount
monitorOptions.Threshold = 2
monitorOptions.MinimumCount = 3
m, err = ConfigureCircuit(monitorOptions)
assert.NoError(t, err)
m.UpdateStatus(false)
assert.Equal(t, int64(0), m.Data().SuccessCount)
assert.Equal(t, int64(1), m.Data().FailureCount)
assert.False(t, m.Data().IsCircuitOpen)
m.UpdateStatus(false)
m.UpdateStatus(false)
m.UpdateStatus(true)
assert.Equal(t, int64(1), m.Data().SuccessCount)
assert.Equal(t, int64(3), m.Data().FailureCount)
assert.True(t, m.Data().IsCircuitOpen)
assert.NotZero(t, m.Data().CircuitOpenedSince)
monitorOptions.Threshold = 20
monitorOptions.MinimumCount = 21
m, err = ConfigureCircuit(monitorOptions)
assert.NoError(t, err)
m.UpdateStatus(true)
assert.False(t, m.Data().IsCircuitOpen)
assert.True(t, callBackCalledClosed)
}
// func TestInterval(t *testing.T) {
// callBackCalledClosed := false
// monitorOptionsX := CircuitOptions{
// Name: generateRandomString(10),
// Threshold: 2,
// MinimumCount: 4,
// IntervalInSeconds: 5,
// ThresholdType: ThresholdCount,
// OnCircuitClosed: func(x CallbackEvent) {
// callBackCalledClosed = true
// },
// }
// mx, err := ConfigureCircuit(monitorOptionsX)
// assert.NoError(t, err)
// // Simulate failures and a success
// mx.UpdateStatus(false)
// mx.UpdateStatus(false)
// mx.UpdateStatus(true)
// mx.UpdateStatus(false)
// // Assert initial state
// assert.Equal(t, int64(1), mx.Data().SuccessCount)
// assert.Equal(t, int64(3), mx.Data().FailureCount)
// assert.True(t, mx.Data().IsCircuitOpen)
// assert.NotZero(t, mx.Data().CircuitOpenedSince)
// futureTime := time.Now().Unix() + int64(monitorOptionsX.IntervalInSeconds+1)
// for time.Now().Unix() < futureTime {
// // Simulate internal logic of the monitor on interval tick
// //mx.UpdateStatus(true) // Simulate any internal housekeeping
// }
// // Assert circuit closed state
// assert.Equal(t, int64(0), mx.Data().SuccessCount)
// assert.Equal(t, int64(0), mx.Data().FailureCount)
// assert.False(t, mx.Data().IsCircuitOpen)
// assert.True(t, callBackCalledClosed)
// assert.Zero(t, mx.Data().CircuitOpenedSince)
// }
func generateRandomString(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func TestUpdateStatusRaceSingle(t *testing.T) {
monitorOptionsX := CircuitOptions{
Name: generateRandomString(10),
Threshold: 2,
MinimumCount: 4,
IntervalInSeconds: 60,
ThresholdType: ThresholdCount,
}
mx, err := ConfigureCircuit(monitorOptionsX)
assert.NoError(t, err)
numGoroutines := 100
var wg sync.WaitGroup
wg.Add(numGoroutines)
for i := 0; i < numGoroutines; i++ {
go func() {
rand.Seed(time.Now().UnixNano())
randId := rand.Intn(100-1) + 1
mx.UpdateStatus(randId%2 == 0)
// Decrement the wait group counter
wg.Done()
}()
}
wg.Wait()
assert.Equal(t, int64(100), mx.Data().SuccessCount+mx.Data().FailureCount)
}
func TestUpdateStatusRaceTwo(t *testing.T) {
monitorOptionsX := CircuitOptions{
Name: "x",
Threshold: 2,
MinimumCount: 4,
IntervalInSeconds: 60,
ThresholdType: ThresholdCount,
}
mx, err := ConfigureCircuit(monitorOptionsX)
assert.NoError(t, err)
monitorOptionsX1 := CircuitOptions{
Name: "x1",
Threshold: 2,
MinimumCount: 4,
IntervalInSeconds: 60,
ThresholdType: ThresholdCount,
}
mx1, errx1 := ConfigureCircuit(monitorOptionsX1)
assert.NoError(t, errx1)
numGoroutines := 100
var wg sync.WaitGroup
wg.Add(numGoroutines)
for i := 0; i < numGoroutines; i++ {
go func() {
rand.Seed(time.Now().UnixNano())
randId := rand.Intn(100-1) + 1
mx.UpdateStatus(randId%2 == 0)
mx1.UpdateStatus(randId%2 == 0)
wg.Done()
}()
}
wg.Wait()
assert.Equal(t, int64(200), mx.Data().SuccessCount+mx.Data().FailureCount+mx1.Data().SuccessCount+mx1.Data().FailureCount)
}
// func TestGetMonitor(t *testing.T) {
// tripperOpts := TripperOptions{}
// tripper := Configure(tripperOpts)
// // Test case 1: Get an existing monitor
// // Expected output: Monitor and no error
// monitorOptions := CircuitOptions{
// Name: "test",
// Threshold: 65,
// MinimumCount: 20,
// IntervalInSeconds: 120,
// ThresholdType: ThresholdPercentage,
// }
// _, err := tripper.AddMonitor(monitorOptions)
// assert.NoError(t, err)
// m, err := tripper.GetMonitor("test")
// assert.NoError(t, err)
// assert.NotNil(t, m)
// // Test case 2: Get a non-existing monitor
// // Expected output: Error
// _, err = tripper.GetMonitor("non-existing")
// assert.Error(t, err)
// assert.EqualError(t, err, "Monitor with name non-existing does not exist")
// }
// func TestIsCircuitOpen(t *testing.T) {
// // Test case 1: Circuit is closed
// // Expected output: false
// monitorOptions := CircuitOptions{
// Name: "test",
// Threshold: 65,
// MinimumCount: 20,
// IntervalInSeconds: 120,
// ThresholdType: ThresholdPercentage,
// }
// tripperOpts := TripperOptions{}
// tripper := Configure(tripperOpts)
// m, err := tripper.AddMonitor(monitorOptions)
// assert.NoError(t, err)
// assert.NotNil(t, m)
// result := m.IsCircuitOpen()
// assert.False(t, result)
// }
func TestIsCircuitOpen(t *testing.T) {
// Test case 1: Circuit is open
// Expected output: true
monitorOptions := CircuitOptions{
Name: "test",
Threshold: 50,
MinimumCount: 2,
IntervalInSeconds: 120,
ThresholdType: ThresholdPercentage,
}
m, err := ConfigureCircuit(monitorOptions)
assert.NoError(t, err)
m.UpdateStatus(false)
m.UpdateStatus(false)
m.UpdateStatus(false)
assert.True(t, m.IsCircuitOpen())
// Test case 2: Circuit is closed
// Expected output: false
m.UpdateStatus(true)
m.UpdateStatus(true)
m.UpdateStatus(true)
m.UpdateStatus(true)
m.UpdateStatus(true)
m.UpdateStatus(true)
m.UpdateStatus(true)
assert.False(t, m.IsCircuitOpen())
}
func TestUpdateStatusConsecutive(t *testing.T) {
monitorOptions := CircuitOptions{
Name: "TEST_ThresholdConsecutive",
Threshold: 5,
MinimumCount: 2,
IntervalInSeconds: 120,
ThresholdType: ThresholdConsecutive,
}
m, err := ConfigureCircuit(monitorOptions)
assert.NoError(t, err)
m.UpdateStatus(false)
m.UpdateStatus(false)
m.UpdateStatus(false)
m.UpdateStatus(false)
m.UpdateStatus(false)
assert.True(t, m.IsCircuitOpen())
m.UpdateStatus(false)
m.UpdateStatus(false)
m.UpdateStatus(true)
assert.False(t, m.IsCircuitOpen())
m.UpdateStatus(false)
m.UpdateStatus(false)
m.UpdateStatus(false)
m.UpdateStatus(false)
m.UpdateStatus(false)
assert.True(t, m.IsCircuitOpen())
}