forked from jiyeyuran/mediasoup-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_test.go
86 lines (69 loc) · 1.5 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
package mediasoup
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
type MockFunc struct {
require *require.Assertions
notifyChan chan []interface{}
results [][]interface{}
}
func NewMockFunc(t *testing.T) *MockFunc {
return &MockFunc{
require: require.New(t),
notifyChan: make(chan []interface{}, 100),
}
}
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() {
w.require.NotZero(w.CalledTimes())
}
func (w *MockFunc) ExpectCalledTimes(called int) {
w.require.Equal(called, w.CalledTimes())
}
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 with 10ms timeout
timer := time.NewTimer(time.Millisecond * 10)
defer timer.Stop()
for {
select {
case result := <-w.notifyChan:
w.results = append(w.results, result)
case <-timer.C:
return
}
}
}