test: replace timers with chan communication

pull/283/head
Demian 4 years ago
parent 31048be20c
commit 0ae9a122e5

@ -110,11 +110,10 @@ func TestBotStart(t *testing.T) {
// remove webhook to be sure that bot can poll
assert.NoError(t, b.RemoveWebhook())
time.AfterFunc(50*time.Millisecond, b.Stop)
b.Start() // stops after some delay
assert.Empty(t, b.stop)
go b.Start()
b.Stop()
tp := &testPoller{updates: make(chan Update, 1)}
tp := newTestPoller()
go func() {
tp.updates <- Update{Message: &Message{Text: "/start"}}
}()
@ -126,11 +125,14 @@ func TestBotStart(t *testing.T) {
var ok bool
b.Handle("/start", func(m *Message) {
assert.Equal(t, m.Text, "/start")
tp.done <- struct{}{}
ok = true
})
time.AfterFunc(100*time.Millisecond, b.Stop)
b.Start() // stops after some delay
go b.Start()
<-tp.done
b.Stop()
assert.True(t, ok)
}
@ -144,9 +146,12 @@ func TestBotIncomingUpdate(t *testing.T) {
t.Fatal(err)
}
tp := &testPoller{updates: make(chan Update, 1)}
tp := newTestPoller()
b.Poller = tp
b.Handle("/stop", func(m *Message) {
tp.done <- struct{}{}
})
b.Handle("/start", func(m *Message) {
assert.Equal(t, "/start", m.Text)
})
@ -280,10 +285,12 @@ func TestBotIncomingUpdate(t *testing.T) {
tp.updates <- Update{PreCheckoutQuery: &PreCheckoutQuery{ID: "checkout"}}
tp.updates <- Update{Poll: &Poll{ID: "poll"}}
tp.updates <- Update{PollAnswer: &PollAnswer{PollID: "poll"}}
tp.updates <- Update{Message: &Message{Text: "/stop"}}
}()
time.AfterFunc(100*time.Millisecond, b.Stop)
b.Start() // stops after some delay
go b.Start()
<-tp.done
b.Stop()
}
func TestBot(t *testing.T) {

Loading…
Cancel
Save