1
1
package flow_test
2
2
3
3
import (
4
- "container/heap"
5
- "fmt"
6
4
"reflect"
7
5
"sort"
8
6
"strings"
@@ -11,7 +9,6 @@ import (
11
9
12
10
ext "github.com/reugn/go-streams/extension"
13
11
"github.com/reugn/go-streams/flow"
14
- "github.com/reugn/go-streams/util"
15
12
)
16
13
17
14
var addAsterisk = func (in string ) []string {
@@ -31,7 +28,6 @@ var reduceSum = func(a int, b int) int {
31
28
32
29
func ingestSlice [T any ](source []T , in chan interface {}) {
33
30
for _ , e := range source {
34
- fmt .Printf ("ingest: %v" , e )
35
31
in <- e
36
32
}
37
33
}
@@ -53,26 +49,23 @@ func TestComplexFlow(t *testing.T) {
53
49
source := ext .NewChanSource (in )
54
50
toUpperMapFlow := flow .NewMap (strings .ToUpper , 1 )
55
51
appendAsteriskFlatMapFlow := flow .NewFlatMap (addAsterisk , 1 )
56
- throttler := flow .NewThrottler (10 , time .Second , 50 , flow .Backpressure )
57
- slidingWindow := flow .NewSlidingWindow (2 * time .Second , 2 * time .Second )
58
- tumblingWindow := flow .NewTumblingWindow (time .Second )
59
- filterNotContainsA := flow .NewFilter (filterNotContainsA , 1 )
52
+ throttler := flow .NewThrottler (10 , 200 * time .Millisecond , 50 , flow .Backpressure )
53
+ tumblingWindow := flow .NewTumblingWindow (200 * time .Millisecond )
54
+ filterFlow := flow .NewFilter (filterNotContainsA , 1 )
60
55
sink := ext .NewChanSink (out )
61
56
62
57
inputValues := []string {"a" , "b" , "c" }
63
58
go ingestSlice (inputValues , in )
64
- go closeDeferred (in , 3 * time .Second )
59
+ go closeDeferred (in , time .Second )
65
60
66
61
go func () {
67
62
source .
68
63
Via (toUpperMapFlow ).
69
64
Via (appendAsteriskFlatMapFlow ).
70
65
Via (tumblingWindow ).
71
66
Via (flow .Flatten (1 )).
72
- Via (slidingWindow ).
73
67
Via (throttler ).
74
- Via (flow .Flatten (1 )).
75
- Via (filterNotContainsA ).
68
+ Via (filterFlow ).
76
69
To (sink )
77
70
}()
78
71
@@ -85,42 +78,41 @@ func TestComplexFlow(t *testing.T) {
85
78
assertEquals (t , expectedValues , outputValues )
86
79
}
87
80
88
- func TestFanOutFlow (t * testing.T ) {
89
- in := make (chan interface {})
90
- out := make (chan interface {})
81
+ func TestSplitFlow (t * testing.T ) {
82
+ in := make (chan interface {}, 3 )
83
+ out := make (chan interface {}, 3 )
91
84
92
85
source := ext .NewChanSource (in )
93
- filterNotContainsA := flow .NewFilter (filterNotContainsA , 1 )
94
86
toUpperMapFlow := flow .NewMap (strings .ToUpper , 1 )
95
87
sink := ext .NewChanSink (out )
96
88
97
89
inputValues := []string {"a" , "b" , "c" }
98
- go ingestSlice (inputValues , in )
99
- go closeDeferred (in , 100 * time . Millisecond )
90
+ ingestSlice (inputValues , in )
91
+ close (in )
100
92
101
- go func () {
102
- fanOut := flow . FanOut ( source .Via ( filterNotContainsA ). Via ( toUpperMapFlow ), 2 )
103
- flow .
104
- Merge ( fanOut ... ).
105
- To ( sink )
106
- }( )
93
+ split := flow . Split (
94
+ source .
95
+ Via ( toUpperMapFlow ), filterNotContainsA )
96
+
97
+ flow . Merge ( split [ 0 ], split [ 1 ]).
98
+ To ( sink )
107
99
108
100
var outputValues []string
109
101
for e := range sink .Out {
110
102
outputValues = append (outputValues , e .(string ))
111
103
}
112
104
sort .Strings (outputValues )
113
105
114
- expectedValues := []string {"B " , "B" , "C " , "C" }
106
+ expectedValues := []string {"A " , "B" , "C" }
115
107
assertEquals (t , expectedValues , outputValues )
116
108
}
117
109
118
- func TestRoundRobinFlow (t * testing.T ) {
110
+ func TestFanOutFlow (t * testing.T ) {
119
111
in := make (chan interface {})
120
112
out := make (chan interface {})
121
113
122
114
source := ext .NewChanSource (in )
123
- filterNotContainsA := flow .NewFilter (filterNotContainsA , 1 )
115
+ filterFlow := flow .NewFilter (filterNotContainsA , 1 )
124
116
toUpperMapFlow := flow .NewMap (strings .ToUpper , 1 )
125
117
sink := ext .NewChanSink (out )
126
118
@@ -129,9 +121,12 @@ func TestRoundRobinFlow(t *testing.T) {
129
121
go closeDeferred (in , 100 * time .Millisecond )
130
122
131
123
go func () {
132
- roundRobin := flow .RoundRobin (source .Via (filterNotContainsA ).Via (toUpperMapFlow ), 2 )
124
+ fanOut := flow .FanOut (
125
+ source .
126
+ Via (filterFlow ).
127
+ Via (toUpperMapFlow ), 2 )
133
128
flow .
134
- Merge (roundRobin ... ).
129
+ Merge (fanOut ... ).
135
130
To (sink )
136
131
}()
137
132
@@ -141,39 +136,41 @@ func TestRoundRobinFlow(t *testing.T) {
141
136
}
142
137
sort .Strings (outputValues )
143
138
144
- expectedValues := []string {"B" , "C" }
139
+ expectedValues := []string {"B" , "B" , "C" , " C" }
145
140
assertEquals (t , expectedValues , outputValues )
146
141
}
147
142
148
- func TestSessionWindow (t * testing.T ) {
143
+ func TestRoundRobinFlow (t * testing.T ) {
149
144
in := make (chan interface {})
150
145
out := make (chan interface {})
151
146
152
147
source := ext .NewChanSource (in )
153
- sessionWindow := flow .NewSessionWindow (200 * time .Millisecond )
148
+ filterFlow := flow .NewFilter (filterNotContainsA , 1 )
149
+ toUpperMapFlow := flow .NewMap (strings .ToUpper , 1 )
154
150
sink := ext .NewChanSink (out )
155
151
156
152
inputValues := []string {"a" , "b" , "c" }
157
153
go ingestSlice (inputValues , in )
158
- go ingestDeferred ("d" , in , 300 * time .Millisecond )
159
- go ingestDeferred ("e" , in , 700 * time .Millisecond )
160
- go closeDeferred (in , time .Second )
154
+ go closeDeferred (in , 100 * time .Millisecond )
161
155
162
156
go func () {
163
- source .
164
- Via (sessionWindow ).
157
+ roundRobin := flow .RoundRobin (
158
+ source .
159
+ Via (filterFlow ).
160
+ Via (toUpperMapFlow ), 2 )
161
+ flow .
162
+ Merge (roundRobin ... ).
165
163
To (sink )
166
164
}()
167
165
168
- var outputValues [][] interface {}
166
+ var outputValues []string
169
167
for e := range sink .Out {
170
- outputValues = append (outputValues , e .([] interface {} ))
168
+ outputValues = append (outputValues , e .(string ))
171
169
}
170
+ sort .Strings (outputValues )
172
171
173
- assertEquals (t , 3 , len (outputValues ))
174
- assertEquals (t , 3 , len (outputValues [0 ]))
175
- assertEquals (t , 1 , len (outputValues [1 ]))
176
- assertEquals (t , 1 , len (outputValues [2 ]))
172
+ expectedValues := []string {"B" , "C" }
173
+ assertEquals (t , expectedValues , outputValues )
177
174
}
178
175
179
176
func TestReduceFlow (t * testing.T ) {
@@ -201,19 +198,6 @@ func TestReduceFlow(t *testing.T) {
201
198
assertEquals (t , expectedValues , outputValues )
202
199
}
203
200
204
- func TestQueue (t * testing.T ) {
205
- queue := & flow.PriorityQueue {}
206
- heap .Push (queue , flow .NewItem (1 , util .NowNano (), 0 ))
207
- heap .Push (queue , flow .NewItem (2 , 1234 , 0 ))
208
- heap .Push (queue , flow .NewItem (3 , util .NowNano (), 0 ))
209
- queue .Swap (0 , 1 )
210
- head := queue .Head ()
211
- queue .Update (head , util .NowNano ())
212
- first := heap .Pop (queue ).(* flow.Item )
213
-
214
- assertEquals (t , 2 , first .Msg .(int ))
215
- }
216
-
217
201
func assertEquals [T any ](t * testing.T , expected T , actual T ) {
218
202
if ! reflect .DeepEqual (expected , actual ) {
219
203
t .Fatalf ("%v != %v" , expected , actual )
0 commit comments