-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathbatch_test.go
89 lines (72 loc) · 2.15 KB
/
batch_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
package flow_test
import (
"fmt"
"testing"
"time"
ext "github.com/reugn/go-streams/extension"
"github.com/reugn/go-streams/flow"
"github.com/reugn/go-streams/internal/assert"
)
func TestBatch(t *testing.T) {
in := make(chan any)
out := make(chan any)
source := ext.NewChanSource(in)
batch := flow.NewBatch[string](4, 40*time.Millisecond)
sink := ext.NewChanSink(out)
assert.NotEqual(t, batch.Out(), nil)
inputValues := []string{"a", "b", "c", "d", "e", "f", "g"}
go func() {
for _, e := range inputValues {
ingestDeferred(e, in, 5*time.Millisecond)
}
}()
go ingestDeferred("h", in, 90*time.Millisecond)
go closeDeferred(in, 100*time.Millisecond)
go func() {
source.
Via(batch).
To(sink)
}()
outputValues := readSlice[[]string](sink.Out)
fmt.Println(outputValues)
assert.Equal(t, 3, len(outputValues)) // [[a b c d] [e f g] [h]]
assert.Equal(t, []string{"a", "b", "c", "d"}, outputValues[0])
assert.Equal(t, []string{"e", "f", "g"}, outputValues[1])
assert.Equal(t, []string{"h"}, outputValues[2])
}
func TestBatch_Ptr(t *testing.T) {
in := make(chan any)
out := make(chan any)
source := ext.NewChanSource(in)
batch := flow.NewBatch[*string](4, 40*time.Millisecond)
sink := ext.NewChanSink(out)
assert.NotEqual(t, batch.Out(), nil)
inputValues := ptrSlice([]string{"a", "b", "c", "d", "e", "f", "g"})
go func() {
for _, e := range inputValues {
ingestDeferred(e, in, 5*time.Millisecond)
}
}()
go ingestDeferred(ptr("h"), in, 90*time.Millisecond)
go closeDeferred(in, 100*time.Millisecond)
go func() {
source.
Via(batch).
Via(flow.NewPassThrough()). // Via coverage
To(sink)
}()
outputValues := readSlice[[]*string](sink.Out)
fmt.Println(outputValues)
assert.Equal(t, 3, len(outputValues)) // [[a b c d] [e f g] [h]]
assert.Equal(t, ptrSlice([]string{"a", "b", "c", "d"}), outputValues[0])
assert.Equal(t, ptrSlice([]string{"e", "f", "g"}), outputValues[1])
assert.Equal(t, ptrSlice([]string{"h"}), outputValues[2])
}
func TestBatch_InvalidArguments(t *testing.T) {
assert.Panics(t, func() {
flow.NewBatch[string](0, time.Second)
})
assert.Panics(t, func() {
flow.NewBatch[string](-1, time.Second)
})
}