-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
eventer_test.go
78 lines (60 loc) · 1.49 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
76
77
78
package gobot
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEventerAddEvent(t *testing.T) {
e := NewEventer()
e.AddEvent("test")
if _, ok := e.Events()["test"]; !ok {
require.Fail(t, "Could not add event to list of Event names")
}
assert.Equal(t, "test", e.Event("test"))
assert.Equal(t, "", e.Event("unknown"))
}
func TestEventerDeleteEvent(t *testing.T) {
e := NewEventer()
e.AddEvent("test1")
e.DeleteEvent("test1")
if _, ok := e.Events()["test1"]; ok {
require.Fail(t, "Could not add delete event from list of Event names")
}
}
func TestEventerOn(t *testing.T) {
e := NewEventer()
sem := make(chan bool)
_ = e.On("test", func(data interface{}) {
sem <- true
})
// wait some time to ensure the eventer go routine is working
time.Sleep(10 * time.Millisecond)
e.Publish("test", true)
select {
case <-sem:
case <-time.After(10 * time.Millisecond):
require.Fail(t, "On was not called")
}
}
func TestEventerOnce(t *testing.T) {
e := NewEventer()
sem := make(chan bool)
_ = e.Once("test", func(data interface{}) {
sem <- true
})
// wait some time to ensure the eventer go routine is working
time.Sleep(10 * time.Millisecond)
e.Publish("test", true)
select {
case <-sem:
case <-time.After(10 * time.Millisecond):
require.Fail(t, "Once was not called")
}
e.Publish("test", true)
select {
case <-sem:
require.Fail(t, "Once was called twice")
case <-time.After(10 * time.Millisecond):
}
}