-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathsession_window_test.go
106 lines (83 loc) · 2.72 KB
/
session_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
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 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 TestSessionWindow(t *testing.T) {
in := make(chan any)
out := make(chan any)
source := ext.NewChanSource(in)
sessionWindow := flow.NewSessionWindow[string](20 * time.Millisecond)
sink := ext.NewChanSink(out)
assert.NotEqual(t, sessionWindow.Out(), nil)
inputValues := []string{"a", "b", "c"}
go ingestSlice(inputValues, in)
go ingestDeferred("d", in, 30*time.Millisecond)
go ingestDeferred("e", in, 70*time.Millisecond)
go closeDeferred(in, 100*time.Millisecond)
go func() {
source.
Via(sessionWindow).
To(sink)
}()
outputValues := readSlice[[]string](sink.Out)
fmt.Println(outputValues)
assert.Equal(t, 3, len(outputValues)) // [[a b c] [d] [e]]
assert.Equal(t, []string{"a", "b", "c"}, outputValues[0])
assert.Equal(t, []string{"d"}, outputValues[1])
assert.Equal(t, []string{"e"}, outputValues[2])
}
func TestSessionWindow_Long(t *testing.T) {
in := make(chan any)
out := make(chan any)
source := ext.NewChanSource(in)
sessionWindow := flow.NewSessionWindow[string](20 * time.Millisecond)
sink := ext.NewChanSink(out)
inputValues := []string{"a", "b", "c", "d", "e", "f", "g"}
go func() {
for _, e := range inputValues {
ingestDeferred(e, in, 10*time.Millisecond)
}
}()
go ingestDeferred("h", in, 140*time.Millisecond)
go closeDeferred(in, 150*time.Millisecond)
go func() {
source.
Via(sessionWindow).
To(sink)
}()
outputValues := readSlice[[]string](sink.Out)
fmt.Println(outputValues)
assert.Equal(t, 2, len(outputValues)) // [[a b c d e f g] [h]]
assert.Equal(t, []string{"a", "b", "c", "d", "e", "f", "g"}, outputValues[0])
assert.Equal(t, []string{"h"}, outputValues[1])
}
func TestSessionWindow_Ptr(t *testing.T) {
in := make(chan any)
out := make(chan any)
source := ext.NewChanSource(in)
sessionWindow := flow.NewSessionWindow[*string](20 * time.Millisecond)
sink := ext.NewChanSink(out)
assert.NotEqual(t, sessionWindow.Out(), nil)
inputValues := ptrSlice([]string{"a", "b", "c"})
go ingestSlice(inputValues, in)
go ingestDeferred(ptr("d"), in, 30*time.Millisecond)
go ingestDeferred(ptr("e"), in, 70*time.Millisecond)
go closeDeferred(in, 100*time.Millisecond)
go func() {
source.
Via(sessionWindow).
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]]
assert.Equal(t, ptrSlice([]string{"a", "b", "c"}), outputValues[0])
assert.Equal(t, ptrSlice([]string{"d"}), outputValues[1])
assert.Equal(t, ptrSlice([]string{"e"}), outputValues[2])
}