-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_pipelines_test.go
265 lines (232 loc) · 8.77 KB
/
util_pipelines_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package pipelines
import (
"context"
"sync"
"testing"
)
func TestOrDone(t *testing.T) {
t.Run("when the inStream has a nil value, then the method should panic", func(t *testing.T) {
defer func() {
if perr := recover(); perr == nil {
t.Error("expected the method to panic")
}
}()
_ = OrDone[string](context.Background(), nil)
})
t.Run("when we cancel the context before the OrDone pipeline, we should receive a closed channel with less than one item in there", func(t *testing.T) {
list := []string{"something", "nother", "another"}
ctx, cancel := context.WithCancel(context.Background())
inStream := GenerateFromSlice(ctx, list)
cancel()
outStream := OrDone(ctx, inStream)
expectStreamLengthToBeLessThan(2, outStream, t)
expectClosedChannel(true, outStream, t)
})
t.Run("when we pass a single item into the stream, we should receive a single item back", func(t *testing.T) {
list := []string{"something"}
ctx := context.Background()
outStream := OrDone(ctx, GenerateFromSlice(ctx, list))
expectStreamLengthToBe(len(list), outStream, t)
expectClosedChannel(true, outStream, t)
})
t.Run("when we pass multiple values into the stream, we should receive the same number of values back", func(t *testing.T) {
list := []string{"something", "nother", "another"}
ctx := context.Background()
outStream := OrDone(ctx, GenerateFromSlice(ctx, list))
expectStreamLengthToBe(len(list), outStream, t)
expectClosedChannel(true, outStream, t)
})
t.Run("when we pass multiple values into the stream, we should receive those same values back", func(t *testing.T) {
list := []string{"something", "nother", "another"}
ctx := context.Background()
outStream := OrDone(ctx, GenerateFromSlice(ctx, list))
expectOrderedResultsList(list, outStream, t)
expectClosedChannel(true, outStream, t)
})
}
func TestTeeSplitter(t *testing.T) {
t.Run("when the inStream has a nil value, then the method should panic", func(t *testing.T) {
defer func() {
if perr := recover(); perr == nil {
t.Error("expected the method to panic")
}
}()
_, _ = TeeSplitter[string](context.Background(), nil)
})
t.Run("when we cancel the context before the TeeSplitter pipeline, we should receive 2 closed channel with containing less than one item", func(t *testing.T) {
list := []string{"something", "nother", "another"}
ctx, cancel := context.WithCancel(context.Background())
inStream := GenerateFromSlice(ctx, list)
cancel()
o1, o2 := TeeSplitter(ctx, inStream)
cnts := countAllStreamLengths(o1, o2)
for _, cnt := range cnts {
if cnt > 2 {
t.Errorf("expected less than one item but got %d", cnt)
}
}
})
t.Run("when we provide an empty stream, we should recieve 2 empty closed streams", func(t *testing.T) {
list := []string{}
ctx := context.Background()
o1, o2 := TeeSplitter(ctx, GenerateFromSlice(ctx, list))
expectStreamLengthToBe(len(list), o1, t)
expectStreamLengthToBe(len(list), o2, t)
expectClosedChannel(true, o1, t)
expectClosedChannel(true, o2, t)
})
t.Run("when we pass a stream that contains a single item, we should recieve 2 closed streams containing a single item", func(t *testing.T) {
list := []string{"hello"}
ctx := context.Background()
o1, o2 := TeeSplitter(ctx, GenerateFromSlice(ctx, list))
cnts := countAllStreamLengths(o1, o2)
if cnts[0] != len(list) || cnts[0] != cnts[1] {
t.Errorf("expected both stream length to be %d but stream 1 length was %d and stream 2 length was %d", len(list), cnts[0], cnts[1])
}
expectClosedChannel(true, o1, t)
expectClosedChannel(true, o2, t)
})
t.Run("when we pass a single value into the stream, we should recieve 2 identical, closed streams containing that same single value", func(t *testing.T) {
list := []string{"hello"}
ctx := context.Background()
o1, o2 := TeeSplitter(ctx, GenerateFromSlice(ctx, list))
var idx int
for val1 := range o1 {
if val2 := <-o2; val1 != list[idx] || val1 != val2 {
t.Errorf("expected value to be %s but stream1 = %s and stream2 = %s", list[idx], val1, val2)
}
idx++
}
expectClosedChannel(true, o1, t)
expectClosedChannel(true, o2, t)
})
t.Run("when we pass a multiple values into the stream, we should recieve 2 identical, closed streams containing those same values", func(t *testing.T) {
list := []string{"hello", "hi", "bonjour", "salut"}
ctx := context.Background()
o1, o2 := TeeSplitter(ctx, GenerateFromSlice(ctx, list))
var idx int
for val1 := range o1 {
if val2 := <-o2; val1 != list[idx] || val1 != val2 {
t.Errorf("expected value to be %s but stream1 = %s and stream2 = %s", list[idx], val1, val2)
}
idx++
}
expectClosedChannel(true, o1, t)
expectClosedChannel(true, o2, t)
})
}
func countAllStreamLengths[T any](streams ...(<-chan T)) []int {
wg := sync.WaitGroup{}
wg.Add(len(streams))
counters := make([]int, len(streams))
for idx, stream := range streams {
go func(idx int, stream <-chan T) {
defer wg.Done()
for range stream {
counters[idx]++
}
}(idx, stream)
}
wg.Wait()
return counters
}
func TestCombine(t *testing.T) {
t.Run("when a single stream with a nil value has been passed, we should return an empty closed channel", func(t *testing.T) {
ctx := context.Background()
outStream := Combine[string](ctx, nil)
expectStreamLengthToBe(0, outStream, t)
expectClosedChannel(true, outStream, t)
})
t.Run("when no streams are passed, we should return an empty closed channel", func(t *testing.T) {
ctx := context.Background()
outStream := Combine[string](ctx)
expectStreamLengthToBe(0, outStream, t)
expectClosedChannel(true, outStream, t)
})
t.Run("when a single empty stream has been passed, we should return an empty closed channel", func(t *testing.T) {
ctx := context.Background()
outStream := Combine(ctx, GenerateFromSlice(ctx, []string{}))
expectStreamLengthToBe(0, outStream, t)
expectClosedChannel(true, outStream, t)
})
t.Run("when the context is cancelled before the pipeline, we should return a closed channel", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
a := []string{"hello"}
b := []string{"bonjour"}
streamA := GenerateFromSlice(ctx, a)
streamB := GenerateFromSlice(ctx, b)
cancel()
outStream := Combine(ctx, streamA, streamB)
expectStreamLengthToBeLessThan(1, outStream, t)
expectClosedChannel(true, outStream, t)
})
t.Run("when a one of the streams has a nil value, it is ignored and only values from the other streams are returned", func(t *testing.T) {
list := []string{"hello"}
ctx := context.Background()
stream1 := GenerateFromSlice(ctx, list)
var stream2 chan string
outStream := Combine(ctx, stream1, stream2)
expectStreamLengthToBe(len(list), outStream, t)
expectClosedChannel(true, outStream, t)
})
t.Run("when we provide many streams with a single value, the resluting channel length should be the sum of all these streams content", func(t *testing.T) {
a := []string{"hello"}
b := []string{"bonjour"}
c := []string{"guten tag"}
len := len(a) + len(b) + len(c)
ctx := context.Background()
streamA := GenerateFromSlice(ctx, a)
streamB := GenerateFromSlice(ctx, b)
streamC := GenerateFromSlice(ctx, c)
outStream := Combine(ctx, streamA, streamB, streamC)
expectStreamLengthToBe(len, outStream, t)
expectClosedChannel(true, outStream, t)
})
t.Run("when we provide many streams with multiple values, the resluting channel length should be the sum of all these streams content", func(t *testing.T) {
a := []string{"apple", "pear", "grape"}
b := []string{"chicken"}
c := []string{"brocolli", "cheese", "turnip"}
d := []string{}
len := len(a) + len(b) + len(c)
ctx := context.Background()
streamA := GenerateFromSlice(ctx, a)
streamB := GenerateFromSlice(ctx, b)
streamC := GenerateFromSlice(ctx, c)
streamD := GenerateFromSlice(ctx, d)
outStream := Combine(ctx, streamA, streamB, streamC, streamD)
expectStreamLengthToBe(len, outStream, t)
expectClosedChannel(true, outStream, t)
})
t.Run("when we provide many streams with multiple values, the resluting channel should contain all these values", func(t *testing.T) {
a := []string{"apple", "pear", "grape"}
b := []string{"chicken", "peanuts"}
ctx := context.Background()
outStream := Combine(ctx, GenerateFromSlice(ctx, a), GenerateFromSlice(ctx, b))
outList := make([]string, len(a)+len(b))
var idx int
for val := range outStream {
outList[idx] = val
idx++
}
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
defer wg.Done()
for _, v := range a {
if idx := getIndexOf(v, outList); idx < 0 {
t.Errorf("could not find %s in outList", v)
}
}
}()
go func() {
defer wg.Done()
for _, v := range a {
if idx := getIndexOf(v, outList); idx < 0 {
t.Errorf("could not find %s in outList", v)
}
}
}()
wg.Wait()
expectClosedChannel(true, outStream, t)
})
}