-
Notifications
You must be signed in to change notification settings - Fork 35
/
slice_test.go
1490 lines (1409 loc) · 42.3 KB
/
slice_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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package bigslice_test
import (
"bufio"
"bytes"
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"reflect"
"runtime"
"sort"
"strings"
"sync"
"testing"
"testing/quick"
"text/tabwriter"
fuzz "github.com/google/gofuzz"
"github.com/grailbio/base/errors"
"github.com/grailbio/base/log"
"github.com/grailbio/bigmachine/rpc"
"github.com/grailbio/bigmachine/testsystem"
"github.com/grailbio/bigslice"
"github.com/grailbio/bigslice/exec"
"github.com/grailbio/bigslice/metrics"
"github.com/grailbio/bigslice/sliceio"
"github.com/grailbio/bigslice/slicetest"
"github.com/grailbio/bigslice/typecheck"
)
func init() {
log.AddFlags() // so they can be used in tests
}
func sortColumns(columns []reflect.Value) {
s := new(columnSlice)
s.keys = columns[0].Interface().([]string)
s.swappers = make([]func(i, j int), len(columns))
for i := range columns {
s.swappers[i] = reflect.Swapper(columns[i].Interface())
}
sort.Stable(s)
}
type columnSlice struct {
keys []string
swappers []func(i, j int)
}
func (c columnSlice) Len() int { return len(c.keys) }
func (c columnSlice) Less(i, j int) bool { return c.keys[i] < c.keys[j] }
func (c columnSlice) Swap(i, j int) {
for _, swap := range c.swappers {
swap(i, j)
}
}
var executors = map[string]exec.Option{
"Local": exec.Local,
"Bigmachine.Test": exec.Bigmachine(testsystem.New()),
}
func run(ctx context.Context, t *testing.T, slice bigslice.Slice) map[string]*sliceio.Scanner {
t.Helper()
scannerErrs := runError(ctx, t, slice)
scanners := make(map[string]*sliceio.Scanner, len(scannerErrs))
for name, scannerErr := range scannerErrs {
if err := scannerErr.Err; err != nil {
t.Errorf("executor %s error %v", name, err)
} else {
scanners[name] = scannerErr.Scanner
}
}
return scanners
}
type scannerErr struct {
*sliceio.Scanner
Err error
}
func runError(ctx context.Context, t *testing.T, slice bigslice.Slice) map[string]scannerErr {
t.Helper()
results := make(map[string]scannerErr)
fn := bigslice.Func(func() bigslice.Slice { return slice })
for name, opt := range executors {
if testing.Short() && name != "Local" {
continue
}
sess := exec.Start(opt)
// TODO(marius): faster teardown in bigmachine so that we can call this here.
// defer sess.Shutdown()
res, err := sess.Run(ctx, fn)
results[name] = scannerErr{res.Scanner(), err}
}
return results
}
func assertColumnsEqual(t *testing.T, sort bool, columns ...interface{}) {
t.Helper()
if len(columns)%2 != 0 {
t.Fatal("must pass even number of columns")
}
numColumns := len(columns) / 2
if numColumns < 1 {
t.Fatal("must have at least one column to compare")
}
gotCols := make([]reflect.Value, numColumns)
wantCols := make([]reflect.Value, numColumns)
for i := range columns {
j := i / 2
if i%2 == 0 {
gotCols[j] = reflect.ValueOf(columns[i])
if gotCols[j].Kind() != reflect.Slice {
t.Errorf("column %d of actual must be a slice", j)
return
}
if j > 0 && gotCols[j].Len() != gotCols[j-1].Len() {
t.Errorf("got %d, want %d columns in actual", gotCols[j].Len(), gotCols[j-1].Len())
return
}
} else {
// Problems with our expected columns are fatal, as that means that
// the test itself is incorrectly constructed.
wantCols[j] = reflect.ValueOf(columns[i])
if wantCols[j].Kind() != reflect.Slice {
t.Fatalf("column %d of expected must be a slice", j)
}
if j > 0 && wantCols[j].Len() != wantCols[j-1].Len() {
t.Fatalf("got %d, want %d columns in expected", wantCols[j].Len(), wantCols[j-1].Len())
}
}
}
if sort {
sortColumns(gotCols)
sortColumns(wantCols)
}
switch got, want := gotCols[0].Len(), wantCols[0].Len(); {
case got == want:
case got < want:
t.Errorf("short result: got %v, want %v", got, want)
return
case want < got:
row := make([]string, len(gotCols))
for i := range row {
row[i] = fmt.Sprint(gotCols[i].Index(want).Interface())
}
// Show one row of extra values to help debug.
t.Errorf("extra values: %v", strings.Join(row, ","))
}
// wantCols[0].Len() <= gotCols[0].Len() so we compare wantCols[0].Len()
// rows.
numRows := wantCols[0].Len()
got := make([]interface{}, numColumns)
want := make([]interface{}, numColumns)
for i := 0; i < numColumns; i++ {
got[i] = gotCols[i].Interface()
want[i] = wantCols[i].Interface()
}
if !reflect.DeepEqual(got, want) {
// Print full rows for small results. They are easier to interpret
// than diffs.
if numRows < 10 && numColumns < 10 {
var (
gotRows = make([]string, numRows)
wantRows = make([]string, numRows)
)
for i := range gotRows {
var (
got = make([]string, numColumns)
want = make([]string, numColumns)
)
for j := range got {
got[j] = fmt.Sprint(gotCols[j].Index(i).Interface())
want[j] = fmt.Sprint(wantCols[j].Index(i).Interface())
}
gotRows[i] = strings.Join(got, " ")
wantRows[i] = strings.Join(want, " ")
}
t.Errorf("result mismatch:\ngot:\n%s\nwant:\n%s", strings.Join(gotRows, "\n"), strings.Join(wantRows, "\n"))
return
}
// Print as columns
var b bytes.Buffer
var tw tabwriter.Writer
tw.Init(&b, 4, 4, 1, ' ', 0)
for i := 0; i < numRows; i++ {
var diff bool
row := make([]string, numColumns)
for j := range row {
got := gotCols[j].Index(i).Interface()
want := wantCols[j].Index(i).Interface()
if !reflect.DeepEqual(got, want) {
diff = true
row[j] = fmt.Sprintf("%v->%v", want, got)
} else {
row[j] = fmt.Sprint(got)
}
}
if diff {
fmt.Fprintf(&tw, "[%d] %s\n", i, strings.Join(row, "\t"))
}
}
tw.Flush()
t.Errorf("result mismatch:\n%s", b.String())
}
}
func assertEqual(t *testing.T, slice bigslice.Slice, sort bool, expect ...interface{}) {
if !testing.Short() {
rpc.InjectFailures = true
defer func() { rpc.InjectFailures = false }()
}
t.Helper()
for name, s := range run(context.Background(), t, slice) {
t.Run(name, func(t *testing.T) {
defer s.Close()
args := make([]interface{}, len(expect))
for i := range args {
// Make this one larger to make sure we exhaust the scanner.
v := reflect.ValueOf(expect[i])
slice := reflect.MakeSlice(v.Type(), v.Len()+1, v.Len()+1)
args[i] = slice.Interface()
}
n, ok := s.Scanv(context.Background(), args...)
if ok {
t.Errorf("%s: long read (%d)", name, n)
}
if err := s.Err(); err != nil {
t.Errorf("%s: %v", name, err)
return
}
for i := range args {
args[i] = reflect.ValueOf(args[i]).Slice(0, n).Interface()
}
columns := make([]interface{}, len(expect)*2)
for i := range expect {
columns[i*2] = args[i]
columns[i*2+1] = expect[i]
}
assertColumnsEqual(t, sort, columns...)
})
}
}
func expectTypeError(t *testing.T, message string, fn func()) {
t.Helper()
typecheck.TestCalldepth = 2
_, file, line, ok := runtime.Caller(1)
if !ok {
t.Fatal("runtime.Caller error")
}
defer func() {
t.Helper()
typecheck.TestCalldepth = 0
e := recover()
if e == nil {
t.Fatal("expected error")
}
err, ok := e.(*typecheck.Error)
if !ok {
t.Fatalf("expected typeError, got %T", e)
}
if got, want := err.File, file; got != want {
t.Errorf("got %v, want %v", got, want)
}
if got, want := err.Line, line; got != want {
t.Errorf("got %v, want %v", got, want)
}
if got, want := err.Err.Error(), message; got != want {
t.Errorf("got %q, want %q", got, want)
}
}()
fn()
}
type genNshard int
func (genNshard) Generate(rand *rand.Rand, size int) reflect.Value {
// The number of shards must be >= 1 (guaranteed by constSlice
// construction).
return reflect.ValueOf(genNshard(rand.Intn(size) + 1))
}
type genNrow int
func (genNrow) Generate(rand *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(genNrow(rand.Intn(size)))
}
// TestConstShard verifies that the algorithm used to shard const slice data
// behaves properly. The algorithm must provide shards that: cover the entire
// data set, are within the bounds of data, are mutually exclusive, and are
// distributed evenly.
func TestConstShard(t *testing.T) {
f := func(gN genNrow, gNshard genNshard) bool {
var (
n = int(gN)
nshard = int(gNshard)
covered = make([]bool, n)
offsets = make([]int, nshard)
counts = make([]int, nshard)
)
for shard := 0; shard < nshard; shard++ {
offset, count := bigslice.ConstShard(n, nshard, shard)
for i := offset; i < offset+count; i++ {
if i < 0 || i >= n {
// Out of bounds of data.
return false
}
if covered[i] {
// Already covered by another shard.
return false
}
covered[i] = true
}
offsets[shard] = offset
offsets[shard] = count
}
for _, c := range covered {
if !c {
// Data element that was part of no shard.
return false
}
}
if nshard == 0 {
return true
}
var (
minCount = counts[0]
maxCount = counts[0]
)
for _, c := range counts {
if c < minCount {
minCount = c
}
if c > maxCount {
maxCount = c
}
}
// Check even distribution.
return maxCount-minCount <= 1
}
// Some known edge cases.
for _, c := range []struct {
n int
nshard int
}{
{0, 0},
{10, 10}, // Equal rows and shards.
{30, 10}, // Even multiple of shards.
{1, 10}, // More shards than elements.
} {
name := fmt.Sprintf("n:%d nshard:%d", c.n, c.nshard)
t.Run(name, func(t *testing.T) {
if !f(genNrow(c.n), genNshard(c.nshard)) {
t.Errorf("misbehaves")
}
})
}
// Random cases.
t.Run("Quick", func(t *testing.T) {
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
})
}
func TestConst(t *testing.T) {
const N = 10000
fz := fuzz.New()
fz.NilChance(0)
fz.NumElements(N, N)
var (
col1 []string
col2 []int
)
fz.Fuzz(&col1)
fz.Fuzz(&col2)
for nshards := 1; nshards < 20; nshards++ {
slice := bigslice.Const(nshards, col1, col2)
assertEqual(t, slice, true, col1, col2)
}
}
func TestConstError(t *testing.T) {
expectTypeError(t, "const: invalid slice inputs", func() { bigslice.Const(1, 123) })
}
func TestReaderFunc(t *testing.T) {
const (
N = 10000
Nshard = 10
)
type state struct {
*fuzz.Fuzzer
total int
}
slice := bigslice.ReaderFunc(Nshard, func(shard int, state *state, strings []string, ints []int) (n int, err error) {
// The input should be zerod by bigslice.
var nnonzero int
for i := range strings {
if strings[i] != "" || ints[i] != 0 {
nnonzero++
}
}
if nnonzero > 0 {
t.Errorf("%d (of %d) nonzero rows", nnonzero, len(strings))
}
if state.Fuzzer == nil {
state.Fuzzer = fuzz.New()
}
state.NumElements(1, len(strings))
var (
fstrings []string
fints []int
)
state.Fuzz(&fstrings)
state.Fuzz(&fints)
n = copy(strings, fstrings)
m := copy(ints, fints)
if m < n {
n = m
}
state.total += n
if state.total >= N {
return n - (state.total - N), sliceio.EOF
}
return n, nil
})
// Map everything to the same key so we can count them.
slice = bigslice.Map(slice, func(s string, i int) (key string, count int) { return "", 1 })
slice = bigslice.Fold(slice, func(a, e int) int { return a + e })
assertEqual(t, slice, false, []string{""}, []int{N * Nshard})
}
func TestReaderFuncError(t *testing.T) {
expectTypeError(t, "readerfunc: invalid reader function type func()", func() { bigslice.ReaderFunc(1, func() {}) })
expectTypeError(t, "readerfunc: invalid reader function type string", func() { bigslice.ReaderFunc(1, "invalid") })
expectTypeError(t, "readerfunc: invalid reader function type func(string, string, []int) (int, error)", func() { bigslice.ReaderFunc(1, func(shard string, state string, x []int) (int, error) { panic("") }) })
expectTypeError(t, "readerfunc: function func(int, string, []int) error does not return (int, error)", func() { bigslice.ReaderFunc(1, func(shard int, state string, x []int) error { panic("") }) })
expectTypeError(t, "readerfunc: invalid reader function type func(int, string) (int, error)", func() { bigslice.ReaderFunc(1, func(shard int, state string) (int, error) { panic("") }) })
}
const readerFuncForgetEOFMessage = "warning: reader func returned empty vector"
// TestReaderFuncForgetEOF runs a buggy ReaderFunc that never returns sliceio.EOF. We check that
// bigslice prints a warning.
func TestReaderFuncForgetEOF(t *testing.T) {
var logOut bytes.Buffer
log.SetOutput(&logOut)
const N = 500
slice := bigslice.ReaderFunc(1, func(_ int, state *int, _ []int) (int, error) {
// Simulate an empty input. Users should return sliceio.EOF immediately, but some forget
// and just return nil. Eventually return EOF so the test terminates.
if *state >= N {
return 0, sliceio.EOF
}
*state++
return 0, nil
})
assertEqual(t, slice, false, []int{})
if !strings.Contains(logOut.String(), readerFuncForgetEOFMessage) {
t.Errorf("expected empty vector log message, got: %q", logOut.String())
}
}
// TestReaderFuncNoForgetEOF complements TestReaderFuncForgetEOF, testing that no spurious log
// messages are written if reader funcs return non-empty vectors.
func TestReaderFuncNoForgetEOF(t *testing.T) {
var logOut bytes.Buffer
log.SetOutput(&logOut)
const N = 500
slice := bigslice.ReaderFunc(1, func(_ int, state *int, out []int) (int, error) {
// Simulate an empty input. Users should return sliceio.EOF immediately, but some forget
// and just return nil. Eventually return EOF so the test terminates.
if *state >= N {
return 0, sliceio.EOF
}
*state++
return 1, nil
})
assertEqual(t, slice, false, make([]int, N))
if strings.Contains(logOut.String(), readerFuncForgetEOFMessage) {
t.Errorf("expected no empty vector log message, got: %q", logOut.String())
}
}
// TestWriterFunc tests the basic functionality of WriterFunc, verifying that
// all data is passed to the write function, and all data is available in the
// resulting slice.
func TestWriterFunc(t *testing.T) {
const (
N = 10000
Nshard = 10
)
fz := fuzz.New()
fz.NilChance(0)
fz.NumElements(N, N)
var (
col1 []string
col2 []int
)
fz.Fuzz(&col1)
fz.Fuzz(&col2)
slice := bigslice.Const(Nshard, col1, col2)
type state struct {
col1 []string
col2 []int
errs []error
}
var (
writerMutex sync.Mutex
// The states of the writers, by shard.
writerStates []state
)
slice = bigslice.WriterFunc(slice,
func(shard int, state *state, err error, col1 []string, col2 []int) error {
state.col1 = append(state.col1, col1...)
state.col2 = append(state.col2, col2...)
state.errs = append(state.errs, err)
if err != nil {
writerMutex.Lock()
defer writerMutex.Unlock()
writerStates[shard] = *state
}
return nil
})
// We expect both the columns written by the writer func and the columns in
// the resulting slice to match the input. We make a copy to avoid
// disturbing the inputs, as we'll end up sorting these to compare them.
wantCol1 := append([]string{}, col1...)
wantCol2 := append([]int{}, col2...)
ctx := context.Background()
fn := bigslice.Func(func() bigslice.Slice { return slice })
for name, opt := range executors {
t.Run(name, func(t *testing.T) {
// Each execution starts with a fresh state for the writer.
writerStates = make([]state, Nshard)
sess := exec.Start(opt)
res, err := sess.Run(ctx, fn)
if err != nil {
t.Errorf("executor %s error %v", name, err)
return
}
// Check the columns in the output slice.
scanner := res.Scanner()
defer scanner.Close()
var (
s string
i int
resCol1 []string
resCol2 []int
)
for scanner.Scan(context.Background(), &s, &i) {
resCol1 = append(resCol1, s)
resCol2 = append(resCol2, i)
}
assertColumnsEqual(t, true, resCol1, wantCol1, resCol2, wantCol2)
// Check the columns written by the writer func.
var (
writerCol1 []string
writerCol2 []int
)
for _, state := range writerStates {
writerCol1 = append(writerCol1, state.col1...)
writerCol2 = append(writerCol2, state.col2...)
}
assertColumnsEqual(t, true, writerCol1, wantCol1, writerCol2, wantCol2)
// Check that errors were passed as expected to the writer func.
for shard, state := range writerStates {
if len(state.errs) < 1 {
t.Errorf("writer for shard %d did not get EOF", shard)
continue
}
for i := 0; i < len(state.errs)-1; i++ {
if state.errs[i] != nil {
// Only the last error received should be non-nil.
t.Errorf("got premature error")
break
}
}
if got, want := state.errs[len(state.errs)-1], sliceio.EOF; got != want {
t.Errorf("got %v, want %v", got, want)
}
}
})
}
}
// TestWriterFuncBadFunc tests the type-checking of the writer func passed to
// WriterFunc.
func TestWriterFuncBadFunc(t *testing.T) {
for _, c := range []struct {
name string
message string
f interface{}
}{
{
"String",
"writerfunc: invalid writer function type string; must be func(shard int, state stateType, err error, col1 []string, col2 []int) error",
"I'm not a function at all",
},
{
"NoArguments",
"writerfunc: invalid writer function type func(); must be func(shard int, state stateType, err error, col1 []string, col2 []int) error",
func() {},
},
{
"NonSliceColumn",
"writerfunc: invalid writer function type func(int, int, error, string, []int) error; must be func(shard int, state stateType, err error, col1 []string, col2 []int) error",
func(shard int, state int, err error, col1 string, col2 []int) error { panic("") },
},
{
"NotEnoughColumns",
"writerfunc: invalid writer function type func(int, int, error, []string) error; must be func(shard int, state stateType, err error, col1 []string, col2 []int) error",
func(shard int, state int, err error, col1 []string) error { panic("") },
},
{
"TooManyColumns",
"writerfunc: invalid writer function type func(int, int, error, []string, []int, []int) error; must be func(shard int, state stateType, err error, col1 []string, col2 []int) error",
func(shard int, state int, err error, col1 []string, col2 []int, col3 []int) error { panic("") },
},
{
"StringShard",
"writerfunc: invalid writer function type func(string, int, error, []string, []int) error; must be func(shard int, state stateType, err error, col1 []string, col2 []int) error",
func(shard string, state int, err error, col1 []string, col2 []int) error { panic("") },
},
{
"WrongColumnElementType",
"writerfunc: invalid writer function type func(int, int, error, []string, []string) error; must be func(shard int, state stateType, err error, col1 []string, col2 []int) error",
func(shard int, state int, err error, col1 []string, col2 []string) error { panic("") },
},
{
"NoReturn",
"writerfunc: invalid writer function type func(int, int, error, []string, []int); must return error",
func(shard int, state int, err error, col1 []string, col2 []int) { panic("") },
},
{
"ReturnInt",
"writerfunc: invalid writer function type func(int, int, error, []string, []int) int; must return error",
func(shard int, state int, err error, col1 []string, col2 []int) int { panic("") },
},
} {
t.Run(c.name, func(t *testing.T) {
slice := bigslice.Const(1, []string{}, []int{})
expectTypeError(t, c.message, func() { bigslice.WriterFunc(slice, c.f) })
})
}
}
// TestWriterFuncError tests the behavior of WriterFunc under various error
// conditions.
func TestWriterFuncError(t *testing.T) {
assertWriterErr := func(t *testing.T, slice bigslice.Slice) {
fn := bigslice.Func(func() bigslice.Slice { return slice })
for name, opt := range executors {
t.Run(name, func(t *testing.T) {
sess := exec.Start(opt)
_, err := sess.Run(context.Background(), fn)
if err == nil {
t.Errorf("expected error")
} else {
if got, want := err.Error(), "writerError"; !strings.Contains(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}
})
}
}
// The write function always returns an error, so we should see it.
t.Run("WriteAlwaysErr", func(t *testing.T) {
slice := bigslice.Const(2, []string{"a", "b", "c", "d"})
slice = bigslice.WriterFunc(slice, func(shard int, state int, err error, col1 []string) error {
return errors.New("writerError")
})
assertWriterErr(t, slice)
})
// The write function returns an error when it sees the EOF. We expect to
// see the returned error, even though the underlying read succeeded
// without error.
t.Run("WriteErrOnEOF", func(t *testing.T) {
slice := bigslice.Const(2, []string{"a", "b", "c", "d"})
slice = bigslice.WriterFunc(slice, func(shard int, state int, err error, col1 []string) error {
if err == sliceio.EOF {
return errors.New("writerError")
}
return nil
})
assertWriterErr(t, slice)
})
}
func TestMap(t *testing.T) {
const N = 100000
input := make([]int, N)
output := make([]string, N)
for i := range input {
input[i] = i
output[i] = fmt.Sprint(i)
}
slice := bigslice.Const(1, input)
slice = bigslice.Map(slice, func(i int) string { return fmt.Sprint(i) })
assertEqual(t, slice, false, output)
}
func TestMapError(t *testing.T) {
input := bigslice.Const(1, []string{"x", "y"})
expectTypeError(t, "map: invalid map function int", func() { bigslice.Map(input, 123) })
expectTypeError(t, "map: function func(int) string does not match input slice type slice[1]string", func() { bigslice.Map(input, func(x int) string { return "" }) })
expectTypeError(t, "map: function func(int, int) string does not match input slice type slice[1]string", func() { bigslice.Map(input, func(x, y int) string { return "" }) })
expectTypeError(t, "map: need at least one output column", func() { bigslice.Map(input, func(x string) {}) })
}
func TestFilter(t *testing.T) {
const N = 100000
input := make([]int, N)
output := make([]int, N/2)
for i := range input {
input[i] = i
if i%2 == 0 {
output[i/2] = i
}
}
slice := bigslice.Const(N/1000, input)
slice = bigslice.Filter(slice, func(i int) bool { return i%2 == 0 })
assertEqual(t, slice, false, output)
slice = bigslice.Const(1, input)
slice = bigslice.Filter(slice, func(i int) bool { return false })
assertEqual(t, slice, false, []int{})
slice = bigslice.Const(1, input)
slice = bigslice.Filter(slice, func(i int) bool {
switch i {
case N / 4, N / 2, 3 * N / 4:
return true
default:
return false
}
})
assertEqual(t, slice, false, []int{N / 4, N / 2, 3 * N / 4})
}
func TestFilterError(t *testing.T) {
input := bigslice.Const(1, []string{"x", "y"})
expectTypeError(t, "filter: invalid predicate function int", func() { bigslice.Filter(input, 123) })
expectTypeError(t, "filter: function func(int) bool does not match input slice type slice[1]string", func() { bigslice.Filter(input, func(x int) bool { return false }) })
expectTypeError(t, "filter: function func(int, int) string does not match input slice type slice[1]string", func() { bigslice.Filter(input, func(x, y int) string { return "" }) })
expectTypeError(t, "filter: predicate must return a single boolean value", func() { bigslice.Filter(input, func(x string) {}) })
expectTypeError(t, "filter: predicate must return a single boolean value", func() { bigslice.Filter(input, func(x string) int { return 0 }) })
expectTypeError(t, "filter: predicate must return a single boolean value", func() { bigslice.Filter(input, func(x string) (bool, int) { return false, 0 }) })
}
func TestFlatmap(t *testing.T) {
slice := bigslice.Const(2, []string{"x,x", "y,y,y", "z", "", "x"})
slice = bigslice.Flatmap(slice, func(s string) []string {
if s == "" {
return nil
}
return strings.Split(s, ",")
})
assertEqual(t, slice, true, []string{"x", "x", "x", "y", "y", "y", "z"})
// Multiple columns
slice = bigslice.Flatmap(slice, func(s string) ([]string, []int) {
return []string{s}, []int{len(s)}
})
assertEqual(t, slice, true,
[]string{"x", "x", "x", "y", "y", "y", "z"},
[]int{1, 1, 1, 1, 1, 1, 1},
)
// Filter everything
slice = bigslice.Flatmap(slice, func(s string, i int) []string {
return nil
})
assertEqual(t, slice, true, []string{})
// Partial filter
slice = bigslice.Const(1, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
slice = bigslice.Flatmap(slice, func(i int) []int {
if i%2 == 0 {
return []int{i}
}
return nil
})
assertEqual(t, slice, false, []int{0, 2, 4, 6, 8, 10})
// Large slices
input := make([]string, 1024*10)
for i := range input {
input[i] = fmt.Sprint(i)
}
slice = bigslice.Const(5, input)
slice = bigslice.Flatmap(slice, func(s string) []string {
switch s {
case "1024":
return []string{s}
case "5000":
return []string{s}
default:
return nil
}
})
assertEqual(t, slice, true, []string{"1024", "5000"})
}
func TestFlatmapBuffered(t *testing.T) {
zeros := make([]int, 1025)
slice := bigslice.Const(1, []int{0})
slice = bigslice.Flatmap(slice, func(i int) []int {
return zeros
})
// Drive it manually:
assertEqual(t, slice, false, zeros)
}
func TestFlatmapError(t *testing.T) {
input := bigslice.Const(1, []int{1, 2, 3})
expectTypeError(t, "flatmap: invalid flatmap function int", func() { bigslice.Flatmap(input, 123) })
expectTypeError(t, "flatmap: flatmap function func(string) []int does not match input slice type slice[1]int", func() { bigslice.Flatmap(input, func(s string) []int { return nil }) })
expectTypeError(t, "flatmap: flatmap function func(int) int is not vectorized", func() { bigslice.Flatmap(input, func(i int) int { return 0 }) })
expectTypeError(t, "flatmap: flatmap function func(int, int) []int does not match input slice type slice[1]int", func() { bigslice.Flatmap(input, func(i, j int) []int { return nil }) })
}
func TestFold(t *testing.T) {
const N = 10000
fz := fuzz.New()
fz.NilChance(0)
fz.NumElements(N/2, N/2)
var (
keys []string
values []int
)
fz.Fuzz(&keys)
fz.Fuzz(&values)
keys = append(keys, keys...)
values = append(values, values...)
slice := bigslice.Const(N/1000, keys, values)
slice = bigslice.Fold(slice, func(a, e int) int { return a + e })
expect := make(map[string]int)
for i, key := range keys {
expect[key] += values[i]
}
var (
expectKeys []string
expectValues []int
)
for key, value := range expect {
expectKeys = append(expectKeys, key)
expectValues = append(expectValues, value)
}
assertEqual(t, slice, true, expectKeys, expectValues)
// Make sure we can partition other element types also.
slice = bigslice.Const(N/1000, values, keys)
slice = bigslice.Fold(slice, func(a int, e string) int { return a + len(e) })
slice = bigslice.Map(slice, func(key, count int) (int, int) { return 0, count })
slice = bigslice.Fold(slice, func(a, e int) int { return a + e })
var totalSize int
for _, key := range keys {
totalSize += len(key)
}
assertEqual(t, slice, false, []int{0}, []int{totalSize})
}
func TestFoldError(t *testing.T) {
input := bigslice.Const(1, []int{1, 2, 3})
floatInput := bigslice.Map(input, func(x int) (float64, int) { return 0, 0 })
intInput := bigslice.Map(input, func(x int) (int, int) { return 0, 0 })
expectTypeError(t, "fold: key type float64 cannot be accumulated", func() { bigslice.Fold(floatInput, func(x int) int { return 0 }) })
expectTypeError(t, "Fold can be applied only for slices with at least two columns; got 1", func() { bigslice.Fold(input, func(x int) int { return 0 }) })
expectTypeError(t, "fold: expected func(acc, t2, t3, ..., tn), got func(int) int", func() { bigslice.Fold(intInput, func(x int) int { return 0 }) })
expectTypeError(t, "fold: expected func(acc, t2, t3, ..., tn), got func(int, int) string", func() { bigslice.Fold(intInput, func(a, x int) string { return "" }) })
expectTypeError(t, "fold: fold functions must return exactly one value", func() { bigslice.Fold(intInput, func(a, x int) (int, int) { return 0, 0 }) })
expectTypeError(t, "fold: expected func(acc, t2, t3, ..., tn), got func(int, string) int", func() { bigslice.Fold(intInput, func(a int, x string) int { return 0 }) })
}
func TestHead(t *testing.T) {
slice := bigslice.Head(bigslice.Const(2, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}), 2)
assertEqual(t, slice, false, []int{1, 2, 6, 7})
}
// TestPrefixedPragma verifies that Prefixed slices properly adopt pragmas from
// their underlying slices.
func TestPrefixedPragma(t *testing.T) {
slice := bigslice.Const(2, []int{0, 1, 2}, []string{"a", "b", "c"})
slice = bigslice.Map(slice, func(i int, s string) (int, string) {
return i, s
}, bigslice.Exclusive)
slice = bigslice.Prefixed(slice, 2)
pragma, ok := slice.(bigslice.Pragma)
if !ok {
t.Fatal("Prefixed does not implement Pragma")
}
if !pragma.Exclusive() {
t.Error("Prefixed not Exclusive")
}
}
func TestScan(t *testing.T) {
const (
N = 10000
Nshard = 10
)
input := make([]int, N)
for i := range input {
input[i] = i
}
var mu sync.Mutex
output := make([]int, N)
shards := make([]int, Nshard)
slice := bigslice.Const(Nshard, input)
slice = bigslice.Scan(slice, func(shard int, scan *sliceio.Scanner) error {
mu.Lock()
defer mu.Unlock()
shards[shard]++
var elem int
ctx := context.Background()
for scan.Scan(ctx, &elem) {
output[elem]++
}
return scan.Err()
})
n := len(run(context.Background(), t, slice))
for i, got := range output {
if want := n; got != want {
t.Errorf("wrong count for output %d, got %v, want %v", i, got, want)
}
}
for i, got := range shards {
if want := n; got != want {
t.Errorf("wrong count for shard %d, got %v, want %v", i, got, want)
}
}
}
func TestPanic(t *testing.T) {
slice := bigslice.Const(1, []int{1, 2, 3})
slice = bigslice.Map(slice, func(i int) int {
panic(i)
})
fn := bigslice.Func(func() bigslice.Slice { return slice })
ctx := context.Background()
for name, opt := range executors {
sess := exec.Start(opt)
// TODO(marius): faster teardown in bigmachine so that we can call this here.
// defer sess.Shutdown()
_, err := sess.Run(ctx, fn)
if err == nil {
t.Errorf("executor %s: expected error", name)
continue
}
if msg := err.Error(); !strings.Contains(msg, "panic while evaluating slice") {
t.Errorf("wrong error message %q", msg)
}
}
}
func TestEncodingError(t *testing.T) {
type ungobable struct {
x int
}
slice := bigslice.Const(1, []int{1, 2, 3})
slice = bigslice.Map(slice, func(x int) (int, ungobable) { return x, ungobable{x} })
slice = bigslice.Reduce(slice, func(a, e ungobable) ungobable { return ungobable{a.x + e.x} })
scannerErrs := runError(context.Background(), t, slice)
for name, scannerErr := range scannerErrs {
// The local executor keeps things in memory by default.
// Note thaht while, currently the Bigmachine executors will by default
// run everything through gob, this is not at all a requirement. So this
// test may begin failing in the presence of future optimizatons.
if name == "Local" {
continue
}
err := scannerErr.Err