-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathevent_emitter_test.go
72 lines (55 loc) · 1.36 KB
/
event_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
package mediasoup
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEventEmitter(t *testing.T) {
emitter := NewEventEmitter()
called := 0
callback1 := func(i int) {
called++
}
// test "On" and "Emit"
emitter.On("event1", callback1)
emitter.Emit("event1", 1)
emitter.Emit("event1", 1, 2) // more arguments than expected
emitter.Emit("event1") // ignore argument
assert.Equal(t, 3, called)
called = 0
// test "Off"
emitter.Off("event1", callback1)
emitter.Emit("event1")
assert.Equal(t, 0, called)
// test "Once"
emitter.Once("event1", callback1)
emitter.Emit("event1")
emitter.Emit("event1")
assert.Equal(t, 1, called)
called = 0
type Struct struct {
A int
}
val := Struct{A: 1}
emitter.On("event2", func(s *Struct) {
called += s.A
})
emitter.On("event2", func(s Struct) {
called += s.A
})
emitter.On("event2", func(s Struct) {
called += s.A
panic("ignored panic")
})
// emit struct, failed on pointer argument
emitter.SafeEmit("event2", val)
data, _ := json.Marshal(val)
// emit json data, success on pointer argument
emitter.SafeEmit("event2", data)
assert.Equal(t, 2*3-1, called)
emitter.RemoveAllListeners("event2")
assert.Equal(t, 0, emitter.ListenerCount("event2"))
emitter.On("event2", func() {})
emitter.RemoveAllListeners()
assert.Equal(t, 0, emitter.ListenerCount())
}