-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_test.go
396 lines (359 loc) · 8.66 KB
/
channel_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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package longpoll
import (
"context"
"errors"
"fmt"
"io"
"reflect"
"testing"
"time"
)
// simple input/output test cases
func TestChannel(t *testing.T) {
tests := []struct {
name string
ctx context.Context
cfg *ChannelConfig
channel func() <-chan int
handler func(value int) error
expectedResult error
expectedPanic string
}{
{
name: "NilContext",
ctx: nil,
channel: func() <-chan int {
ch := make(chan int)
close(ch)
return ch
},
handler: func(value int) error { return nil },
expectedPanic: "longpoll: nil context",
},
{
name: "NilChannel",
ctx: context.Background(),
channel: func() <-chan int {
return nil
},
handler: func(value int) error { return nil },
expectedPanic: "longpoll: nil channel",
},
{
name: "NilHandler",
ctx: context.Background(),
channel: func() <-chan int {
ch := make(chan int)
close(ch)
return ch
},
handler: nil,
expectedPanic: "longpoll: nil handler",
},
{
name: "ContextCanceled",
ctx: func() context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
}(),
channel: func() <-chan int {
ch := make(chan int)
close(ch)
return ch
},
handler: func(value int) error { return nil },
expectedResult: context.Canceled,
},
{
name: "ChannelClosed",
ctx: context.Background(),
channel: func() <-chan int {
ch := make(chan int)
close(ch)
return ch
},
handler: func(value int) error { return nil },
expectedResult: io.EOF,
},
{
name: "HandlerError",
ctx: context.Background(),
channel: func() <-chan int {
ch := make(chan int, 1)
ch <- 1
close(ch)
return ch
},
handler: func(value int) error { return errors.New("handler error") },
expectedResult: errors.New("handler error"),
},
{
name: "MaxSizeExceededClosed",
ctx: context.Background(),
cfg: &ChannelConfig{
MaxSize: 3,
},
channel: func() <-chan int {
ch := make(chan int, 4)
ch <- 1
ch <- 2
ch <- 3
ch <- 4
return ch
},
handler: func(value int) error { return nil },
expectedResult: nil,
},
{
name: "MaxSizeExceededClosed",
ctx: context.Background(),
cfg: &ChannelConfig{
MaxSize: 3,
},
channel: func() <-chan int {
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)
return ch
},
handler: func(value int) error { return nil },
expectedResult: nil,
},
{
name: "EOF",
ctx: context.Background(),
cfg: &ChannelConfig{
MaxSize: 3,
},
channel: func() <-chan int {
ch := make(chan int, 3)
ch <- 1
ch <- 2
close(ch)
return ch
},
handler: func(value int) error { return nil },
expectedResult: io.EOF,
},
{
name: "MinSizeNotReached",
ctx: context.Background(),
cfg: &ChannelConfig{
MinSize: 2,
},
channel: func() <-chan int {
ch := make(chan int, 1)
ch <- 1
close(ch)
return ch
},
handler: func(value int) error { return nil },
expectedResult: io.EOF,
},
{
name: "PartialTimeoutReached",
ctx: context.Background(),
cfg: &ChannelConfig{
MinSize: -1,
PartialTimeout: 50 * time.Millisecond,
},
channel: func() <-chan int {
ch := make(chan int)
go func() {
time.Sleep(100 * time.Millisecond)
ch <- 1
close(ch)
}()
return ch
},
handler: func(value int) error { return nil },
expectedResult: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ch := tt.channel()
var success bool
defer func() {
if tt.expectedPanic != `` {
if success {
t.Errorf("Expected panic %q, got none", tt.expectedPanic)
} else if v := fmt.Sprint(recover()); v != tt.expectedPanic {
t.Errorf("Expected panic %q, got %q", tt.expectedPanic, v)
}
} else if !success {
t.Errorf("Expected success, got panic %q", tt.expectedPanic)
}
}()
result := Channel(tt.ctx, tt.cfg, ch, tt.handler)
success = true
if (result == nil && tt.expectedResult != nil) || (result != nil && tt.expectedResult == nil) {
t.Errorf("Expected result %v, got %v", tt.expectedResult, result)
} else if result != nil && tt.expectedResult != nil && result.Error() != tt.expectedResult.Error() {
t.Errorf("Expected result %v, got %v", tt.expectedResult, result)
}
})
}
}
// test that it drains as much as possible prior to exit
func TestChannel_maxSizeLoopNoMoreAvailable(t *testing.T) {
ch := make(chan int, 4)
ch <- 1
ch <- 2
ch <- 3
var values []int
err := Channel(context.Background(), &ChannelConfig{
MaxSize: 10,
MinSize: -1, // disable, trigger second loop immediately
PartialTimeout: -1, // disable, trigger second loop immediately
}, ch, func(value int) error {
values = append(values, value)
return nil
})
if err != nil {
t.Errorf("Expected no error, got %v", err)
} else if len(values) != 3 {
t.Errorf("Expected 3 values, got %d", len(values))
} else if values[0] != 1 || values[1] != 2 || values[2] != 3 {
t.Errorf("Expected values [1, 2, 3], got %v", values)
}
}
// similar to the above, but the terminal case is the channel is closed
func TestChannel_maxSizeLoopClosed(t *testing.T) {
ch := make(chan int, 4)
ch <- 1
ch <- 2
ch <- 3
close(ch)
var values []int
err := Channel(context.Background(), &ChannelConfig{
MaxSize: 10,
MinSize: -1, // disable, trigger second loop immediately
PartialTimeout: -1, // disable, trigger second loop immediately
}, ch, func(value int) error {
values = append(values, value)
return nil
})
if err != io.EOF {
t.Errorf("Expected io.EOF, got %v", err)
} else if len(values) != 3 {
t.Errorf("Expected 3 values, got %d", len(values))
} else if values[0] != 1 || values[1] != 2 || values[2] != 3 {
t.Errorf("Expected values [1, 2, 3], got %v", values)
}
}
// similar to the above again, but the terminal case is we hit the max size
func TestChannel_maxSizeLoopHitMax(t *testing.T) {
ch := make(chan int, 4)
ch <- 1
ch <- 2
ch <- 3
ch <- 4
var values []int
err := Channel(context.Background(), &ChannelConfig{
MaxSize: 3,
MinSize: -1, // disable, trigger second loop immediately
PartialTimeout: -1, // disable, trigger second loop immediately
}, ch, func(value int) error {
values = append(values, value)
return nil
})
if err != nil {
t.Errorf("Expected no error, got %v", err)
} else if len(values) != 3 {
t.Errorf("Expected 3 values, got %d", len(values))
} else if values[0] != 1 || values[1] != 2 || values[2] != 3 {
t.Errorf("Expected values [1, 2, 3], got %v", values)
}
if v := <-ch; v != 4 {
t.Errorf("Expected value 4, got %d", v)
}
}
// testing that disabling min size when using partial timeout causes it to wait
// for the first value, at least (or timeout/cancel)
func TestChannel_waitForSingleValuePartialTimeoutNoMinSize(t *testing.T) {
ch := make(chan int, 1)
in := make(chan int)
out := make(chan error)
startTime := time.Now()
go func() {
out <- Channel(context.Background(), &ChannelConfig{
MaxSize: 10,
MinSize: -1,
PartialTimeout: time.Second * 3,
}, ch, func(value int) error {
in <- value
return nil
})
}()
time.Sleep(time.Millisecond * 30)
select {
case <-in:
t.Fatal()
case <-out:
t.Fatal()
default:
}
ch <- 1
if v := <-in; v != 1 {
t.Fatal(v)
}
if err := <-out; err != nil {
t.Fatal(err)
}
if d := time.Since(startTime); d > time.Second*2 {
t.Fatal(d)
}
}
// tests an immediate exit case where the configuration specifies no waiting,
// and there are no values available
func TestChannel_noMinSizeOrPartialTimeoutNoValues(t *testing.T) {
ch := make(chan bool)
if err := Channel(context.Background(), &ChannelConfig{
MaxSize: -1,
MinSize: -1,
PartialTimeout: -1,
}, ch, func(value bool) error {
t.Fatal(value)
return nil
}); err != nil {
t.Fatal(err)
}
}
func TestChannel_minSizeNoPartialTimeout(t *testing.T) {
ch := make(chan float64)
out := make(chan error)
var values []float64
go func() {
out <- Channel(context.Background(), &ChannelConfig{
MaxSize: -1,
MinSize: 5,
PartialTimeout: -1,
}, ch, func(value float64) error {
values = append(values, value)
return nil
})
}()
time.Sleep(time.Millisecond * 30)
ch <- 1
ch <- 2
ch <- 3
time.Sleep(time.Millisecond * 30)
ch <- 4
time.Sleep(time.Millisecond * 30)
select {
case <-out:
t.Fatal()
default:
}
ch <- 5
if err := <-out; err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(values, []float64{1, 2, 3, 4, 5}) {
t.Fatal(values)
}
}