-
Notifications
You must be signed in to change notification settings - Fork 0
/
scatter_gather_test.go
117 lines (107 loc) · 2.45 KB
/
scatter_gather_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
package scattergather
import (
"errors"
"reflect"
"sort"
"testing"
)
func TestScatterGatherWithInputParams(t *testing.T) {
testcases := []struct {
name string
input []string
batchSize int
fn func([]string) ([]string, error)
want []string
err error
}{
{
name: "test input size less than batch size",
input: []string{"bird", "cat", "dog", "fish"},
batchSize: 5,
fn: func(input []string) ([]string, error) {
var rs []string
for _, str := range input {
rs = append(rs, "my "+str)
}
return rs, nil
},
want: []string{
"my bird",
"my cat",
"my dog",
"my fish",
},
err: nil,
},
{
name: "test input size equal batch size",
input: []string{"bird", "cat", "dog", "fish"},
batchSize: 4,
fn: func(input []string) ([]string, error) {
var rs []string
for _, str := range input {
rs = append(rs, "my "+str)
}
return rs, nil
},
want: []string{
"my bird",
"my cat",
"my dog",
"my fish",
},
err: nil,
},
{
name: "test input size great than batch size",
input: []string{"bird", "cat", "dog", "fish"},
batchSize: 2,
fn: func(input []string) ([]string, error) {
var rs []string
for _, str := range input {
rs = append(rs, "my "+str)
}
return rs, nil
},
want: []string{
"my bird",
"my cat",
"my dog",
"my fish",
},
err: nil,
},
{
name: "test invalid batch size",
input: []string{"bird", "cat", "dog", "fish"},
batchSize: -1,
fn: nil,
want: nil,
err: ErrInvalidBatchSize,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
got, err := ScattergatherWithInputParams(tc.input, tc.batchSize, tc.fn)
if tc.err != err {
t.Fatalf("tc: %s, got: %+v, want: %+v", tc.name, err, tc.err)
}
sort.Strings(tc.want)
sort.Strings(got)
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("tc: %s, got: %v, want %v, err: %v %v %v", tc.name, got, tc.want, tc.err, err, tc.err == err)
}
})
}
}
func TestScatterGatherWithInput_ReturnError(t *testing.T) {
want := errors.New("random failure")
input := []string{"bird", "cat", "dog", "fish"}
_, err := ScattergatherWithInputParams(input, 2, func(k []string) ([]string, error) {
return nil, want
})
if err != want {
t.Fatalf("got: %v, want: %v", err, want)
}
// assert.EqualError(t, err, "failed to handle")
}