-
Notifications
You must be signed in to change notification settings - Fork 307
/
async-helper.go
120 lines (105 loc) · 3.44 KB
/
async-helper.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
package testutils
import (
"fmt"
"sync"
"time"
"github.com/golang/mock/gomock"
"github.com/onsi/ginkgo/v2"
"github.com/rudderlabs/rudder-server/utils/misc"
)
// AsyncTestHelper provides synchronization methods to test goroutines.
// Example:
//
// var (
// asyncHelper testutils.AsyncTestHelper
// )
//
// BeforeEach(func() {
// mockMyInterface.EXPECT().MyMethodInGoroutine().Do(asyncHelper.ExpectAndNotifyCallback())
// })
//
// AfterEach(func() {
// asyncHelper.WaitWithTimeout(time.Second)
// })
type AsyncTestHelper struct {
wg sync.WaitGroup
waitingMap map[string]int
waitingMapLock sync.RWMutex
}
// ExpectAndNotifyCallback Adds one to this helper's WaitGroup, and provides a callback that calls Done on it.
// Should be used for gomock Do calls that trigger via mocked functions executed in a goroutine.
func (helper *AsyncTestHelper) ExpectAndNotifyCallback() func(...interface{}) {
helper.wg.Add(1)
return func(...interface{}) {
helper.wg.Done()
}
}
// ExpectAndNotifyCallback Adds one to this helper's WaitGroup, and provides a callback that calls Done on it.
// Should be used for gomock Do calls that trigger via mocked functions executed in a goroutine.
func (helper *AsyncTestHelper) ExpectAndNotifyCallbackWithName(name string) func(...interface{}) {
helper.waitingMapLock.Lock()
defer helper.waitingMapLock.Unlock()
if _, ok := helper.waitingMap[name]; !ok {
helper.waitingMap[name] = 0
}
helper.waitingMap[name]++
helper.wg.Add(1)
return func(...interface{}) {
helper.waitingMapLock.Lock()
defer helper.waitingMapLock.Unlock()
helper.wg.Done()
helper.waitingMap[name]--
}
}
// ExpectAndNotifyCallback Adds one to this helper's WaitGroup, and provides a callback that calls Done on it.
// Should be used for gomock Do calls that trigger via mocked functions executed in a goroutine.
func (helper *AsyncTestHelper) ExpectAndNotifyCallbackWithNameOnce(name string) func() {
var s sync.Once
f := helper.ExpectAndNotifyCallbackWithName(name)
return func() {
s.Do(func() {
f()
})
}
}
// WaitWithTimeout waits for this helper's WaitGroup until provided timeout.
// Should wait for all ExpectAndNotifyCallback callbacks, registered in asynchronous mocks calls
func (helper *AsyncTestHelper) WaitWithTimeout(d time.Duration) {
helper.RunTestWithTimeout(func() {
helper.wg.Wait()
}, d)
}
// RegisterCalls registers a number of calls to this async helper, so that they are waited for.
func (helper *AsyncTestHelper) RegisterCalls(calls ...*gomock.Call) {
for _, call := range calls {
call.Do(helper.ExpectAndNotifyCallback())
}
}
// RunTestWithTimeout runs function f until provided timeout.
// If the function times out, it will cause the ginkgo test to fail.
func (helper *AsyncTestHelper) RunTestWithTimeout(f func(), d time.Duration) {
misc.RunWithTimeout(func() {
defer ginkgo.GinkgoRecover()
f()
}, func() {
helper.waitingMapLock.RLock()
defer helper.waitingMapLock.RUnlock()
for k, v := range helper.waitingMap {
fmt.Println(k, "", v)
}
ginkgo.Fail("Async helper's wait group timed out")
}, d)
}
// RunTestWithTimeout runs function f until provided timeout.
// If the function times out, it will cause the ginkgo test to fail.
func RunTestWithTimeout(f func(), d time.Duration) {
misc.RunWithTimeout(func() {
defer ginkgo.GinkgoRecover()
f()
}, func() {
ginkgo.Fail("Async helper's wait group timed out")
}, d)
}
func (helper *AsyncTestHelper) Setup() {
helper.waitingMap = make(map[string]int)
}