11package flow_test
22
33import (
4- "container/heap"
5- "fmt"
64 "reflect"
75 "sort"
86 "strings"
@@ -11,7 +9,6 @@ import (
119
1210 ext "github.com/reugn/go-streams/extension"
1311 "github.com/reugn/go-streams/flow"
14- "github.com/reugn/go-streams/util"
1512)
1613
1714var addAsterisk = func (in string ) []string {
@@ -31,7 +28,6 @@ var reduceSum = func(a int, b int) int {
3128
3229func ingestSlice [T any ](source []T , in chan interface {}) {
3330 for _ , e := range source {
34- fmt .Printf ("ingest: %v" , e )
3531 in <- e
3632 }
3733}
@@ -53,26 +49,23 @@ func TestComplexFlow(t *testing.T) {
5349 source := ext .NewChanSource (in )
5450 toUpperMapFlow := flow .NewMap (strings .ToUpper , 1 )
5551 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 )
6055 sink := ext .NewChanSink (out )
6156
6257 inputValues := []string {"a" , "b" , "c" }
6358 go ingestSlice (inputValues , in )
64- go closeDeferred (in , 3 * time .Second )
59+ go closeDeferred (in , time .Second )
6560
6661 go func () {
6762 source .
6863 Via (toUpperMapFlow ).
6964 Via (appendAsteriskFlatMapFlow ).
7065 Via (tumblingWindow ).
7166 Via (flow .Flatten (1 )).
72- Via (slidingWindow ).
7367 Via (throttler ).
74- Via (flow .Flatten (1 )).
75- Via (filterNotContainsA ).
68+ Via (filterFlow ).
7669 To (sink )
7770 }()
7871
@@ -85,42 +78,41 @@ func TestComplexFlow(t *testing.T) {
8578 assertEquals (t , expectedValues , outputValues )
8679}
8780
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 )
9184
9285 source := ext .NewChanSource (in )
93- filterNotContainsA := flow .NewFilter (filterNotContainsA , 1 )
9486 toUpperMapFlow := flow .NewMap (strings .ToUpper , 1 )
9587 sink := ext .NewChanSink (out )
9688
9789 inputValues := []string {"a" , "b" , "c" }
98- go ingestSlice (inputValues , in )
99- go closeDeferred (in , 100 * time . Millisecond )
90+ ingestSlice (inputValues , in )
91+ close (in )
10092
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 )
10799
108100 var outputValues []string
109101 for e := range sink .Out {
110102 outputValues = append (outputValues , e .(string ))
111103 }
112104 sort .Strings (outputValues )
113105
114- expectedValues := []string {"B " , "B" , "C " , "C" }
106+ expectedValues := []string {"A " , "B" , "C" }
115107 assertEquals (t , expectedValues , outputValues )
116108}
117109
118- func TestRoundRobinFlow (t * testing.T ) {
110+ func TestFanOutFlow (t * testing.T ) {
119111 in := make (chan interface {})
120112 out := make (chan interface {})
121113
122114 source := ext .NewChanSource (in )
123- filterNotContainsA := flow .NewFilter (filterNotContainsA , 1 )
115+ filterFlow := flow .NewFilter (filterNotContainsA , 1 )
124116 toUpperMapFlow := flow .NewMap (strings .ToUpper , 1 )
125117 sink := ext .NewChanSink (out )
126118
@@ -129,9 +121,12 @@ func TestRoundRobinFlow(t *testing.T) {
129121 go closeDeferred (in , 100 * time .Millisecond )
130122
131123 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 )
133128 flow .
134- Merge (roundRobin ... ).
129+ Merge (fanOut ... ).
135130 To (sink )
136131 }()
137132
@@ -141,39 +136,41 @@ func TestRoundRobinFlow(t *testing.T) {
141136 }
142137 sort .Strings (outputValues )
143138
144- expectedValues := []string {"B" , "C" }
139+ expectedValues := []string {"B" , "B" , "C" , " C" }
145140 assertEquals (t , expectedValues , outputValues )
146141}
147142
148- func TestSessionWindow (t * testing.T ) {
143+ func TestRoundRobinFlow (t * testing.T ) {
149144 in := make (chan interface {})
150145 out := make (chan interface {})
151146
152147 source := ext .NewChanSource (in )
153- sessionWindow := flow .NewSessionWindow (200 * time .Millisecond )
148+ filterFlow := flow .NewFilter (filterNotContainsA , 1 )
149+ toUpperMapFlow := flow .NewMap (strings .ToUpper , 1 )
154150 sink := ext .NewChanSink (out )
155151
156152 inputValues := []string {"a" , "b" , "c" }
157153 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 )
161155
162156 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 ... ).
165163 To (sink )
166164 }()
167165
168- var outputValues [][] interface {}
166+ var outputValues []string
169167 for e := range sink .Out {
170- outputValues = append (outputValues , e .([] interface {} ))
168+ outputValues = append (outputValues , e .(string ))
171169 }
170+ sort .Strings (outputValues )
172171
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 )
177174}
178175
179176func TestReduceFlow (t * testing.T ) {
@@ -201,19 +198,6 @@ func TestReduceFlow(t *testing.T) {
201198 assertEquals (t , expectedValues , outputValues )
202199}
203200
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-
217201func assertEquals [T any ](t * testing.T , expected T , actual T ) {
218202 if ! reflect .DeepEqual (expected , actual ) {
219203 t .Fatalf ("%v != %v" , expected , actual )
0 commit comments