2020-04-06 09:57:18 +00:00
|
|
|
package telebot
|
|
|
|
|
2020-05-01 19:40:41 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-04-06 09:57:18 +00:00
|
|
|
type testPoller struct {
|
2020-04-06 20:02:46 +00:00
|
|
|
updates chan Update
|
2020-05-01 19:40:41 +00:00
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestPoller() *testPoller {
|
|
|
|
return &testPoller{
|
|
|
|
updates: make(chan Update, 1),
|
|
|
|
done: make(chan struct{}, 1),
|
|
|
|
}
|
2020-04-06 09:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *testPoller) Poll(b *Bot, updates chan Update, stop chan struct{}) {
|
|
|
|
for {
|
|
|
|
select {
|
2020-04-06 20:02:46 +00:00
|
|
|
case upd := <-p.updates:
|
|
|
|
updates <- upd
|
2020-04-06 09:57:18 +00:00
|
|
|
case <-stop:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 19:40:41 +00:00
|
|
|
|
|
|
|
func TestMiddlewarePoller(t *testing.T) {
|
|
|
|
tp := newTestPoller()
|
|
|
|
var ids []int
|
|
|
|
|
2020-05-05 13:31:38 +00:00
|
|
|
pref := defaultSettings()
|
2021-02-22 17:41:09 +00:00
|
|
|
pref.Offline = true
|
2020-05-05 13:31:38 +00:00
|
|
|
|
|
|
|
b, err := NewBot(pref)
|
2020-05-01 19:40:41 +00:00
|
|
|
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)
|
|
|
|
}
|