aboutsummaryrefslogtreecommitdiff
path: root/pipeline/batch_test.go
blob: 4d79ddca3561e60f0a6452377752caa10809d8b2 (plain)
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
package pipeline

import (
	"context"
	"testing"
	"time"
)

func TestBatch_Full(t *testing.T) {
	t.Parallel()

	input := make(chan int, 10)
	for i := 1; i <= 10; i++ {
		input <- i
	}
	close(input)

	out := Batch(t.Context(), input, 3, time.Second)

	var batches [][]int
	for b := range out {
		batches = append(batches, b)
	}

	if len(batches) != 4 {
		t.Fatalf("expected 4 batches, got %d: %v", len(batches), batches)
	}
	if len(batches[0]) != 3 {
		t.Fatalf("expected first batch size 3, got %d", len(batches[0]))
	}
	if len(batches[3]) != 1 {
		t.Fatalf("expected last batch size 1, got %d", len(batches[3]))
	}
}

func TestBatch_TimeoutFlush(t *testing.T) {
	t.Parallel()

	input := make(chan int)

	out := Batch(t.Context(), input, 100, 50*time.Millisecond)

	input <- 1
	input <- 2

	select {
	case batch := <-out:
		if len(batch) != 2 {
			t.Fatalf("expected batch of 2 from timeout flush, got %d", len(batch))
		}
	case <-time.After(time.Second):
		t.Fatal("timeout waiting for batch flush")
	}

	close(input)
	for range out {
	}
}

func TestBatch_ContextCancel(t *testing.T) {
	t.Parallel()
	ctx, cancel := context.WithCancel(t.Context())

	input := make(chan int)
	out := Batch(ctx, input, 100, time.Second)

	cancel()

	done := make(chan struct{})
	go func() {
		for range out {
		}
		close(done)
	}()

	select {
	case <-done:
	case <-time.After(time.Second):
		t.Fatal("timeout waiting for shutdown")
	}
}

func TestBatch_NilInput(t *testing.T) {
	t.Parallel()

	defer func() {
		if r := recover(); r == nil {
			t.Error("expected panic for nil input")
		}
	}()

	Batch[int](t.Context(), nil, 10, time.Second)
}

func TestBatch_PanicOnZeroSize(t *testing.T) {
	t.Parallel()

	defer func() {
		if r := recover(); r == nil {
			t.Error("expected panic for batch size < 1")
		}
	}()

	input := make(chan int)
	Batch(t.Context(), input, 0, time.Second)
}