-
Notifications
You must be signed in to change notification settings - Fork 4
/
eventemitter_test.go
62 lines (47 loc) · 1.02 KB
/
eventemitter_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
package eventemitter
import (
"fmt"
"testing"
)
// Struct for testing Embedding of EventEmitters
type Server struct {
EventEmitter
}
func TestEmbedding(t *testing.T) {
s := new(Server)
// Don't forget to allocate the memory when
// used as sub type.
s.EventEmitter.Init()
s.On("recv", func(msg string) string {
return msg
})
resp := <-s.Emit("recv", "Hello World")
expected := "Hello World"
if res := resp.Ret[0].(string); res != expected {
t.Errorf("Expected %s, got %s", expected, res)
}
}
func ExampleEmitReturnsEventOnChan() {
emitter := New()
emitter.On("hello", func(name string) string {
return "Hello World " + name
})
e := <-emitter.Emit("hello", "John")
fmt.Println(e.EventName)
// Output:
// hello
}
func BenchmarkEmit(b *testing.B) {
b.StopTimer()
emitter := New()
nListeners := 100
for i := 0; i < nListeners; i++ {
emitter.On("hello", func(name string) string {
return "Hello World " + name
})
}
b.StartTimer()
for i := 0; i < b.N; i++ {
<-emitter.Emit("hello", "John")
}
}