-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsink_test.go
332 lines (262 loc) · 9.42 KB
/
sink_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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package jpipe_test
import (
"context"
"sync"
"testing"
"time"
"github.com/junitechnology/jpipe"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/slices"
)
func TestForEach(t *testing.T) {
t.Run("Processes all values in the channel", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3})
values := []int{}
<-channel.ForEach(func(value int) {
values = append(values, value)
})
assert.Equal(t, []int{1, 2, 3}, values)
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Exits early on pipeline canceled", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3})
values := []int{}
<-channel.ForEach(func(i int) {
if i == 2 {
cancelPipeline(pipeline)
}
values = append(values, i)
})
assert.Equal(t, []int{1, 2}, values)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Concurrency yields reduced processing times", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
values := []int{}
lock := sync.Mutex{}
start := time.Now()
<-channel.ForEach(func(value int) {
time.Sleep(100 * time.Millisecond)
lock.Lock()
values = append(values, value)
lock.Unlock()
}, jpipe.Concurrent(5))
elapsed := time.Since(start)
slices.Sort(values) // The output order with concurrency is unpredictable
assert.Equal(t, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, values)
assert.Less(t, elapsed, 400*time.Millisecond) // It would have taken 1000ms serially, but it takes about 200ms with 10 elements and concurrency 5
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}
func TestReduce(t *testing.T) {
t.Run("Reduces all values", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3})
result := <-jpipe.Reduce(channel, func(acc int64, value int) int64 { return acc + int64(value) })
assert.Equal(t, int64(6), result)
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Exits early on pipeline canceled", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
goChannel := make(chan int)
channel := jpipe.FromGoChannel(pipeline, goChannel)
resultChannel := jpipe.Reduce(channel, func(acc int64, value int) int64 { return acc + int64(value) })
goChannel <- 1
goChannel <- 2
time.Sleep(time.Millisecond) // Give time for the last value to be processed
cancelPipeline(pipeline)
result := <-resultChannel
assert.Equal(t, int64(3), result)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}
func TestCount(t *testing.T) {
t.Run("Counts all values", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{10, 20, 30})
result := <-channel.Count()
assert.Equal(t, int64(3), result)
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Exits early on pipeline canceled", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
goChannel := make(chan int)
channel := jpipe.FromGoChannel(pipeline, goChannel)
resultChannel := channel.Count()
goChannel <- 10
goChannel <- 20
time.Sleep(time.Millisecond) // Give time for the last value to be processed
cancelPipeline(pipeline)
result := <-resultChannel
assert.Equal(t, int64(2), result)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}
func TestToSlice(t *testing.T) {
slice := []int{1, 2, 3}
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, slice)
actual := <-channel.ToSlice()
assert.Equal(t, slice, actual)
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
}
func TestToMap(t *testing.T) {
t.Run("Converts to map keeping first values", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{11, 42, 31, 22, 73})
actual := <-jpipe.ToMap(channel, func(i int) int { return i % 10 }, jpipe.KeepFirst())
assert.Equal(t, map[int]int{1: 11, 2: 42, 3: 73}, actual)
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Converts to map keeping last values", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{11, 42, 31, 22, 73})
actual := <-jpipe.ToMap(channel, func(i int) int { return i % 10 }, jpipe.KeepLast())
assert.Equal(t, map[int]int{1: 31, 2: 22, 3: 73}, actual)
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}
func TestLast(t *testing.T) {
t.Run("Returns last value", func(t *testing.T) {
slice := []int{1, 2, 3}
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, slice)
last := <-channel.Last()
assert.Equal(t, 3, last)
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Returned channel closes if no value seen", func(t *testing.T) {
slice := []int{}
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, slice)
_, open := <-channel.Last()
assert.False(t, open)
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}
func TestAny(t *testing.T) {
t.Run("Returns true when any value passes the predicate", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
goChannel := make(chan int)
channel := jpipe.FromGoChannel(pipeline, goChannel)
resultChannel := channel.Any(func(i int) bool { return i > 3 })
goChannel <- 1
goChannel <- 2
goChannel <- 3
assertChannelOpenButNoValue(t, resultChannel, 10*time.Millisecond)
goChannel <- 4
result := <-resultChannel
assert.True(t, result)
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Returns false if no value passed the predicate", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3})
result := <-channel.Any(func(i int) bool { return i > 3 })
assert.False(t, result)
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Exits early on pipeline canceled", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
goChannel := make(chan int)
channel := jpipe.FromGoChannel(pipeline, goChannel)
go func() {
goChannel <- 1
goChannel <- 2
cancelPipeline(pipeline)
}()
result := <-channel.Any(func(i int) bool { return i > 3 })
assert.False(t, result)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}
func TestAll(t *testing.T) {
t.Run("Returns false when any value fails the predicate", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
goChannel := make(chan int)
channel := jpipe.FromGoChannel(pipeline, goChannel)
resultChannel := channel.All(func(i int) bool { return i <= 3 })
goChannel <- 1
goChannel <- 2
goChannel <- 3
assertChannelOpenButNoValue(t, resultChannel, 10*time.Millisecond)
goChannel <- 4
result := <-resultChannel
assert.False(t, result)
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Returns true if all values passed the predicate", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3})
result := <-channel.Any(func(i int) bool { return i <= 3 })
assert.True(t, result)
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Exits early on pipeline canceled", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
goChannel := make(chan int)
channel := jpipe.FromGoChannel(pipeline, goChannel)
go func() {
goChannel <- 1
goChannel <- 2
cancelPipeline(pipeline)
}()
result := <-channel.Any(func(i int) bool { return i <= 3 })
assert.True(t, result)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}
func TestNone(t *testing.T) {
t.Run("Returns false when some value passes the predicate", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
goChannel := make(chan int)
channel := jpipe.FromGoChannel(pipeline, goChannel)
resultChannel := channel.None(func(i int) bool { return i > 3 })
goChannel <- 1
goChannel <- 2
goChannel <- 3
assertChannelOpenButNoValue(t, resultChannel, 10*time.Millisecond)
goChannel <- 4
result := <-resultChannel
assert.False(t, result)
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Returns true no value passed the predicate", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3})
result := <-channel.None(func(i int) bool { return i > 3 })
assert.True(t, result)
assert.NoError(t, pipeline.Error())
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Exits early on pipeline canceled", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
goChannel := make(chan int)
channel := jpipe.FromGoChannel(pipeline, goChannel)
go func() {
goChannel <- 1
goChannel <- 2
cancelPipeline(pipeline)
}()
result := <-channel.None(func(i int) bool { return i > 3 })
assert.True(t, result)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}