generated from devnw/oss-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstream_test.go
591 lines (487 loc) · 10.8 KB
/
stream_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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
package stream
import (
"context"
"testing"
"time"
"go.devnw.com/gen"
)
func PipeTest[U ~[]T, T comparable](
t *testing.T,
name string,
data []U,
) {
Tst(
t,
name,
data,
func(t *testing.T, data []T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c1, c2 := make(chan T), make(chan T)
go Pipe(ctx, c1, c2)
go func() {
for _, v := range data {
select {
case <-ctx.Done():
return
case c1 <- v:
}
}
}()
for i := 0; i < len(data); i++ {
select {
case <-ctx.Done():
t.Error("context canceled")
return
case out, ok := <-c2:
if !ok {
if i != len(data)-1 {
t.Fatal("c2 closed prematurely")
}
return
}
if out != data[i] {
t.Errorf("expected %v, got %v", data[i], out)
}
}
}
})
}
func Test_Pipe(t *testing.T) {
PipeTest(t, "int8", IntTests[int8](100, 1000))
PipeTest(t, "uint8", IntTests[uint8](100, 1000))
PipeTest(t, "uint8", IntTests[uint8](100, 1000))
PipeTest(t, "uint16", IntTests[uint16](100, 1000))
PipeTest(t, "int32", IntTests[int32](100, 1000))
PipeTest(t, "uint32", IntTests[uint32](100, 1000))
PipeTest(t, "int64", IntTests[int64](100, 1000))
PipeTest(t, "uint64", IntTests[uint64](100, 1000))
PipeTest(t, "float32", FloatTests[float32](100, 1000))
PipeTest(t, "float64", FloatTests[float64](100, 1000))
}
func FanInTest[U ~[]T, T comparable](
t *testing.T,
name string,
data []U,
) {
Tst(
t,
name,
data,
func(t *testing.T, data []T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
divisor := 5
if len(data)%divisor != 0 {
t.Fatalf("data length must be divisible by %v", divisor)
}
out := make([]chan T, divisor)
// Initialize channels
for i := range out {
out[i] = make(chan T)
}
fan := FanIn(ctx, gen.ReadOnly(out...)...)
ichan := 0
cursor := 0
for i := len(data) / divisor; i <= len(data); i += len(data) / divisor {
go func(out chan<- T, data []T) {
defer close(out)
for _, v := range data {
select {
case <-ctx.Done():
return
case out <- v:
}
}
}(out[ichan], data[cursor:i])
cursor = i
ichan++
}
returned := make([]T, len(data))
for i := 0; ; i++ {
select {
case <-ctx.Done():
t.Error("context canceled")
return
case out, ok := <-fan:
if !ok {
if i != len(data) {
t.Fatalf("c2 closed prematurely; index %v", i)
}
return
}
returned[i] = out
}
}
})
}
func Test_FanIn(t *testing.T) {
FanInTest(t, "int8", IntTests[int8](100, 1000))
FanInTest(t, "uint8", IntTests[uint8](100, 1000))
FanInTest(t, "uint8", IntTests[uint8](100, 1000))
FanInTest(t, "uint16", IntTests[uint16](100, 1000))
FanInTest(t, "int32", IntTests[int32](100, 1000))
FanInTest(t, "uint32", IntTests[uint32](100, 1000))
FanInTest(t, "int64", IntTests[int64](100, 1000))
FanInTest(t, "uint64", IntTests[uint64](100, 1000))
FanInTest(t, "float32", FloatTests[float32](100, 1000))
FanInTest(t, "float64", FloatTests[float64](100, 1000))
}
func InterceptTest[U ~[]T, T signed](
t *testing.T,
name string,
data []U,
) {
Tst(
t,
name,
data,
func(t *testing.T, data []T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
in := make(chan T)
defer close(in)
out := Intercept(ctx, in, func(_ context.Context, in T) (T, bool) {
return in % 3, true
})
go func() {
for _, v := range data {
select {
case <-ctx.Done():
return
case in <- v:
}
}
}()
for i := 0; i < len(data); i++ {
select {
case <-ctx.Done():
t.Error("context canceled")
return
case out, ok := <-out:
if !ok {
if i != len(data)-1 {
t.Fatal("c2 closed prematurely")
}
}
if out != data[i]%3 {
t.Errorf("expected %v, got %v", data[i], out)
}
}
}
})
}
func Test_Intercept(t *testing.T) {
InterceptTest(t, "int8", IntTests[int8](100, 1000))
InterceptTest(t, "int8", IntTests[int8](100, 1000))
InterceptTest(t, "int32", IntTests[int32](100, 1000))
InterceptTest(t, "int64", IntTests[int64](100, 1000))
}
func Test_Intercept_ChangeType(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
integers := Ints[int](100)
booleans := make([]bool, len(integers))
for i, v := range integers {
booleans[i] = v%2 == 0
}
out := Intercept(
ctx,
gen.Slice[int](integers).Chan(ctx),
func(_ context.Context, in int) (bool, bool) {
return in%2 == 0, true
})
for i := 0; ; i++ {
select {
case <-ctx.Done():
return
case out, ok := <-out:
if !ok {
return
}
if out != booleans[i] {
t.Errorf("expected %v, got %v", booleans[0], out)
}
}
}
}
func Test_Intercept_NotOk(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
in := make(chan int)
defer close(in)
go func() {
in <- 1
in <- 2
in <- 3
in <- 0
}()
out := Intercept(ctx, in, func(_ context.Context, v int) (int, bool) {
if v == 0 {
return 0, true
}
return v, false
})
select {
case <-ctx.Done():
t.Fatal("context canceled")
case out, ok := <-out:
if !ok {
t.Fatal("in closed prematurely")
}
if out != 0 {
t.Errorf("expected 0, got %v", out)
}
}
}
func Test_Intercept_ClosedChan(_ *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
in := make(chan int)
out := Intercept(
ctx,
in,
func(_ context.Context, v int) (int, bool) {
return v, false
})
close(in)
<-out
}
func Test_Intercept_Canceled_On_Wait(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
in := make(chan int)
defer close(in)
// Setup intercept
out := Intercept(ctx, in, func(_ context.Context, v int) (int, bool) {
return v, true
})
// Push to in
in <- 1
// Wait for intercept routine to be scheduled
time.Sleep(time.Millisecond)
// Cancel the routine
cancel()
_, ok := <-out
if ok {
t.Error("expected closed channel")
}
}
func Test_FanOut_Canceled_On_Wait(_ *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
in, out := make(chan int), make(chan int)
defer close(in)
defer close(out)
go func() {
defer cancel()
in <- 1
}()
FanOut(ctx, in, out)
}
//nolint:gocognit // This is a test function
func DistributeTest[U ~[]T, T comparable](
t *testing.T,
name string,
data []U,
) {
Tst(
t,
name,
data,
func(t *testing.T, data []T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c1, c2, c3 := make(chan T), make(chan T), make(chan T)
go Distribute(ctx, gen.Slice[T](data).Chan(ctx), c1, c2, c3)
c1total, c2total, c3total := 0, 0, 0
for i := 0; i < len(data); i++ {
select {
case <-ctx.Done():
t.Error("context canceled")
return
case out, ok := <-c1:
if !ok {
return
}
if out != data[i] {
t.Errorf("expected %v, got %v", data[i], out)
}
c1total++
case out, ok := <-c2:
if !ok {
return
}
if out != data[i] {
t.Errorf("expected %v, got %v", data[i], out)
}
c2total++
case out, ok := <-c3:
if !ok {
return
}
if out != data[i] {
t.Errorf("expected %v, got %v", data[i], out)
}
c3total++
}
}
t.Logf("c1: %v", c1total)
t.Logf("c2: %v", c2total)
t.Logf("c3: %v", c3total)
ctotal := c1total + c2total + c3total
if ctotal != len(data) {
t.Errorf("expected %v, got %v", len(data), ctotal)
}
})
}
func Test_Distribute(t *testing.T) {
DistributeTest(t, "int8", IntTests[int8](100, 1000))
DistributeTest(t, "uint8", IntTests[uint8](100, 1000))
DistributeTest(t, "uint8", IntTests[uint8](100, 1000))
DistributeTest(t, "uint16", IntTests[uint16](100, 1000))
DistributeTest(t, "int32", IntTests[int32](100, 1000))
DistributeTest(t, "uint32", IntTests[uint32](100, 1000))
DistributeTest(t, "int64", IntTests[int64](100, 1000))
DistributeTest(t, "uint64", IntTests[uint64](100, 1000))
DistributeTest(t, "float32", FloatTests[float32](100, 1000))
DistributeTest(t, "float64", FloatTests[float64](100, 1000))
}
func Test_Distribute_Canceled_On_Wait(_ *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
in, out := make(chan int), make(chan int)
defer close(in)
defer close(out)
go func() {
defer cancel()
in <- 1
}()
Distribute(ctx, in, out)
}
func Test_Distribute_ZeroOut(_ *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
in := make(chan int)
defer close(in)
Distribute(ctx, in)
}
func Test_FanOut(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c1, c2, c3 := make(chan int), make(chan int), make(chan int)
var c4 chan int
data := Ints[int](1000)
go FanOut(ctx, gen.Slice[int](data).Chan(ctx), c1, c2, c3, c4)
seen := make(map[int]int)
for i := 0; i < len(data)*3; i++ {
select {
case <-ctx.Done():
t.Fatal("context canceled")
return
case _, ok := <-c1:
if !ok {
return
}
seen[1]++
case _, ok := <-c2:
if !ok {
return
}
seen[2]++
case _, ok := <-c3:
if !ok {
return
}
seen[3]++
case _, ok := <-c4:
if !ok {
return
}
seen[4]++
}
}
if len(seen) != 3 {
t.Fatalf("expected %v, got %v", len(data)-1, len(seen))
}
for k, v := range seen {
if k == 4 {
if v > 0 {
t.Fatalf("expected %v, got %v", 0, v)
}
}
if v != len(data) {
t.Fatalf("Chan C%v: expected %v, got %v", k, len(data), v)
}
}
}
func Test_FanOut_ZeroOut(_ *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
in := make(chan int)
defer close(in)
FanOut(ctx, in)
}
func Test_FanIn_ZeroIn(_ *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
FanIn[int](ctx)
}
func Test_Drain(t *testing.T) {
ctx, cancel := context.WithTimeout(
context.Background(),
time.Second*5,
)
count := 1000
in := make(chan int, count)
start := make(chan struct{})
go func() {
defer cancel()
defer close(in)
<-start
for i := 0; i < count; i++ {
select {
case <-ctx.Done():
return
case in <- i:
}
}
}()
Drain(ctx, in)
close(start)
<-ctx.Done()
if ctx.Err() != context.Canceled {
t.Fatalf("expected %v, got %v", context.Canceled, ctx.Err())
}
}
func Test_Any(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
count := 1000
in := make(chan int, count)
go func() {
defer close(in)
for i := 0; i < count; i++ {
select {
case <-ctx.Done():
return
case in <- i:
}
}
}()
out := Any(ctx, in)
for i := 0; i < count; i++ {
select {
case <-ctx.Done():
return
case v, ok := <-out:
if !ok {
return
}
value, ok := v.(int)
if !ok || value != i {
t.Fatalf("expected %v, got %v", i, value)
}
}
}
}