-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransform_test.go
252 lines (208 loc) · 8.75 KB
/
transform_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
package jpipe_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/junitechnology/jpipe"
"github.com/junitechnology/jpipe/item"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/slices"
)
func TestMap(t *testing.T) {
t.Run("Maps values", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3})
mappedChannel := jpipe.Map(channel, func(i int) string { return fmt.Sprintf("%dA", i) })
mappedValues := drainChannel(mappedChannel)
assert.Equal(t, []string{"1A", "2A", "3A"}, mappedValues)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Exits early if context done", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3})
mappedChannel := jpipe.Map(channel, func(i int) string { return fmt.Sprintf("%dA", i) })
goChannel := mappedChannel.ToGoChannel()
readGoChannel(goChannel, 2)
cancelPipeline(pipeline)
assertChannelClosed(t, goChannel, 10*time.Millisecond)
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})
start := time.Now()
mappedChannel := jpipe.Map(channel, func(i int) string {
time.Sleep(100 * time.Millisecond)
return fmt.Sprintf("%dA", i)
}, jpipe.Concurrent(3))
mappedValues := drainChannel(mappedChannel)
elapsed := time.Since(start)
slices.Sort(mappedValues) // The output order with concurrency is unpredictable
assert.Equal(t, []string{"1A", "2A", "3A", "4A", "5A"}, mappedValues)
assert.Less(t, elapsed, 300*time.Millisecond) // It would have taken 500ms serially, but it takes about 200ms with 5 elements and concurrency 3
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Ordered concurrency yields reduced processing times", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3, 4, 5})
start := time.Now()
mappedChannel := jpipe.Map(channel, func(i int) string {
time.Sleep(100 * time.Millisecond)
return fmt.Sprintf("%dA", i)
}, jpipe.Concurrent(3), jpipe.Ordered(3))
mappedValues := drainChannel(mappedChannel)
elapsed := time.Since(start)
assert.Equal(t, []string{"1A", "2A", "3A", "4A", "5A"}, mappedValues)
assert.Less(t, elapsed, 300*time.Millisecond) // It would have taken 500ms serially, but it takes about 200ms with 5 elements and concurrency 3
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Ordered concurrency keeps input order on output", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromRange(pipeline, 1, 1000)
mappedChannel := jpipe.Map(channel, func(i int) string {
time.Sleep(10 * time.Millisecond)
return fmt.Sprintf("%dA", i)
}, jpipe.Concurrent(20), jpipe.Ordered(20))
mappedValues := drainChannel(mappedChannel)
assert.Len(t, mappedValues, 1000)
for i := 0; i < 1000; i++ {
assert.Equal(t, fmt.Sprintf("%dA", i+1), mappedValues[i])
}
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}
func TestFlatMap(t *testing.T) {
t.Run("FlatMaps values", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3})
mappedChannel := jpipe.FlatMap(channel, func(i int) *jpipe.Channel[string] {
return jpipe.FromSlice(pipeline, []string{fmt.Sprintf("%dA", i), fmt.Sprintf("%dB", i)})
})
mappedValues := drainChannel(mappedChannel)
assert.Equal(t, []string{"1A", "1B", "2A", "2B", "3A", "3B"}, mappedValues)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Exits early if context done", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3})
mappedChannel := jpipe.FlatMap(channel, func(i int) *jpipe.Channel[string] {
return jpipe.FromSlice(pipeline, []string{fmt.Sprintf("%dA", i), fmt.Sprintf("%dB", i)})
})
goChannel := mappedChannel.ToGoChannel()
readGoChannel(goChannel, 4)
cancelPipeline(pipeline)
assertChannelClosed(t, goChannel, 10*time.Millisecond)
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})
start := time.Now()
mappedChannel := jpipe.FlatMap(channel, func(i int) *jpipe.Channel[string] {
time.Sleep(100 * time.Millisecond)
return jpipe.FromSlice(pipeline, []string{fmt.Sprintf("%dA", i), fmt.Sprintf("%dB", i)})
}, jpipe.Concurrent(3))
mappedValues := drainChannel(mappedChannel)
elapsed := time.Since(start)
slices.Sort(mappedValues) // The output order with concurrency is unpredictable
assert.Equal(t, []string{"1A", "1B", "2A", "2B", "3A", "3B", "4A", "4B", "5A", "5B"}, mappedValues)
assert.Less(t, elapsed, 300*time.Millisecond) // It would have taken 500ms serially, but it takes about 200ms with 5 elements and concurrency 3
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}
// TODO: Improve this test, the setup is awkward
func TestBatch(t *testing.T) {
t.Run("Batches values based on size only", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
batchedChannel := jpipe.Batch(channel, 3, 0)
batchedValues := drainChannel(batchedChannel)
assert.Equal(t, [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10}}, batchedValues)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Batches values based on time only", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
sourceGoChannel := make(chan int)
channel := jpipe.FromGoChannel(pipeline, sourceGoChannel)
batchedChannel := jpipe.Batch(channel, 0, 100*time.Millisecond)
go func() {
sourceGoChannel <- 1
<-time.After(40 * time.Millisecond) // 40
sourceGoChannel <- 2
<-time.After(20 * time.Millisecond) // 60
sourceGoChannel <- 3
<-time.After(90 * time.Millisecond) // 150
sourceGoChannel <- 4
<-time.After(200 * time.Millisecond) // 350
sourceGoChannel <- 5
<-time.After(30 * time.Millisecond) // 380
close(sourceGoChannel)
}()
batchedValues := drainChannel(batchedChannel)
assert.Equal(t, [][]int{{1, 2, 3}, {4}, {}, {5}}, batchedValues)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Batches values based on size and time", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
sourceGoChannel := make(chan int)
channel := jpipe.FromGoChannel(pipeline, sourceGoChannel)
batchedChannel := jpipe.Batch(channel, 2, 100*time.Millisecond)
go func() {
sourceGoChannel <- 1
<-time.After(40 * time.Millisecond) // 40
sourceGoChannel <- 2
<-time.After(20 * time.Millisecond) // 60
sourceGoChannel <- 3
<-time.After(220 * time.Millisecond) // 280
sourceGoChannel <- 4
<-time.After(20 * time.Millisecond) // 300
close(sourceGoChannel)
}()
batchedValues := drainChannel(batchedChannel)
assert.Equal(t, [][]int{{1, 2}, {3}, {}, {4}}, batchedValues)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Exits early if pipeline canceled", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
sourceGoChannel := make(chan int, 5)
channel := jpipe.FromGoChannel(pipeline, sourceGoChannel)
batchedChannel := jpipe.Batch(channel, 2, 100*time.Millisecond)
go func() {
sourceGoChannel <- 1
<-time.After(40 * time.Millisecond) // 40
sourceGoChannel <- 2
<-time.After(20 * time.Millisecond) // 60
sourceGoChannel <- 3
<-time.After(120 * time.Millisecond) // 180
cancelPipeline(pipeline)
<-time.After(100 * time.Millisecond) // 280
sourceGoChannel <- 4
<-time.After(20 * time.Millisecond) // 300
close(sourceGoChannel)
}()
batchedValues := drainChannel(batchedChannel)
assert.Equal(t, [][]int{{1, 2}, {3}}, batchedValues)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}
func TestWrap(t *testing.T) {
t.Run("Filters values", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3})
wrappedChannel := jpipe.Wrap(channel)
values := drainChannel(wrappedChannel)
assert.Equal(t, []item.Item[int]{{Value: 1}, {Value: 2}, {Value: 3}}, values)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Exits early if pipeline canceled", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, []int{1, 2, 3})
wrappedChannel := jpipe.Wrap(channel)
goChannel := wrappedChannel.ToGoChannel()
readGoChannel(goChannel, 2)
cancelPipeline(pipeline)
assertChannelClosed(t, goChannel, 10*time.Millisecond)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}