forked from intuit/funnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
funnel_test.go
317 lines (253 loc) · 8.96 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
package funnel
import (
"errors"
"github.com/stretchr/testify/assert"
"math/rand"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
)
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 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)
}