-
Notifications
You must be signed in to change notification settings - Fork 42
/
mock_test.go
168 lines (134 loc) · 2.88 KB
/
mock_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
package mediasoup
import (
"reflect"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type MockFunc struct {
require *require.Assertions
notifyChan chan []interface{}
results [][]interface{}
timeout time.Duration
}
func NewMockFunc(t *testing.T) *MockFunc {
return &MockFunc{
require: require.New(t),
notifyChan: make(chan []interface{}, 100),
timeout: 50 * time.Millisecond,
}
}
func (w *MockFunc) WithTimeout(timeout time.Duration) *MockFunc {
w.timeout = timeout
return w
}
func (w *MockFunc) Fn() func(...interface{}) {
w.Reset()
return func(args ...interface{}) {
w.notifyChan <- args
}
}
func (w *MockFunc) ExpectCalledWith(args ...interface{}) {
w.wait()
if len(w.results) == 0 {
w.require.FailNow("fn is not called")
return
}
last := w.results[len(w.results)-1]
if len(args) != len(last) {
w.require.FailNow("fn is called, but the number of arguments is not the same")
return
}
for i, arg := range args {
w.require.EqualValues(arg, last[i])
}
}
func (w *MockFunc) ExpectCalled(msgAndArgs ...interface{}) {
w.require.NotZero(w.CalledTimes(), msgAndArgs...)
}
func (w *MockFunc) ExpectCalledTimes(called int, msgAndArgs ...interface{}) {
w.require.Equal(called, w.CalledTimes(), msgAndArgs...)
}
func (w *MockFunc) CalledTimes() int {
w.wait()
return len(w.results)
}
func (w *MockFunc) Reset() {
w.notifyChan = make(chan []interface{}, 100)
w.results = nil
}
func (w *MockFunc) wait() {
// results are already collected
if len(w.results) > 0 {
return
}
// collect results within timeout
timer := time.NewTimer(w.timeout)
defer timer.Stop()
for {
select {
case result := <-w.notifyChan:
w.results = append(w.results, result)
case <-timer.C:
return
}
}
}
type wrapOption func(*asyncResult)
func withWaitTimeout(d time.Duration) wrapOption {
return func(w *asyncResult) {
w.waitTimeout = d
}
}
func asyncRun(fn interface{}, options ...wrapOption) *asyncResult {
w := &asyncResult{
doneCh: make(chan []reflect.Value, 1),
waitTimeout: 10 * time.Millisecond,
}
for _, o := range options {
o(w)
}
go func() {
fnVal := reflect.ValueOf(fn)
w.doneCh <- fnVal.Call(nil)
}()
return w
}
type asyncResult struct {
done bool
doneCh chan []reflect.Value
outs []interface{}
waitTimeout time.Duration
waited bool
}
func (w *asyncResult) Finished() bool {
w.mayWait()
return w.done
}
func (w *asyncResult) Out(i int) interface{} {
w.mayWait()
if len(w.outs)-1 < i {
return nil
}
return w.outs[i]
}
func (w *asyncResult) mayWait() {
if w.waited {
return
}
var waitCh <-chan time.Time
if w.waitTimeout > 0 {
waitTimer := time.NewTimer(w.waitTimeout)
waitCh = waitTimer.C
defer waitTimer.Stop()
}
select {
case outs := <-w.doneCh:
w.done = true
for _, out := range outs {
w.outs = append(w.outs, out.Interface())
}
case <-waitCh:
}
w.waited = true
}