forked from botopolis/bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
responder_queue_internal_test.go
67 lines (52 loc) · 1.48 KB
/
responder_queue_internal_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
package bot
import (
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
type testChat struct{}
func (c testChat) Load(r *Robot) {}
func (c testChat) Username() string { return "" }
func (c testChat) Messages() <-chan Message { return make(chan Message) }
func (c testChat) Send(m Message) error { return nil }
func (c testChat) Reply(m Message) error { return nil }
func (c testChat) Topic(m Message) error { return nil }
func (c testChat) Direct(m Message) error { return nil }
func TestResponderQueueCallback(t *testing.T) {
rq := newResponderQueue(1)
msg := Message{Text: "hi"}
rq.On(DefaultMessage, func(rs *Responder) {
assert.Equal(t, msg, rs.Message)
})
rq.Emit(DefaultMessage, &Responder{Message: msg})
}
func TestResponderQueueChannel(t *testing.T) {
rq := newResponderQueue(2)
msg := Message{Text: "hi"}
pipe := rq.On(Leave)
rq.Emit(Leave, &Responder{Message: msg})
rs := <-pipe
assert.Equal(t, msg, rs.Message)
}
func TestResponderQueueForward(t *testing.T) {
rs := newResponderQueue(0)
msg1 := Message{Type: DefaultMessage, Text: "foo"}
msg2 := Message{Type: Leave, Text: "bar"}
var wg sync.WaitGroup
ch := make(chan Message, 3)
defer close(ch)
wg.Add(2)
ch <- msg1
ch <- msg2
rs.On(DefaultMessage, func(rs *Responder) {
assert.Equal(t, msg1, rs.Message)
wg.Done()
})
rs.On(Leave, func(rs *Responder) {
assert.Equal(t, msg2, rs.Message)
wg.Done()
})
// subject
go rs.Forward(&Robot{Chat: testChat{}}, ch)
wg.Wait()
}