-
Notifications
You must be signed in to change notification settings - Fork 1
/
event_bus_test.go
156 lines (119 loc) · 2.78 KB
/
event_bus_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
package eventbus
import (
"sync"
"testing"
)
func TestNew(t *testing.T) {
if got := New(&Options{Concurrency: 5}); got == nil {
t.Errorf("New() returned nil")
} else if len(got.list.queues) != 5 {
t.Errorf("New() concurrency options is not applied")
}
}
func TestEventBus_removeEvent(t *testing.T) {
b := New()
const (
testEvent EventName = "test"
)
isCalled := false
b.On(testEvent, func(payload []byte) {
isCalled = true
})
b.removeEvent(testEvent)
b.Emit(testEvent, []byte{})
if isCalled {
t.Error("EventBus.removeEvent() dosen't remove event from bus")
}
}
func TestEventBus_Emit_On(t *testing.T) {
b := New()
const (
testEvent EventName = "test"
testEventVaridiacParams = "test2"
)
isCalled := false
b.On(testEvent, func(payload []byte) {
isCalled = true
})
waitCallback := b.Emit(testEvent, []byte{})
err := waitCallback()
if err != nil {
t.Errorf("waitCallback got error %v", err)
}
if !isCalled {
t.Error("EventBus.On()/.Emit() callbacks are not runned at all/ waitingCallback dosen't stop gorutine until all callbacks are called")
}
waitCallback = b.Emit(testEvent, []string{})
err = waitCallback()
if err == nil {
t.Errorf("waitCallback should return error when payload is not correct")
}
_, err = b.On(testEvent, func(payload []string) {
})
if err == nil {
t.Errorf("EventBus.On() should return error when signature dosen't match")
}
isCalled = false
b.On(testEventVaridiacParams, func(a ...int) {
isCalled = true
})
waitCallback = b.Emit(testEventVaridiacParams, 5, 6, 7)
err = waitCallback()
if err != nil {
t.Errorf("waitCallback got error %v", err)
}
if !isCalled {
t.Error("EventBus.On()/.Emit() callbacks are not runned at all when callback has variadic params/ waitingCallback dosen't stop gorutine until all callbacks are called")
}
}
func Test_callbackRunner(t *testing.T) {
if got := callbackRunner(&sync.WaitGroup{}, nil, nil, nil); got == nil {
t.Errorf("callbackRunner() return shouldn't be nil")
}
}
func Test_validateCallback(t *testing.T) {
type args struct {
task interface{}
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Accept functions",
args: args{
task: func() {},
},
},
{
name: "Don't accept struct",
args: args{
task: struct{}{},
},
wantErr: true,
},
{
name: "Don't accept base type",
args: args{
task: 0,
},
wantErr: true,
},
{
name: "Accept variadic function",
args: args{
task: func(...interface{}) {},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := validateCallback(tt.args.task)
if (err != nil) != tt.wantErr {
t.Errorf("validateCallback() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}