forked from olebedev/emitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_test.go
67 lines (53 loc) · 1.04 KB
/
group_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
package emitter
import "testing"
func TestGroupInternals(t *testing.T) {
g := &Group{}
g.init()
expect(t, g.isInit, true)
g.stopIfListen()
expect(t, len(g.stop), 0)
g.listen()
expect(t, g.isListen, true)
g.stopIfListen()
}
func TestGroupBasic(t *testing.T) {
g := &Group{Cap: 5}
e := new(Emitter)
e2 := new(Emitter)
e3 := new(Emitter)
e.Use("*", Sync)
e2.Use("*", Sync)
e3.Use("*", Sync)
g.Add(
e.On("*"),
e2.On("*"),
e3.On("*"),
)
pipe := g.On()
expect(t, len(pipe), 0)
<-e.Emit("*", 1)
<-e.Emit("*", 2)
<-e2.Emit("*", 3)
<-e3.Emit("*", 4)
<-e3.Emit("*", 5)
// departure/arrival order
expect(t, (<-pipe).Int(0), 1)
expect(t, (<-pipe).Int(0), 2)
expect(t, (<-pipe).Int(0), 3)
expect(t, (<-pipe).Int(0), 4)
expect(t, (<-pipe).Int(0), 5)
g.Off(pipe)
_, ok := <-pipe
expect(t, ok, false)
}
func TestGroupFlushOnOff(t *testing.T) {
g := &Group{}
g.On()
expect(t, len(g.listeners), 1)
g.Flush()
expect(t, len(g.listeners), 0)
g.On()
expect(t, len(g.listeners), 1)
g.Off()
expect(t, len(g.listeners), 0)
}