forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reactor_test.go
76 lines (64 loc) · 1.45 KB
/
reactor_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
// +build !integration
package disgord
import (
"context"
"sync"
"testing"
)
func Test_isHandler(t *testing.T) {
handler := make(chan *MessageCreate)
if !isHandler(handler) {
t.Error("chan *Message create was identified as not a handler")
}
handlers := []interface{}{
make(chan *MessageCreate),
make(chan *MessageCreate, 20),
make(chan *Ready, 20),
func() {},
func(s Session) {},
func(s Session, e *MessageCreate) {},
}
for i := range handlers {
if !isHandler(handlers[i]) {
t.Error("identified as not a handler")
}
}
}
func TestRegister(t *testing.T) {
d := newDispatcher()
handler := make(chan *MessageCreate)
if err := d.register(EvtMessageCreate, handler); err != nil {
t.Fatal(err)
}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
<-handler
wg.Done()
}()
d.dispatch(context.Background(), EvtMessageCreate, &MessageCreate{})
wg.Wait()
}
func TestCtrl_CloseChannel(t *testing.T) {
d := newDispatcher()
handler := make(chan *MessageCreate)
ctrl := &Ctrl{Channel: handler}
if err := d.register(EvtMessageCreate, handler, ctrl); err != nil {
t.Fatal(err)
}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
<-handler
wg.Done()
}()
d.dispatch(context.Background(), EvtMessageCreate, &MessageCreate{})
wg.Wait()
// close channel
ctrl.CloseChannel()
if _, open := <-handler; open {
t.Fatal("expected channel to be closed")
}
// should not hang
d.dispatch(context.Background(), EvtMessageCreate, &MessageCreate{})
}