-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemitter_test.go
157 lines (128 loc) · 3.16 KB
/
emitter_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
package eventemitter
import (
"sync"
"sync/atomic"
"testing"
"time"
)
func TestEmitterSynced(t *testing.T) {
testEmitter(t, false)
}
func TestEmitterAsync(t *testing.T) {
testEmitter(t, true)
}
func testEmitter(t *testing.T, async bool) {
var em EventEmitter
var ob Observable
e := NewEmitter(async)
em = e
ob = e
var ASingle, AListener, capture int32
listener := ob.AddListener("test event A", func(args ...interface{}) {
verifyArgs(t, args)
atomic.AddInt32(&AListener, 1)
})
ob.ListenOnce("test event A", func(args ...interface{}) {
verifyArgs(t, args)
atomic.AddInt32(&ASingle, 1)
})
capturer := ob.AddCapturer(func(event EventType, args ...interface{}) {
verifyArgs(t, args)
atomic.AddInt32(&capture, 1)
})
em.EmitEvent("test event A", "test", 123, true)
em.EmitEvent("test event B", "test", 123, true)
em.EmitEvent("test event C", "test", 123, true)
em.EmitEvent("test event A", "test", 123, true)
em.EmitEvent("test event A", "test", 123, true)
ob.RemoveListener("test event A", listener)
ob.RemoveCapturer(capturer)
em.EmitEvent("Testing 123", 1)
em.EmitEvent("test event A", 1)
em.EmitEvent("Wow", 2)
if async {
// Events are async, so wait a bit for them to finish
time.Sleep(time.Millisecond * 200)
}
if atomic.LoadInt32(&ASingle) != 1 {
t.Log("Single A event not triggered right", atomic.LoadInt32(&ASingle))
t.Fail()
}
if atomic.LoadInt32(&AListener) != 3 {
t.Log("A event not triggered right", atomic.LoadInt32(&AListener))
t.Fail()
}
if atomic.LoadInt32(&capture) != 5 {
t.Log("Capture all not triggered right", atomic.LoadInt32(&capture))
t.Fail()
}
}
func verifyArgs(t *testing.T, args []interface{}) {
if len(args) != 3 {
t.Logf("Too few arguments (%d) %#v", len(args), args)
t.Fail()
return
}
s, ok := args[0].(string)
if !ok || s != "test" {
t.Log("Wrong argument for 1:test!")
t.Fail()
}
i, ok := args[1].(int)
if !ok || i != 123 {
t.Log("Wrong argument for 2:123!")
t.Fail()
}
b, ok := args[2].(bool)
if !ok || b != true {
t.Log("Wrong argument for 3:true!")
t.Fail()
}
}
func TestEmitNonAsyncRecursive(t *testing.T) {
e := NewEmitter(false)
var rootFired int
e.AddListener("rootevent", func(args ...interface{}) {
rootFired++
e.EmitEvent("subevent", 1, 2, 3)
e.EmitEvent("subevent", 1, 2, 3)
})
var subFired int
e.AddListener("subevent", func(args ...interface{}) {
if len(args) != 3 {
t.Logf("Too few arguments (%d) %#v", len(args), args)
t.Fail()
return
}
subFired++
})
e.EmitEvent("rootevent", "test")
if rootFired != 1 {
t.Log("Root event all not triggered right", rootFired)
t.Fail()
}
if subFired != 2 {
t.Log("Sub event all not triggered right", subFired)
t.Fail()
}
}
func TestMultipleRoutineEmitListen(t *testing.T) {
e := NewEmitter(true)
wg := sync.WaitGroup{}
wg.Add(20)
for i := 0; i < 10; i++ {
go func() {
e.EmitEvent(EventType("blurp"), 1, 2, 3)
e.EmitEvent(EventType("worst"), 321)
wg.Done()
}()
}
for i := 0; i < 10; i++ {
go func() {
e.AddListener(EventType("blurp"), func(arguments ...interface{}) {})
e.AddCapturer(func(event EventType, arguments ...interface{}) {})
wg.Done()
}()
}
wg.Wait()
}