forked from tucnak/telebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poller_test.go
67 lines (54 loc) · 1 KB
/
poller_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 telebot
import (
"testing"
"github.com/stretchr/testify/assert"
)
type testPoller struct {
updates chan Update
done chan struct{}
}
func newTestPoller() *testPoller {
return &testPoller{
updates: make(chan Update, 1),
done: make(chan struct{}, 1),
}
}
func (p *testPoller) Poll(b *Bot, updates chan Update, stop chan struct{}) {
for {
select {
case upd := <-p.updates:
updates <- upd
case <-stop:
return
default:
}
}
}
func TestMiddlewarePoller(t *testing.T) {
tp := newTestPoller()
var ids []int
pref := defaultSettings()
pref.Offline = true
b, err := NewBot(pref)
if err != nil {
t.Fatal(err)
}
b.Poller = NewMiddlewarePoller(tp, func(u *Update) bool {
if u.ID > 0 {
ids = append(ids, u.ID)
return true
}
tp.done <- struct{}{}
return false
})
go func() {
tp.updates <- Update{ID: 1}
tp.updates <- Update{ID: 2}
tp.updates <- Update{ID: 0}
}()
go b.Start()
<-tp.done
b.Stop()
assert.Contains(t, ids, 1)
assert.Contains(t, ids, 2)
}