forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventer_test.go
75 lines (60 loc) · 1.19 KB
/
eventer_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
package gobot
import (
"testing"
"time"
)
func TestEventerAddEvent(t *testing.T) {
e := NewEventer()
e.AddEvent("test")
if _, ok := e.Events()["test"]; !ok {
t.Errorf("Could not add event to list of Event names")
}
}
func TestEventerDeleteEvent(t *testing.T) {
e := NewEventer()
e.AddEvent("test1")
e.DeleteEvent("test1")
if _, ok := e.Events()["test1"]; ok {
t.Errorf("Could not add delete event from list of Event names")
}
}
func TestEventerOn(t *testing.T) {
e := NewEventer()
e.AddEvent("test")
sem := make(chan bool)
e.On("test", func(data interface{}) {
sem <- true
})
go func() {
e.Publish("test", true)
}()
select {
case <-sem:
case <-time.After(10 * time.Millisecond):
t.Errorf("On was not called")
}
}
func TestEventerOnce(t *testing.T) {
e := NewEventer()
e.AddEvent("test")
sem := make(chan bool)
e.Once("test", func(data interface{}) {
sem <- true
})
go func() {
e.Publish("test", true)
}()
select {
case <-sem:
case <-time.After(10 * time.Millisecond):
t.Errorf("Once was not called")
}
go func() {
e.Publish("test", true)
}()
select {
case <-sem:
t.Errorf("Once was called twice")
case <-time.After(10 * time.Millisecond):
}
}