-
Notifications
You must be signed in to change notification settings - Fork 17
/
funnel_test.go
386 lines (320 loc) · 10.6 KB
/
funnel_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
package funnel
import (
"errors"
"math/rand"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestBasic(t *testing.T) {
fnl := New()
var ops uint64 = 0
var wg sync.WaitGroup
numOfOperations := 50
numOfGoroutines := 50
wg.Add(numOfGoroutines * numOfOperations)
for op := 0; op < numOfOperations; op++ {
opId := "operation" + strconv.Itoa(op)
for i := 0; i < numOfGoroutines; i++ {
go func(numOfGoroutine int, id string) {
defer wg.Done()
if numOfGoroutine%2 == 1 {
time.Sleep(time.Millisecond * 200)
}
res, err := fnl.Execute(id, func() (interface{}, error) {
time.Sleep(time.Millisecond * 100)
atomic.AddUint64(&ops, 1)
return id + "ended successfully", errors.New("no error")
})
if res != id+"ended successfully" || err.Error() != "no error" {
t.Error("The results of operations is not as expected")
}
}(i, opId)
}
}
wg.Wait()
opsFinal := atomic.LoadUint64(&ops)
if int(opsFinal) != numOfOperations*2 {
t.Error("Number of operations performed was higher than expected, expected ", numOfOperations*2, ", got ", opsFinal)
}
}
func TestWithCacheTtl(t *testing.T) {
fnl := New(WithCacheTtl(time.Second))
var ops uint64 = 0
var wg sync.WaitGroup
numOfOperations := 50
numOfGoroutines := 50
wg.Add(numOfGoroutines * numOfOperations)
for op := 0; op < numOfOperations; op++ {
opId := "operation" + strconv.Itoa(op)
for i := 0; i < numOfGoroutines; i++ {
go func(numOfGoroutine int, id string) {
defer wg.Done()
if numOfGoroutine%2 == 1 {
time.Sleep(time.Millisecond * 500)
}
res, err := fnl.Execute(id, func() (interface{}, error) {
time.Sleep(time.Millisecond * 100)
atomic.AddUint64(&ops, 1)
return id + "ended successfully", errors.New("no error")
})
if res != id+"ended successfully" || err.Error() != "no error" {
t.Error("The results of operations is not as expected")
}
}(i, opId)
}
}
wg.Wait()
opsFinal := atomic.LoadUint64(&ops)
if int(opsFinal) != numOfOperations {
t.Error("Number of operations performed was higher than expected, expected ", numOfOperations, ", got ", opsFinal)
}
}
func TestWithShouldCachePredicate(t *testing.T) {
myError := errors.New("something went wrong")
fnl := New(WithCacheTtl(time.Hour), WithShouldCachePredicate(func(response interface{}, err error) bool {
if response == 1 {
return true
} else if response == 2 {
return false
} else if errors.Is(err, myError) {
return false
}
return true
}))
res, err := fnl.Execute("1", func() (interface{}, error) {
return 1, nil
})
assert.Equal(t, res, 1)
assert.Equal(t, err, nil)
op := fnl.opInProcess["1"]
assert.NotNil(t, op)
res, err = fnl.Execute("2", func() (interface{}, error) {
return 2, nil
})
assert.Equal(t, res, 2)
assert.Equal(t, err, nil)
op = fnl.opInProcess["2"]
assert.Nil(t, op)
res, err = fnl.Execute("3", func() (interface{}, error) {
return nil, myError
})
assert.Equal(t, res, nil)
assert.Equal(t, err, myError)
op = fnl.opInProcess["3"]
assert.Nil(t, op)
}
func TestEndsWithPanic(t *testing.T) {
fnl := New()
var numOfGoREndWithPanic uint64 = 0
var wg sync.WaitGroup
numOfOperations := 50
numOfGoroutines := 50
wg.Add(numOfGoroutines * numOfOperations)
for op := 0; op < numOfOperations; op++ {
opId := "operation" + strconv.Itoa(op)
for i := 0; i < numOfGoroutines; i++ {
go func(numOfGoroutine int, id string) {
defer func() {
if rr := recover(); rr != nil {
if rr != "test ends with panic" {
t.Error("unexpected panic message")
}
atomic.AddUint64(&numOfGoREndWithPanic, 1)
wg.Done()
}
}()
if numOfGoroutine%2 == 1 {
time.Sleep(time.Millisecond * 500)
}
fnl.Execute(id, func() (interface{}, error) {
time.Sleep(time.Millisecond * 100)
panic("test ends with panic")
})
t.Error("Should not reach this line because panic should occur")
}(i, opId)
}
}
wg.Wait()
numOfGoREndWithPanicFinal := atomic.LoadUint64(&numOfGoREndWithPanic)
if int(numOfGoREndWithPanicFinal) != numOfOperations*numOfGoroutines {
t.Error("Number of operations that ended with panic is not as expected, expected ", numOfOperations*numOfGoroutines, ", got ", numOfGoREndWithPanicFinal)
}
}
func TestWithTimeout(t *testing.T) {
fnl := New(WithTimeout(time.Millisecond * 50))
var numOfGoREndWithTimeout uint64 = 0
var wg sync.WaitGroup
numOfOperations := 50
numOfGoroutines := 50
wg.Add(numOfGoroutines * numOfOperations)
for op := 0; op < numOfOperations; op++ {
opId := "operation" + strconv.Itoa(op)
for i := 0; i < numOfGoroutines; i++ {
go func(numOfGoroutine int, id string) {
defer wg.Done()
res, err := fnl.Execute(id, func() (interface{}, error) {
time.Sleep(time.Millisecond * 100)
return id + "ended successfully", errors.New("no error")
})
if res == nil && err == timeoutError {
atomic.AddUint64(&numOfGoREndWithTimeout, 1)
}
}(i, opId)
}
}
wg.Wait()
numOfGoREndWithTimeoutFinal := atomic.LoadUint64(&numOfGoREndWithTimeout)
if int(numOfGoREndWithTimeoutFinal) != numOfOperations*numOfGoroutines {
t.Error("Number of operations that ended with timeout expired is not as expected, expected ", numOfOperations*numOfGoroutines, ", got ", numOfGoREndWithTimeoutFinal)
}
}
/*
The following tests the ability of the funnel to create a new operation while an operation of the same id has timed out and its execution function is still running
An unexpired cacheTTL on a timedout operation should also not prohibit creating the new operation
*/
func TestWithTimedoutReruns(t *testing.T) {
fnl := New(WithTimeout(time.Millisecond*50), WithCacheTtl(time.Millisecond*100))
var numOfGoREndWithTimeout uint64 = 0
var numOfStartedOperations uint64 = 0
numOfOperations := 50
numOfGoroutines := 50
for op := 0; op < numOfOperations; op++ {
var wg sync.WaitGroup
wg.Add(numOfGoroutines) // Only when numOfGoroutines operations time out we run the next batch of numOfGoroutines operations. Each such batch is expected to run in a newly created operation request
for i := 0; i < numOfGoroutines; i++ {
go func(numOfGoroutine int, id string) {
defer wg.Done()
res, err := fnl.Execute(id, func() (interface{}, error) {
atomic.AddUint64(&numOfStartedOperations, 1)
time.Sleep(time.Millisecond * 100)
return id + "ended successfully", errors.New("no error")
})
if res == nil && err == timeoutError {
atomic.AddUint64(&numOfGoREndWithTimeout, 1)
}
}(i, "StaticOperationId")
}
wg.Wait()
}
numOfGoREndWithTimeoutFinal := atomic.LoadUint64(&numOfGoREndWithTimeout)
if int(numOfGoREndWithTimeoutFinal) != numOfOperations*numOfGoroutines {
t.Error("Number of operations that ended with timeout expired is not as expected, expected ", numOfOperations*numOfGoroutines, ", got ", numOfGoREndWithTimeoutFinal)
}
numOfStartedOperationsFinal := atomic.LoadUint64(&numOfStartedOperations)
if int(numOfStartedOperationsFinal) != numOfOperations {
t.Error("Number of operation execution starts is not as expected, expected ", numOfOperations, ", got ", numOfStartedOperations)
}
}
/*
All operation execution requests on the same operation instance should timeout at the same time. The expiry time is determined by the timeout parameter and the time of the first execution request.
*/
func TestOperationAbsoluteTimeout(t *testing.T) {
funnelTimeout := time.Duration(500 * time.Millisecond)
operationSleepTime := time.Duration(550 * time.Millisecond)
numOfOperationRequests := 10
requestDelay := time.Duration(30 * time.Millisecond)
operationId := "TestUnifiedTimeout"
fnl := New(WithTimeout(funnelTimeout))
var wg sync.WaitGroup
wg.Add(numOfOperationRequests)
start := time.Now()
for i := 0; i < numOfOperationRequests; i++ {
go func() {
defer wg.Done()
fnl.Execute(operationId, func() (interface{}, error) {
time.Sleep(operationSleepTime)
return operationId + "ended successfully", errors.New("no error")
})
}()
time.Sleep(requestDelay)
}
wg.Wait()
elapsedTimeAllRequests := time.Since(start)
expectedOperationTimeoutWithGrace := funnelTimeout + time.Duration(100*time.Millisecond)
if elapsedTimeAllRequests > expectedOperationTimeoutWithGrace {
t.Error("Expected all operation request to timeout at the same time, funnelTimeout", funnelTimeout, " Elapsed time for all operations", elapsedTimeAllRequests)
}
}
func TestOpInProgress(t *testing.T) {
fnl := New()
opId := "opId"
go func() {
fnl.Execute(opId, func() (i interface{}, err error) {
time.Sleep(time.Second * 2)
return nil, nil
})
}()
time.Sleep(time.Millisecond * 500)
if !fnl.IsOpInProgress(opId) {
t.Error("Expected op to be in progress")
}
}
var getRandomInt = func() (interface{}, error) {
time.Sleep(time.Millisecond * 50)
res := rand.Int()
return &res, nil
}
/*
Test ExecuteAndCopyResult function. Validate that ExecuteAndCopyResult returns copied object and not a shared object between the requesting callers.
*/
func TestExecuteAndCopyResult(t *testing.T) {
fnl := New()
var wg sync.WaitGroup
var num1, num2 *int
wg.Add(2)
// preform getRandomInt twice, with same operation id - both goroutines are expected to get same result.
// each goroutine stores the returned result, the results would be compared later
go func() {
defer wg.Done()
res, _ := fnl.ExecuteAndCopyResult("opId", getRandomInt)
num1 = res.(*int)
}()
go func() {
defer wg.Done()
res, _ := fnl.ExecuteAndCopyResult("opId", getRandomInt)
num2 = res.(*int)
}()
// wait until both goroutines finish
wg.Wait()
// we are expecting ExecuteAndCopyResult to preform deep copy, the returned results for same operation
// should have same values but different addresses
assert.Equal(t, *num1, *num2, "Objects' values are expected to be the same.")
assert.False(t, num1 == num2, "Objects' addresses are expected to be different. addresses received:", num1, ",", num2)
}
// Test cached value
func TestCachedValued(t *testing.T) {
opId := "opId"
f := New(WithCacheTtl(time.Hour), WithTimeout(100*time.Millisecond))
// cache value
f.Execute(opId, func() (interface{}, error) {
return nil, nil
})
numOfGoroutines := 1000
wg := sync.WaitGroup{}
failedExecute := false
for i := 0; i < numOfGoroutines; i++ {
if i%3 == 0 {
time.Sleep(100 * time.Millisecond)
}
wg.Add(1)
go func() {
_, err := f.Execute(opId, func() (interface{}, error) {
return nil, nil
})
if err != nil && errors.Is(err, timeoutError) {
failedExecute = true
}
wg.Done()
}()
// result should have been cached
if failedExecute {
t.Error("false timeout error")
break
}
}
wg.Wait()
}