forked from mymmrac/telego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_group_test.go
316 lines (277 loc) · 6.65 KB
/
handler_group_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package telegohandler
import (
"context"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mymmrac/telego"
)
func TestHandlerGroup_Handle(t *testing.T) {
gr := &HandlerGroup{}
t.Run("panic_nil_handler", func(t *testing.T) {
assert.Panics(t, func() {
gr.Handle(nil)
})
})
handler := Handler(func(bot *telego.Bot, update telego.Update) {})
t.Run("panic_nil_predicate", func(t *testing.T) {
assert.Panics(t, func() {
gr.Handle(handler, nil)
})
})
t.Run("without_predicates", func(t *testing.T) {
gr.Handle(handler)
require.Len(t, gr.handlers, 1)
assert.NotNil(t, gr.handlers[0].handler)
assert.Nil(t, gr.handlers[0].predicates)
gr.handlers = make([]conditionalHandler, 0)
})
predicate := Predicate(func(update telego.Update) bool { return false })
t.Run("with_predicates", func(t *testing.T) {
gr.Handle(handler, predicate)
require.Len(t, gr.handlers, 1)
assert.NotNil(t, gr.handlers[0].handler)
assert.NotNil(t, gr.handlers[0].predicates)
gr.handlers = make([]conditionalHandler, 0)
})
}
func TestHandlerGroup_Group(t *testing.T) {
gr := &HandlerGroup{}
t.Run("panic_nil_predicate", func(t *testing.T) {
assert.Panics(t, func() {
_ = gr.Group(nil)
})
})
t.Run("without_predicates", func(t *testing.T) {
newGr := gr.Group()
require.Len(t, gr.groups, 1)
assert.Equal(t, newGr, gr.groups[0])
gr.groups = nil
})
predicate := Predicate(func(update telego.Update) bool { return false })
t.Run("with_predicates", func(t *testing.T) {
newGr := gr.Group(predicate)
require.Len(t, gr.groups, 1)
assert.Equal(t, newGr, gr.groups[0])
assert.NotEmpty(t, gr.groups[0].predicates)
gr.groups = nil
})
}
func TestHandlerGroup_Use(t *testing.T) {
gr := &HandlerGroup{}
t.Run("panic_nil_middleware", func(t *testing.T) {
assert.Panics(t, func() {
gr.Use(nil)
})
})
middleware := Middleware(func(bot *telego.Bot, update telego.Update, next Handler) {
next(bot, update)
})
t.Run("success", func(t *testing.T) {
gr.Use(middleware)
require.Len(t, gr.middlewares, 1)
assert.NotNil(t, gr.middlewares[0])
})
}
func TestHandlerGroup_processUpdate(t *testing.T) {
var order []int
lock := sync.Mutex{}
updOrder := func(i int) {
lock.Lock()
order = append(order, i)
lock.Unlock()
}
gr := &HandlerGroup{
predicates: []Predicate{
func(update telego.Update) bool {
t.Log("Predicate")
updOrder(1)
return true
},
},
middlewares: []Middleware{
func(bot *telego.Bot, update telego.Update, next Handler) {
t.Log("Before next")
updOrder(9)
next(bot, update)
next(bot, update)
t.Log("After next")
updOrder(10)
},
func(bot *telego.Bot, update telego.Update, next Handler) {
t.Log("Before nested next")
updOrder(11)
next(bot, update)
t.Log("After nested next")
updOrder(12)
},
func(bot *telego.Bot, update telego.Update, next Handler) {
t.Log("Before nested next go")
updOrder(20)
go next(bot, update)
t.Log("After nested next go")
updOrder(21)
},
},
groups: []*HandlerGroup{
{
handlers: []conditionalHandler{
{
predicates: []Predicate{
func(update telego.Update) bool {
t.Log("Predicate handler nested in a group")
updOrder(14)
return false
},
},
},
},
},
{
middlewares: []Middleware{
func(bot *telego.Bot, update telego.Update, next Handler) {
t.Log("Before nested in a group next")
updOrder(15)
ctx, cancel := context.WithCancel(update.Context())
cancel()
next(bot, update.WithContext(ctx))
updOrder(19)
},
},
groups: []*HandlerGroup{
{
middlewares: []Middleware{
func(bot *telego.Bot, update telego.Update, next Handler) {
assert.Fail(t, "shouldn't be called")
},
},
},
},
},
{
middlewares: []Middleware{
func(bot *telego.Bot, update telego.Update, next Handler) {
t.Log("Before nested in a group next")
updOrder(18)
ctx, cancel := context.WithTimeout(update.Context(), smallTimeout)
next(bot, update.WithContext(ctx))
cancel()
t.Log("After nested in a group next")
updOrder(17)
},
func(bot *telego.Bot, update telego.Update, next Handler) {
t.Log("Before nested in a group next")
updOrder(16)
},
},
},
{
predicates: []Predicate{
func(update telego.Update) bool {
t.Log("Predicate nested in a group")
updOrder(13)
return false
},
},
},
{
predicates: []Predicate{
func(update telego.Update) bool {
t.Log("Predicate nested in a group")
updOrder(2)
return true
},
},
middlewares: []Middleware{
func(bot *telego.Bot, update telego.Update, next Handler) {
t.Log("Before nested in a group next")
updOrder(5)
next(bot, update)
t.Log("After nested in a group next")
updOrder(6)
},
},
handlers: []conditionalHandler{
{
handler: func(bot *telego.Bot, update telego.Update) {
t.Log("Handler in a group")
updOrder(3)
},
predicates: []Predicate{
func(update telego.Update) bool {
t.Log("Predicate handler nested in a group")
updOrder(4)
return true
},
},
},
},
},
},
handlers: []conditionalHandler{
{
handler: func(bot *telego.Bot, update telego.Update) {
t.Log("Handler")
updOrder(7)
},
predicates: []Predicate{
func(update telego.Update) bool {
t.Log("Predicate handler")
updOrder(8)
return true
},
},
},
},
}
gr.processUpdate(nil, telego.Update{})
lock.Lock()
t.Log("Order:", order)
ok := false
for i, value := range order {
if value == 21 {
order = append(order[:i], order[i+1:]...)
ok = true
break
}
}
assert.True(t, ok)
assert.Equal(t, []int{1, 9, 11, 20, 14, 15, 19, 18, 16, 17, 13, 2, 5, 4, 3, 6, 12, 10}, order)
lock.Unlock()
}
func TestHandlerGroup_parallel(t *testing.T) {
gr := &HandlerGroup{}
wait := sync.WaitGroup{}
wait.Add(1)
h := func(bot *telego.Bot, update telego.Update) {}
p := func(update telego.Update) bool { return false }
m := func(bot *telego.Bot, update telego.Update, next Handler) { next(bot, update) }
wg := sync.WaitGroup{}
n := 64
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
wait.Wait()
gr.Handle(h, p)
wg.Done()
}()
wg.Add(1)
go func() {
wait.Wait()
gr.Group(p)
wg.Done()
}()
wg.Add(1)
go func() {
wait.Wait()
gr.Use(m)
wg.Done()
}()
}
wait.Done()
wg.Wait()
assert.Len(t, gr.handlers, n)
assert.Len(t, gr.groups, n)
assert.Len(t, gr.middlewares, n)
}