aboutsummaryrefslogtreecommitdiff
path: root/pipeline/batch_test.go
diff options
context:
space:
mode:
authorsangtran <tranquangsang12.7@gmail.com>2026-09-03 23:10:33 +0700
committersangtran <tranquangsang12.7@gmail.com>2026-09-03 23:10:33 +0700
commit02123df8cc15896f97c1580e49d8e2a991e37ddd (patch)
treed0c00bc3f65e5f5493a9dff8034c80640cb12960 /pipeline/batch_test.go
signatureVerified sangtran <tranquangsang12.7@gmail.com> Key: 8DD79823D235E1BB ultimate trust
downloadquacker-main.tar.gz
quacker-main.zip
Verified Moving all legacy quacker source code from github to cgitHEADmain
Diffstat (limited to 'pipeline/batch_test.go')
-rw-r--r--pipeline/batch_test.go106
1 files changed, 106 insertions, 0 deletions
diff --git a/pipeline/batch_test.go b/pipeline/batch_test.go
new file mode 100644
index 0000000..4d79ddc
--- /dev/null
+++ b/pipeline/batch_test.go
@@ -0,0 +1,106 @@
1package pipeline
2
3import (
4 "context"
5 "testing"
6 "time"
7)
8
9func TestBatch_Full(t *testing.T) {
10 t.Parallel()
11
12 input := make(chan int, 10)
13 for i := 1; i <= 10; i++ {
14 input <- i
15 }
16 close(input)
17
18 out := Batch(t.Context(), input, 3, time.Second)
19
20 var batches [][]int
21 for b := range out {
22 batches = append(batches, b)
23 }
24
25 if len(batches) != 4 {
26 t.Fatalf("expected 4 batches, got %d: %v", len(batches), batches)
27 }
28 if len(batches[0]) != 3 {
29 t.Fatalf("expected first batch size 3, got %d", len(batches[0]))
30 }
31 if len(batches[3]) != 1 {
32 t.Fatalf("expected last batch size 1, got %d", len(batches[3]))
33 }
34}
35
36func TestBatch_TimeoutFlush(t *testing.T) {
37 t.Parallel()
38
39 input := make(chan int)
40
41 out := Batch(t.Context(), input, 100, 50*time.Millisecond)
42
43 input <- 1
44 input <- 2
45
46 select {
47 case batch := <-out:
48 if len(batch) != 2 {
49 t.Fatalf("expected batch of 2 from timeout flush, got %d", len(batch))
50 }
51 case <-time.After(time.Second):
52 t.Fatal("timeout waiting for batch flush")
53 }
54
55 close(input)
56 for range out {
57 }
58}
59
60func TestBatch_ContextCancel(t *testing.T) {
61 t.Parallel()
62 ctx, cancel := context.WithCancel(t.Context())
63
64 input := make(chan int)
65 out := Batch(ctx, input, 100, time.Second)
66
67 cancel()
68
69 done := make(chan struct{})
70 go func() {
71 for range out {
72 }
73 close(done)
74 }()
75
76 select {
77 case <-done:
78 case <-time.After(time.Second):
79 t.Fatal("timeout waiting for shutdown")
80 }
81}
82
83func TestBatch_NilInput(t *testing.T) {
84 t.Parallel()
85
86 defer func() {
87 if r := recover(); r == nil {
88 t.Error("expected panic for nil input")
89 }
90 }()
91
92 Batch[int](t.Context(), nil, 10, time.Second)
93}
94
95func TestBatch_PanicOnZeroSize(t *testing.T) {
96 t.Parallel()
97
98 defer func() {
99 if r := recover(); r == nil {
100 t.Error("expected panic for batch size < 1")
101 }
102 }()
103
104 input := make(chan int)
105 Batch(t.Context(), input, 0, time.Second)
106}