-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatachan_test.go
352 lines (305 loc) · 6.39 KB
/
datachan_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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package datachan
import (
"encoding/gob"
"sync"
"testing"
)
func init() {
gob.Register(&kvii{})
}
func generateInts(limit int) chan int {
out := make(chan int)
go func() {
for i := 0; i < limit; i++ {
out <- i
}
close(out)
}()
return out
}
func TestSource(t *testing.T) {
in := generateInts(5)
src := Source(in)
sink := src.Sink(1)
for i := 0; i < 5; i++ {
v := <-sink
if v.(int) != i {
t.Fatal("Expected", i, "got", v)
}
}
}
func TestMap(t *testing.T) {
in := generateInts(5)
src := Source(in)
mapper := src.Map(2, func(v int, output chan<- int) {
output <- v + v
})
sink := mapper.Sink(1)
acc := 0
for i := 0; i < 5; i++ {
acc += (<-sink).(int)
}
if acc != 20 {
t.Fatal("Expected", 20, "got", acc)
}
}
type kvii struct {
Key int
Value int
}
func TestReduce(t *testing.T) {
in := generateInts(5)
flow := Source(in).Map(2, func(v int, output chan<- *kvii) {
output <- &kvii{Key: v % 2, Value: v}
}).Reduce(50,
func(n *kvii) int {
return n.Key
},
func(key int, acc *kvii, value *kvii) *kvii {
acc.Value += value.Value
return acc
}).Sink(1)
acc0 := 0
acc1 := 0
for i := 0; i < 2; i++ {
item := (<-flow).(*kvii)
if item.Key%2 == 0 {
acc0 += item.Value
} else {
acc1 += item.Value
}
}
if acc0 != 6 {
t.Fatal("Expected", 6, "got", acc0)
}
if acc1 != 4 {
t.Fatal("Expected", 4, "got", acc1)
}
_, ok := <-flow
if ok {
t.Fatal("expected closed channel")
}
}
func TestReduceBigger(t *testing.T) {
in := generateInts(256000)
flow := Source(in).Map(8, func(v int, output chan<- *kvii) {
output <- &kvii{Key: v % 2, Value: v}
}).Reduce(16,
func(n *kvii) int {
return n.Key
},
func(key int, acc *kvii, value *kvii) *kvii {
acc.Value += value.Value
return acc
}).Sink(1)
acc0 := 0
acc1 := 0
for i := 0; i < 2; i++ {
item := (<-flow).(*kvii)
if item.Key%2 == 0 {
acc0 += item.Value
} else {
acc1 += item.Value
}
}
var expected0, expected1 int
for i := 0; i < 256000; i++ {
if i%2 == 0 {
expected0 += i
} else {
expected1 += i
}
}
if acc0 != expected0 {
t.Fatal("Expected", expected0, "got", acc0)
}
if acc1 != expected1 {
t.Fatal("Expected", expected1, "got", acc1)
}
_, ok := <-flow
if ok {
t.Fatal("expected closed channel")
}
}
func TestCombiner(t *testing.T) {
limit := 10
spillSize := 3
flow := Source(generateInts(limit)).Map(4, func(v int, output chan<- int) {
output <- v
}).Combiner(spillSize,
func(n int) int {
return n
},
func(key int, acc int, value int) int {
return acc + value
}).Sink(1)
obtained := make([]int, limit)
for i := 0; i < limit; i++ {
item := <-flow
obtained[i] = item.(int)
}
_, ok := <-flow
if ok {
t.Fatal("expected closed channel")
}
}
func TestCombinerSorter(t *testing.T) {
limit := 10
keyer := func(n int) int {
return n
}
flow := Source(generateInts(limit)).Map(4, func(v int, output chan<- int) {
output <- v
}).Combiner(5,
keyer,
func(key int, acc int, value int) int {
return acc + value
}).Sort(4, keyer).Sink(1)
obtained := make([]int, limit)
for i := 0; i < limit; i++ {
item := <-flow
obtained[item.(int)] = item.(int)
}
for i := 0; i < limit; i++ {
if i != obtained[i] {
t.Fatal("Expected item", i, "to be", i, "found", obtained[i])
}
}
_, ok := <-flow
if ok {
t.Fatal("expected closed channel")
}
}
func TestReduceWithBasicSpilling(t *testing.T) {
limit := 10
spillSize := 3
flow := Source(generateInts(limit)).Map(8, func(v int, output chan<- int) {
output <- v
}).Reduce(spillSize,
func(n int) int {
return n
},
func(key int, acc int, value int) int {
return acc + value
}).Sink(1)
obtained := make([]int, limit)
for i := 0; i < limit; i++ {
item := (<-flow).(int)
obtained[i] = item
}
expected := make([]int, limit)
for i := 0; i < limit; i++ {
expected[i%limit] += i
}
for k, v := range obtained {
if expected[k] != v {
t.Fatal("Expected", expected[k], "for key", k, "found", obtained[k], "instead")
}
}
_, ok := <-flow
if ok {
t.Fatal("expected closed channel")
}
}
func TestReduceWithSpilling(t *testing.T) {
limit := 512000
spillSize := 4000
groups := 5000
flow := Source(generateInts(limit)).Map(8, func(v int, output chan<- *kvii) {
output <- &kvii{Key: v % groups, Value: v}
}).Reduce(spillSize,
func(n *kvii) int {
return n.Key
},
func(key int, acc *kvii, value *kvii) *kvii {
acc.Value += value.Value
return acc
}).Sink(1)
obtained := make([]int, groups)
for i := 0; i < groups; i++ {
item := (<-flow).(*kvii)
//if int(item.Key.(int)) != i {
// t.Fatal("Key", i, "expected to be", i, "found", item.Key.(int), "instead")
//}
obtained[i] = item.Value
}
expected := make([]int, groups)
for i := 0; i < limit; i++ {
expected[i%groups] += i
}
for k, v := range expected {
if obtained[k] != v {
t.Fatal("Expected", v, "for key", k, "found", obtained[k], "instead")
}
}
_, ok := <-flow
if ok {
t.Fatal("expected closed channel")
}
}
func TestTee(t *testing.T) {
total := 1000
f1, f2 := Source(generateInts(total)).Tee()
wg := sync.WaitGroup{}
checker := func(id string, s *Stage) {
sink := s.Sink(1)
i := 0
for value := range sink {
if i != value {
t.Fatal(id, "expected", i, "(first time) received", value)
}
i++
}
if i < total {
t.Fatal("Expected more values, got just", i)
}
wg.Done()
}
wg.Add(2)
go checker("first", f1)
go checker("second", f2)
wg.Wait()
}
func TestTeeMerge(t *testing.T) {
limit := 100
f1, f2 := Source(generateInts(100)).Tee()
final := f1.Merge(f2)
var totalSum int
for iv := range final.Sink(1) {
totalSum += iv.(int)
}
if totalSum != 2*(limit*(limit-1)/2) {
t.Fatal("Expected sum ", 2*(limit*(limit-1)/2), "got", totalSum)
}
}
func TestStage_Filter(t *testing.T) {
limit := 10000
flow := Source(generateInts(limit)).Filter(10, func(value int) bool {
return value%2 == 0
}).Sink(1)
acc := 0
for value := range flow {
if value.(int)%2 == 1 {
t.Fatal("Odd values should have been filtered away, found", value)
} else {
acc += value.(int)
}
}
expected := 0
for i := 0; i < limit; i++ {
if i%2 == 0 {
expected += i
}
}
if acc != expected {
t.Fatal("Sum expected to be", expected, "got", acc)
}
}
func TestStage_Count(t *testing.T) {
limit := 10000
flow := Source(generateInts(limit)).Count()
processed := int(<-flow)
if limit != processed {
t.Fatal("Expected ", limit, "items, counted", processed)
}
}