-
Notifications
You must be signed in to change notification settings - Fork 0
/
functional_test.go
262 lines (214 loc) · 6.03 KB
/
functional_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
package fngo
import (
"context"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParallelFilter(t *testing.T) {
expectedNames := map[string]any{
"alice": true,
"charlie": true,
"darren": true,
}
tests := []struct {
name string
filterError error
cancelContext bool
expectedError error
}{
{"nominal", nil, false, nil},
{"filterError", assert.AnError, false, assert.AnError},
{"masterContextCanceled", nil, true, context.Canceled},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if test.cancelContext {
cancel()
}
names := SliceSource(ctx, []string{"alice", "bob", "charlie", "darren", "erin"})
filteredNames := ParallelFilter(names, func(_ context.Context, name string) (bool, error) {
return strings.ContainsRune(name, 'a'), test.filterError
})
actualNames := make(map[string]any)
err := Sink(filteredNames, func(_ context.Context, name string) error {
actualNames[name] = true
return nil
})
assert.Equal(t, test.expectedError, err, "wrong error")
if test.expectedError == nil {
assert.Equal(t, expectedNames, actualNames, "wrong names")
}
})
}
}
func TestParallelMap(t *testing.T) {
expectedLengths := map[int]any{
3: true,
4: true,
5: true,
6: true,
7: true,
}
tests := []struct {
name string
mapError error
cancelContext bool
expectedError error
}{
{"nominal", nil, false, nil},
{"mapError", assert.AnError, false, assert.AnError},
{"masterContextCanceled", nil, true, context.Canceled},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if test.cancelContext {
cancel()
}
names := SliceSource(ctx, []string{"alice", "bob", "charlie", "darren", "erin"})
mappedLengths := ParallelMap(names, func(_ context.Context, name string) (int, error) {
return len(name), test.mapError
})
actualLengths := make(map[int]any)
err := Sink(mappedLengths, func(_ context.Context, length int) error {
actualLengths[length] = true
return nil
})
assert.Equal(t, test.expectedError, err, "wrong error")
if test.expectedError == nil {
assert.Equal(t, expectedLengths, actualLengths, "wrong lengths")
}
})
}
}
func TestPipeline(t *testing.T) {
expectedNames := []string{
"alice",
"alice",
"alice",
"alice",
"alice",
"charlie",
"charlie",
"charlie",
"charlie",
"charlie",
"charlie",
"charlie",
"david",
"david",
"david",
"david",
"david",
}
tests := []struct {
name string
sourceError error
filterError error
mapError error
sinkError error
cancelContext bool
expectedError error
}{
{"nominal", nil, nil, nil, nil, false, nil},
{"sourceError", assert.AnError, nil, nil, nil, false, assert.AnError},
{"filterError", nil, assert.AnError, nil, nil, false, assert.AnError},
{"mapError", nil, nil, assert.AnError, nil, false, assert.AnError},
{"sinkError", nil, nil, nil, assert.AnError, false, assert.AnError},
{"masterContextCanceled", nil, nil, nil, nil, true, context.Canceled},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if test.cancelContext {
cancel()
}
names := Source(ctx, func(_ context.Context, emit func(string) error) error {
for _, name := range []string{"alice", "bob", "charlie", "david", "erin"} {
if err := emit(name); err != nil {
return err
}
}
return test.sourceError
})
namesWithA := Filter(names, func(_ context.Context, name string) (bool, error) {
return strings.ContainsRune(name, 'a'), test.filterError
})
repeatedNames := Map(namesWithA, func(_ context.Context, name string) ([]string, error) {
output := make([]string, len(name))
for i := range output {
output[i] = name
}
return output, test.mapError
})
flattenedNames := Flatten(repeatedNames)
actualNames := make([]string, 0)
err := Sink(flattenedNames, func(_ context.Context, name string) error {
actualNames = append(actualNames, name)
return test.sinkError
})
assert.Equal(t, test.expectedError, err, "wrong error")
if test.expectedError == nil {
assert.Equal(t, expectedNames, actualNames, "wrong names")
}
})
}
}
func TestReduce(t *testing.T) {
const expectedTotalLength = 24
tests := []struct {
name string
reduceError error
expectedError error
}{
{"nominal", nil, nil},
{"reduceError", assert.AnError, assert.AnError},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
names := SliceSource(context.Background(), []string{"alice", "bob", "charlie", "david", "erin"})
actualTotalLength, err := Reduce(names, func(_ context.Context, name string, currentTotal int) (int, error) {
return currentTotal + len(name), test.reduceError
}, 0)
assert.Equal(t, test.expectedError, err, "wrong error")
if test.expectedError == nil {
assert.Equal(t, expectedTotalLength, actualTotalLength, "wrong total-length")
}
})
}
}
func TestSliceSource(t *testing.T) {
expectedNames := []string{"alice", "bob", "charlie", "david", "erin"}
tests := []struct {
name string
cancelContext bool
expectedError error
}{
{"nominal", false, nil},
{"masterContextCanceled", true, context.Canceled},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if test.cancelContext {
cancel()
}
names := SliceSource(ctx, expectedNames)
actualNames := make([]string, 0)
err := Sink(names, func(_ context.Context, name string) error {
actualNames = append(actualNames, name)
return nil
})
assert.Equal(t, test.expectedError, err, "wrong error")
if test.expectedError == nil {
assert.Equal(t, expectedNames, actualNames, "wrong names")
}
})
}
}