-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathlong_polling_test.go
125 lines (99 loc) · 2.85 KB
/
long_polling_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package telego
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)
const timeout = time.Second
func TestBot_UpdatesViaLongPolling(t *testing.T) {
ctrl := gomock.NewController(t)
t.Run("success", func(t *testing.T) {
m := newMockedBot(ctrl)
m.MockRequestConstructor.EXPECT().
JSONRequest(gomock.Any()).
Return(data, nil).MinTimes(1)
expectedUpdates := []Update{
{UpdateID: 1},
{UpdateID: 2},
}
resp := telegoResponse(t, expectedUpdates)
m.MockAPICaller.EXPECT().
Call(gomock.Any(), gomock.Any(), gomock.Any()).
Return(resp, nil).MinTimes(1)
assert.NotPanics(t, func() {
updates, err := m.Bot.UpdatesViaLongPolling(testCtx, nil)
require.NoError(t, err)
time.Sleep(time.Millisecond * 10)
select {
case <-time.After(timeout):
t.Fatal("Timeout")
case update := <-updates:
assert.NotZero(t, update.UpdateID)
}
})
})
t.Run("error_get_update", func(t *testing.T) {
m := newMockedBot(ctrl)
m.MockRequestConstructor.EXPECT().
JSONRequest(gomock.Any()).
Return(nil, errTest).MinTimes(1)
assert.NotPanics(t, func() {
_, err := m.Bot.UpdatesViaLongPolling(testCtx, nil)
require.NoError(t, err)
time.Sleep(time.Millisecond * 10)
})
})
t.Run("error_already_running", func(t *testing.T) {
m := newMockedBot(ctrl)
m.MockRequestConstructor.EXPECT().
JSONRequest(gomock.Any()).
Return(nil, errTest).AnyTimes()
assert.NotPanics(t, func() {
_, err := m.Bot.UpdatesViaLongPolling(testCtx, nil)
require.NoError(t, err)
_, err = m.Bot.UpdatesViaLongPolling(testCtx, nil)
require.Error(t, err)
})
})
t.Run("error_options", func(t *testing.T) {
m := newMockedBot(ctrl)
assert.NotPanics(t, func() {
_, err := m.Bot.UpdatesViaLongPolling(testCtx, nil, WithLongPollingUpdateInterval(-time.Second))
require.Error(t, err)
})
})
}
func TestWithLongPollingUpdateInterval(t *testing.T) {
ctx := &longPolling{}
interval := time.Second
t.Run("success", func(t *testing.T) {
err := WithLongPollingUpdateInterval(interval)(ctx)
require.NoError(t, err)
assert.EqualValues(t, interval, ctx.updateInterval)
})
t.Run("error", func(t *testing.T) {
err := WithLongPollingUpdateInterval(-interval)(ctx)
require.Error(t, err)
})
}
func TestWithLongPollingRetryTimeout(t *testing.T) {
ctx := &longPolling{}
t.Run("success", func(t *testing.T) {
err := WithLongPollingRetryTimeout(timeout)(ctx)
require.NoError(t, err)
assert.EqualValues(t, timeout, ctx.retryTimeout)
})
t.Run("error", func(t *testing.T) {
err := WithLongPollingRetryTimeout(-timeout)(ctx)
require.Error(t, err)
})
}
func TestWithLongPollingBuffer(t *testing.T) {
ctx := &longPolling{}
buffer := uint(1)
err := WithLongPollingBuffer(buffer)(ctx)
require.NoError(t, err)
assert.EqualValues(t, buffer, ctx.updateChanBuffer)
}