-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_test.go
233 lines (222 loc) · 4.69 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package gsignal
import (
"testing"
)
type eventConnection struct {
disposed bool
}
func (c *eventConnection) IsDisposed() bool { return c.disposed }
func TestEvent(t *testing.T) {
var e Event[int]
counter := 0
e.Connect(nil, func(arg int) {
counter += arg
})
e.Connect(nil, func(arg int) {
counter++
})
e.Emit(10)
if counter != 11 {
t.Fatal("unexpected counter value")
}
e.Emit(1)
if counter != 13 {
t.Fatal("unexpected counter value")
}
e.Emit(0)
if counter != 14 {
t.Fatal("unexpected counter value")
}
}
func TestEventDisposed(t *testing.T) {
var e Event[int]
counter := 0
conn := eventConnection{disposed: true}
e.Connect(&conn, func(arg int) {
counter += arg
})
if len(e.handlers) != 1 {
t.Fatal("unexpected number of handlers")
}
e.Emit(1)
e.Emit(1)
if counter != 0 {
t.Fatal("unexpected counter value")
}
if len(e.handlers) != 0 {
t.Fatal("unexpected number of handlers")
}
}
func TestEventDisposed2(t *testing.T) {
var e Event[Void]
var connections [3]eventConnection
var counters [3]int
for i := range connections {
id := i
e.Connect(&connections[id], func(Void) {
counters[id]++
})
}
if len(e.handlers) != 3 {
t.Fatal("unexpected number of handlers")
}
e.Emit(Void{})
for _, v := range counters {
if v != 1 {
t.Fatal("unexpected counter value")
}
}
connections[2].disposed = true
e.Emit(Void{})
if len(e.handlers) != 2 {
t.Fatal("unexpected number of handlers")
}
if counters[2] != 1 {
t.Fatal("unexpected counter value")
}
if counters[0] != 2 || counters[1] != 2 {
t.Fatal("unexpected counter value")
}
connections[0].disposed = true
e.Emit(Void{})
if len(e.handlers) != 1 {
t.Fatal("unexpected number of handlers")
}
if counters[2] != 1 || counters[0] != 2 {
t.Fatal("unexpected counter value")
}
if counters[1] != 3 {
t.Fatal("unexpected counter value")
}
e.Emit(Void{})
if counters[1] != 4 {
t.Fatal("unexpected counter value")
}
connections[1].disposed = true
e.Emit(Void{})
if len(e.handlers) != 0 {
t.Fatal("unexpected number of handlers")
}
if counters[1] != 4 {
t.Fatal("unexpected counter value")
}
}
func TestEventDisposed3(t *testing.T) {
var e Event[Void]
var connections [3]eventConnection
var counters [3]int
for i := range connections {
id := i
e.Connect(&connections[id], func(Void) {
counters[id]++
})
}
connections[0].disposed = true
connections[2].disposed = true
e.Emit(Void{})
e.Emit(Void{})
e.Emit(Void{})
if len(e.handlers) != 1 {
t.Fatal("unexpected number of handlers")
}
if counters[0] != 0 || counters[2] != 0 {
t.Fatal("unexpected counter value")
}
if counters[1] != 3 {
t.Fatal("unexpected counter value")
}
connections[1].disposed = true
for i := 0; i < 2; i++ {
e.Emit(Void{})
if len(e.handlers) != 0 {
t.Fatal("unexpected number of handlers")
}
if counters[0] != 0 || counters[1] != 3 || counters[2] != 0 {
t.Fatal("unexpected counter value")
}
}
}
func TestDisconnect(t *testing.T) {
var e Event[int]
var conn1 eventConnection
var conn2 eventConnection
counter := 0
e.Connect(&conn1, func(arg int) {
counter += arg
})
e.Connect(&conn2, func(arg int) {
counter -= arg
})
e.Emit(10)
e.Emit(5)
if counter != 0 {
t.Fatal("unexpected counter value")
}
if len(e.handlers) != 2 {
t.Fatal("unexpected number of handlers")
}
e.Disconnect(&conn1)
// The disconnection is delayed until the emit, so the number
// of handlers will remain the same.
if len(e.handlers) != 2 {
t.Fatal("unexpected number of handlers")
}
e.Emit(5)
if counter != -5 {
t.Fatal("unexpected counter value")
}
if len(e.handlers) != 1 {
t.Fatal("unexpected number of handlers")
}
e.Emit(1)
if counter != -6 {
t.Fatal("unexpected counter value")
}
conn2.disposed = true
e.Emit(1)
if counter != -6 {
t.Fatal("unexpected counter value")
}
if len(e.handlers) != 0 {
t.Fatal("unexpected number of handlers")
}
}
func BenchmarkEmitVoid(b *testing.B) {
var e0 Event[Void]
var e1 Event[Void]
e1.Connect(nil, func(Void) {})
var e2 Event[Void]
e2.Connect(nil, func(Void) {})
e2.Connect(nil, func(Void) {})
b.Run("Count0", func(b *testing.B) {
for i := 0; i < b.N; i++ {
e0.Emit(Void{})
}
})
b.Run("Count1", func(b *testing.B) {
for i := 0; i < b.N; i++ {
e1.Emit(Void{})
}
})
b.Run("Count2", func(b *testing.B) {
for i := 0; i < b.N; i++ {
e2.Emit(Void{})
}
})
}
func BenchmarkConnectEmitDisconnectEmit(b *testing.B) {
var e Event[Void]
conn1 := &eventConnection{}
conn2 := &eventConnection{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
conn1.disposed = false
conn2.disposed = false
e.Connect(conn1, func(Void) {})
e.Connect(conn2, func(Void) {})
e.Emit(Void{})
e.Disconnect(conn1)
e.Disconnect(conn2)
e.Emit(Void{})
}
}