-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflashfloodexample_test.go
More file actions
625 lines (515 loc) · 12.6 KB
/
flashfloodexample_test.go
File metadata and controls
625 lines (515 loc) · 12.6 KB
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
package flashflood_test
import (
"fmt"
"strings"
"time"
"github.com/thisisdevelopment/flashflood/v2"
)
// TestObj and getTestObjs are defined in flashflood__01_test.go
// example using Push method buffer of 3 pushing 5 elements, no gate
func ExampleFlashFlood_Push_example01() {
ff := flashflood.New[TestObj](&flashflood.Opts{
BufferAmount: 3,
Timeout: time.Duration(250 * time.Millisecond),
})
// o[0] := TestObj{Key: "k1", Value: "v1"}
// ...
o := getTestObjs(5)
// get the channel from buffer
ch, err := ff.GetChan()
_ = err
// push elements in buffer
ff.Push(o[0], o[1], o[2], o[3])
// listen on channel
select {
case v := <-ch:
fmt.Println("KV:", v.Key, v.Value)
}
// push element in buffer
ff.Push(o[4])
select {
case v := <-ch:
fmt.Println("KV:", v.Key, v.Value)
}
// Output: KV: k1 v1
// KV: k2 v2
}
func ExampleFlashFlood_Unshift_example01() {
ff := flashflood.New[TestObj](&flashflood.Opts{
BufferAmount: 3,
Timeout: time.Duration(250 * time.Millisecond),
})
o := getTestObjs(5)
ch, err := ff.GetChan()
_ = err
ff.Push(o[0], o[1], o[2])
ff.Unshift(o[3])
ff.Unshift(o[4])
select {
case v := <-ch:
fmt.Println("KV:", v.Key, v.Value)
}
select {
case v := <-ch:
fmt.Println("KV:", v.Key, v.Value)
}
select {
case v := <-ch:
fmt.Println("KV:", v.Key, v.Value)
}
// Output: KV: k4 v4
// KV: k5 v5
// KV: k1 v1
}
func ExampleFlashFlood_Unshift_example02() {
ff := flashflood.New[TestObj](&flashflood.Opts{
BufferAmount: 3,
Timeout: time.Duration(250 * time.Millisecond),
})
o := getTestObjs(5)
ch, err := ff.GetChan()
_ = err
ff.Push(o[0], o[1], o[2])
ff.Unshift(o[3], o[4])
select {
case v := <-ch:
fmt.Println("KV:", v.Key, v.Value)
}
select {
case v := <-ch:
fmt.Println("KV:", v.Key, v.Value)
}
select {
case v := <-ch:
fmt.Println("KV:", v.Key, v.Value)
}
// Output: KV: k4 v4
// KV: k5 v5
// KV: k1 v1
}
func ExampleFlashFlood_Purge() {
// instance
ff := flashflood.New[TestObj](&flashflood.Opts{
BufferAmount: 2,
Timeout: time.Duration(250 * time.Millisecond),
})
o := getTestObjs(5)
// Prep test Objs
// get the channel
ch, err := ff.GetChan()
_ = err
// push to buffer
ff.Push(o[0], o[1])
// purge buffer
ff.Purge()
// push to buffer
ff.Push(o[2], o[3], o[4])
select {
case v := <-ch:
fmt.Println("KV:", v.Key, v.Value)
}
select {
case v := <-ch:
fmt.Println("KV:", v.Key, v.Value)
}
select {
case v := <-ch:
fmt.Println("KV:", v.Key, v.Value)
}
// Output: KV: k3 v3
// KV: k4 v4
// KV: k5 v5
}
// example using Push method buffer of 4 pushing 6 byte slices of 3 bytes each, gate of 3, deprecated byte functions removed in v2
func ExampleFlashFlood_Push_example02() {
ff := flashflood.New[[]byte](&flashflood.Opts{
BufferAmount: 4,
Timeout: time.Duration(100 * time.Millisecond), // Reduced from 250ms for CI
GateAmount: 3,
})
b := [][]byte{
{0x1, 0x2, 0x3},
{0x4, 0x5, 0x6},
{0x7, 0x8, 0x9},
{0xa, 0xb, 0xc},
{0xd, 0xe, 0xf},
{0x10},
}
// Add byte merging function
ff.AddFunc(flashflood.FuncMergeBytes())
ch, err := ff.GetChan()
_ = err
ff.Push(b[0], b[1], b[2], b[3], b[4], b[5])
time.Sleep(200 * time.Millisecond) // Reduced from 500ms for CI
run := true
for run {
select {
case v := <-ch:
fmt.Println("RESULT:", v)
default:
run = false
break
}
}
// Output: RESULT: [1 2 3 4 5 6 7 8 9]
// RESULT: [10 11 12 13 14 15 16]
}
// example using Push method buffer of 4 pushing 6 byte slices of 3 bytes each, gate of 2, use merge callback function to merge output, not waiting for a timeout
func ExampleFlashFlood_Push_example03() {
ff := flashflood.New[[]byte](&flashflood.Opts{
BufferAmount: 4,
Timeout: time.Duration(250 * time.Millisecond),
GateAmount: 2,
})
b := [][]byte{
{0x1, 0x2, 0x3},
{0x4, 0x5, 0x6},
{0x7, 0x8, 0x9},
{0xa, 0xb, 0xc},
{0xd, 0xe, 0xf},
{0x10},
}
// Add byte merging function
ff.AddFunc(flashflood.FuncMergeBytes())
ch, err := ff.GetChan()
_ = err
ff.Push(b[0], b[1], b[2], b[3], b[4], b[5])
run := true
for run {
select {
case v := <-ch:
fmt.Println("RESULT:", v)
default:
run = false
break
}
}
// Output: RESULT: [1 2 3 4 5 6]
}
// example using Push method buffer of 4 pushing 6 byte slices of 3 bytes each, gate of 2, use merge callback function to merge output, wait for timeout
func ExampleFlashFlood_Push_example04() {
ff := flashflood.New[[]byte](&flashflood.Opts{
BufferAmount: 4,
Timeout: time.Duration(100 * time.Millisecond), // Reduced from 250ms for CI
GateAmount: 2,
})
b := [][]byte{
{0x1, 0x2, 0x3},
{0x4, 0x5, 0x6},
{0x7, 0x8, 0x9},
{0xa, 0xb, 0xc},
{0xd, 0xe, 0xf},
{0x10},
}
// Add byte merging function
ff.AddFunc(flashflood.FuncMergeBytes())
ch, err := ff.GetChan()
_ = err
ff.Push(b[0], b[1], b[2], b[3], b[4], b[5])
time.Sleep(200 * time.Millisecond) // Reduced from 500ms for CI
run := true
for run {
select {
case v := <-ch:
fmt.Println("RESULT:", v)
default:
run = false
break
}
}
// Output: RESULT: [1 2 3 4 5 6]
// RESULT: [7 8 9 10 11 12]
// RESULT: [13 14 15 16]
}
// example using Push method buffer of 4 pushing 6 byte slices of 3 bytes each, gate of 3, use merge callback function to merge output, not waiting for timeout (result is no output at all)
func ExampleFlashFlood_Push_example05() {
ff := flashflood.New[[]byte](&flashflood.Opts{
BufferAmount: 4,
Timeout: time.Duration(250 * time.Millisecond),
GateAmount: 3,
})
b := [][]byte{
{0x1, 0x2, 0x3},
{0x4, 0x5, 0x6},
{0x7, 0x8, 0x9},
{0xa, 0xb, 0xc},
{0xd, 0xe, 0xf},
{0x10},
}
// Add byte merging function
ff.AddFunc(flashflood.FuncMergeBytes())
ch, err := ff.GetChan()
_ = err
ff.Push(b[0], b[1], b[2], b[3], b[4], b[5])
run := true
for run {
select {
case v := <-ch:
fmt.Println("RESULT:", v)
default:
run = false
break
}
}
// Output:
}
// example using Push method buffer of 4 pushing 6 byte slices of 3 bytes each, gate of 3, use callback function send individual bytes on channel, wait for timeout
func ExampleFlashFlood_Push_example06() {
ff := flashflood.New[byte](&flashflood.Opts{
BufferAmount: 4,
Timeout: time.Duration(100 * time.Millisecond), // Reduced from 250ms for CI
GateAmount: 3,
})
b := [][]byte{
{0x1, 0x2, 0x3},
{0x4, 0x5, 0x6},
{0x7, 0x8, 0x9},
{0xa, 0xb, 0xc},
{0xd, 0xe, 0xf},
{0x10},
}
// Add individual bytes function
ff.AddFunc(flashflood.FuncReturnIndividualBytes())
ch, err := ff.GetChan()
_ = err
// Push individual bytes from the byte slices
for _, slice := range b {
for _, byteVal := range slice {
ff.Push(byteVal)
}
}
time.Sleep(200 * time.Millisecond) // Reduced from 500ms for CI
run := true
for run {
select {
case v := <-ch:
fmt.Println("RESULT:", v)
default:
run = false
break
}
}
// Output: RESULT: 1
// RESULT: 2
// RESULT: 3
// RESULT: 4
// RESULT: 5
// RESULT: 6
// RESULT: 7
// RESULT: 8
// RESULT: 9
// RESULT: 10
// RESULT: 11
// RESULT: 12
// RESULT: 13
// RESULT: 14
// RESULT: 15
// RESULT: 16
}
// example using Push method buffer of 3 pushing 7 byte slices of 3 bytes each, gate of 3, use callback function send elements on channel, wait for timeout
func ExampleFlashFlood_Push_example07() {
ff := flashflood.New[[][]byte](&flashflood.Opts{
BufferAmount: 3,
Timeout: time.Duration(100 * time.Millisecond),
GateAmount: 3,
})
b := [][]byte{
{0x1, 0x2, 0x3},
{0x4, 0x5, 0x6},
{0x7, 0x8, 0x9},
{0xa, 0xb, 0xc},
{0xd, 0xe, 0xf},
{0x10, 0x11, 0x12},
{0x13},
}
ff.AddFunc(flashflood.FuncMergeChunkedElements[[]byte]())
ch, err := ff.GetChan()
_ = err
// Push individual byte slices for chunked processing
for _, bs := range b {
ff.Push([][]byte{bs}) // Wrap each []byte in [][]byte for chunking
}
time.Sleep(200 * time.Millisecond) // Reduced from 500ms for CI
run := true
for run {
select {
case v := <-ch:
fmt.Println("RESULT:", v)
default:
run = false
break
}
}
// Output: RESULT: [[1 2 3] [4 5 6] [7 8 9]]
// RESULT: [[10 11 12] [13 14 15] [16 17 18]]
// RESULT: [[19]]
}
// example Debug enabled
func ExampleFlashFlood_Push_example08() {
ff := flashflood.New[TestObj](&flashflood.Opts{
BufferAmount: 3,
Timeout: time.Duration(250 * time.Millisecond),
Debug: true,
})
o := getTestObjs(5)
ch, _ := ff.GetChan()
ff.Push(o[0], o[1], o[2], o[3], o[4])
for i := 0; i < 5; i++ {
select {
case v := <-ch:
fmt.Println(v.Key)
}
}
// Output: DEBUG: []flashflood_test.TestObj{flashflood_test.TestObj{Key:"k1", Value:"v1"}, flashflood_test.TestObj{Key:"k2", Value:"v2"}}
// k1
// k2
// DEBUG: []flashflood_test.TestObj{flashflood_test.TestObj{Key:"k3", Value:"v3"}, flashflood_test.TestObj{Key:"k4", Value:"v4"}, flashflood_test.TestObj{Key:"k5", Value:"v5"}}
// k3
// k4
// k5
}
// example Debug enabled using callback function.
func ExampleFlashFlood_Push_example09() {
ff := flashflood.New[TestObj](&flashflood.Opts{
BufferAmount: 3,
Timeout: time.Duration(250 * time.Millisecond),
Debug: true,
})
o := getTestObjs(5)
ch, _ := ff.GetChan()
f := func(objs []TestObj, _ *flashflood.FlashFlood[TestObj]) []TestObj {
var c []TestObj
for k, v := range objs {
// Transform TestObj to new TestObj with modified fields
transformed := TestObj{
Key: fmt.Sprintf("---%d_%s", k, v.Key),
Value: v.Value,
}
c = append(c, transformed)
}
return c
}
ff.AddFunc(f)
ff.Push(o[0], o[1], o[2], o[3], o[4])
for i := 0; i < 5; i++ {
select {
case v := <-ch:
fmt.Println(v)
}
}
//Output: DEBUG: []flashflood_test.TestObj{flashflood_test.TestObj{Key:"---0_k1", Value:"v1"}, flashflood_test.TestObj{Key:"---1_k2", Value:"v2"}}
//{---0_k1 v1}
//{---1_k2 v2}
//DEBUG: []flashflood_test.TestObj{flashflood_test.TestObj{Key:"---0_k3", Value:"v3"}, flashflood_test.TestObj{Key:"---1_k4", Value:"v4"}, flashflood_test.TestObj{Key:"---2_k5", Value:"v5"}}
//{---0_k3 v3}
//{---1_k4 v4}
//{---2_k5 v5}
}
// example Debug enabled using multiple callback function. Each function is performed on the element flushed out to the channel in order
func ExampleFlashFlood_Push_example10() {
ff := flashflood.New[string](&flashflood.Opts{
BufferAmount: 3,
Timeout: time.Duration(250 * time.Millisecond),
Debug: true,
})
o := getTestObjs(5)
ch, _ := ff.GetChan()
// Convert TestObj to string and apply transformations
f1 := func(objs []string, _ *flashflood.FlashFlood[string]) []string {
var c []string
for _, v := range objs {
// First transformation: add prefix
c = append(c, fmt.Sprintf("---%s", v))
}
return c
}
f2 := func(objs []string, _ *flashflood.FlashFlood[string]) []string {
var c []string
for _, v := range objs {
// Second transformation: remove 'v'
c = append(c, strings.ReplaceAll(v, "v", ""))
}
return c
}
ff.AddFunc(f1)
ff.AddFunc(f2)
// Convert TestObj to strings and push
ff.Push(
fmt.Sprintf("%s_%s", o[0].Key, o[0].Value),
fmt.Sprintf("%s_%s", o[1].Key, o[1].Value),
fmt.Sprintf("%s_%s", o[2].Key, o[2].Value),
fmt.Sprintf("%s_%s", o[3].Key, o[3].Value),
fmt.Sprintf("%s_%s", o[4].Key, o[4].Value),
)
for i := 0; i < 5; i++ {
select {
case v := <-ch:
fmt.Printf("%v\n", v)
}
}
//Output: DEBUG: []string{"---k1_1", "---k2_2"}
//---k1_1
//---k2_2
//DEBUG: []string{"---k3_3", "---k4_4", "---k5_5"}
//---k3_3
//---k4_4
//---k5_5
}
// example to drain elements on channel
func ExampleFlashFlood_Drain_example01() {
ff := flashflood.New[[]byte](&flashflood.Opts{
BufferAmount: 3,
Timeout: time.Duration(100 * time.Millisecond),
GateAmount: 2,
})
b := [][]byte{
{0x1, 0x2, 0x3},
{0x4, 0x5, 0x6},
{0x7, 0x8, 0x9},
{0xa, 0xb, 0xc},
{0xd, 0xe, 0xf},
{0x10, 0x11, 0x12},
}
ch, err := ff.GetChan()
_ = err
ff.Push(b[0], b[1], b[2], b[3], b[4], b[5])
ff.Drain(true, false)
run := true
for run {
select {
case v := <-ch:
fmt.Println("RESULT:", v)
default:
run = false
break
}
}
// Output: RESULT: [1 2 3]
// RESULT: [4 5 6]
// RESULT: [7 8 9]
// RESULT: [10 11 12]
// RESULT: [13 14 15]
// RESULT: [16 17 18]
}
// example to drain elements not using the channel and return them
func ExampleFlashFlood_Drain_example02() {
ff := flashflood.New[[]byte](&flashflood.Opts{
BufferAmount: 3,
Timeout: time.Duration(100 * time.Millisecond),
})
b := [][]byte{
{0x1, 0x2, 0x3},
{0x4, 0x5, 0x6},
{0x7, 0x8, 0x9},
{0xa, 0xb, 0xc},
{0xd, 0xe, 0xf},
{0x10, 0x11, 0x12},
}
ch, err := ff.GetChan()
_ = err
_ = ch
ff.Push(b[0], b[1], b[2], b[3], b[4], b[5])
e, _ := ff.Drain(false, false)
fmt.Println("RESULT:", e)
// Output: RESULT: [[10 11 12] [13 14 15] [16 17 18]]
}