-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_test.go
145 lines (135 loc) · 3.09 KB
/
event_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
package goevent
import (
"strconv"
"sync"
"testing"
"time"
)
func TestEvent_Name(t *testing.T) {
names := []string{
"name",
"$$$$",
"سلام",
"______",
" ",
"a b",
" AAA",
"AAA ",
}
for _, name := range names {
t.Run(name, func(t *testing.T) {
e := Event{name: name}
got := e.Name()
if got != name {
t.Errorf("Expected event name %s, got %s", name, got)
}
})
}
}
func TestEvent_AddASyncListener(t *testing.T) {
listener := func(payload Payload) {}
for i:=0; i<10; i++ {
t.Run(strconv.Itoa(i)+" listeners", func(t *testing.T){
e := Event{}
for j:=0; j<i;j++ {
e.AddASyncListener(listener)
}
listenerCount := len(e.aSyncListeners)
if listenerCount != i {
t.Errorf("Expected %d async listeners, got %d", i, len(e.aSyncListeners))
}
})
}
}
func TestEvent_AddSyncListener(t *testing.T) {
listener := func(payload Payload) {}
for i:=0; i<10; i++ {
t.Run(strconv.Itoa(i)+" listeners", func(t *testing.T){
e := Event{}
for j:=0; j<i;j++ {
e.AddSyncListener(listener)
}
listenerCount := len(e.syncListeners)
if listenerCount != i {
t.Errorf("Expected %d sync listeners, got %d", i, len(e.syncListeners))
}
})
}
}
func TestEvent_ASyncListeners(t *testing.T) {
listener := func(payload Payload){}
for i:=0; i<10; i++ {
t.Run(strconv.Itoa(i)+" listeners", func(t *testing.T){
listeners := []Listener{}
for j:=0; j<i; j++ {
listeners = append(listeners, listener)
}
e := Event{}
e.aSyncListeners = listeners
if len(e.ASyncListeners()) != i {
t.Errorf("Expected %d async listeners, got %d", i, len(e.ASyncListeners()))
}
})
}
}
func TestEvent_SyncListeners(t *testing.T) {
listener := func(payload Payload){}
for i:=0; i<10; i++ {
t.Run(strconv.Itoa(i)+" listeners", func(t *testing.T){
listeners := []Listener{}
for j:=0; j<i; j++ {
listeners = append(listeners, listener)
}
e := Event{}
e.syncListeners = listeners
if len(e.SyncListeners()) != i {
t.Errorf("Expected %d async listeners, got %d", i, len(e.SyncListeners()))
}
})
}
}
func TestEvent_Dispatch(t *testing.T) {
t.Run("async", func (t *testing.T) {
testPayload := PayloadASyncTest{}
listener := func (payload Payload) {
testPayload.Inc()
}
eventName := "test"
n := 25
for i:=0; i<n; i++ {
ByName(eventName).AddASyncListener(listener)
}
ByName(eventName).Dispatch(CreatePayload())
time.Sleep(time.Second*2)
if testPayload.val != n {
t.Errorf("Expected val to become %d, got %d", n, testPayload.val)
}
})
t.Run("sync", func (t *testing.T) {
testPayload := PayloadSyncTest{}
listener := func (payload Payload) {
testPayload.val++
}
eventName := "test"
n := 25
for i:=0; i<n; i++ {
ByName(eventName).AddSyncListener(listener)
}
ByName(eventName).Dispatch(CreatePayload())
if testPayload.val != n {
t.Errorf("Expected val to become %d, got %d", n, testPayload.val)
}
})
}
type PayloadASyncTest struct {
mux sync.Mutex
val int
}
type PayloadSyncTest struct {
val int
}
func (p *PayloadASyncTest) Inc() {
p.mux.Lock()
p.val = p.val+1
p.mux.Unlock()
}