-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathtumbling_window_test.go
77 lines (61 loc) · 1.89 KB
/
tumbling_window_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
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 TestTumblingWindow(t *testing.T) {
in := make(chan any)
out := make(chan any)
source := ext.NewChanSource(in)
tumblingWindow := flow.NewTumblingWindow[string](50 * time.Millisecond)
sink := ext.NewChanSink(out)
assert.NotEqual(t, tumblingWindow.Out(), nil)
go func() {
inputValues := []string{"a", "b", "c", "d", "e", "f", "g"}
for _, v := range inputValues {
ingestDeferred(v, in, 15*time.Millisecond)
}
closeDeferred(in, 160*time.Millisecond)
}()
go func() {
source.
Via(tumblingWindow).
To(sink)
}()
outputValues := readSlice[[]string](sink.Out)
fmt.Println(outputValues)
assert.Equal(t, 3, len(outputValues)) // [[a b c] [d e f] [g]]
assert.Equal(t, []string{"a", "b", "c"}, outputValues[0])
assert.Equal(t, []string{"d", "e", "f"}, outputValues[1])
assert.Equal(t, []string{"g"}, outputValues[2])
}
func TestTumblingWindow_Ptr(t *testing.T) {
in := make(chan any)
out := make(chan any)
source := ext.NewChanSource(in)
tumblingWindow := flow.NewTumblingWindow[*string](50 * time.Millisecond)
sink := ext.NewChanSink(out)
assert.NotEqual(t, tumblingWindow.Out(), nil)
go func() {
inputValues := ptrSlice([]string{"a", "b", "c", "d", "e", "f", "g"})
for _, v := range inputValues {
ingestDeferred(v, in, 15*time.Millisecond)
}
closeDeferred(in, 160*time.Millisecond)
}()
go func() {
source.
Via(tumblingWindow).
To(sink)
}()
outputValues := readSlice[[]*string](sink.Out)
fmt.Println(outputValues)
assert.Equal(t, 3, len(outputValues)) // [[a b c] [d e f] [g]]
assert.Equal(t, ptrSlice([]string{"a", "b", "c"}), outputValues[0])
assert.Equal(t, ptrSlice([]string{"d", "e", "f"}), outputValues[1])
assert.Equal(t, ptrSlice([]string{"g"}), outputValues[2])
}