-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_test.go
354 lines (284 loc) · 9.04 KB
/
lib_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
package pbatch_test
import (
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/saenai255/pbatch"
)
func TestRun_Success(t *testing.T) {
items := []int{1, 2, 3, 4, 5}
batchSize := 2
process := func(n int) (string, error) {
// Convert each number to a string
return fmt.Sprintf("Number: %d", n), nil
}
results, err := pbatch.Run(items, batchSize, pbatch.STOP_ON_ERROR, process)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
expected := []string{"Number: 1", "Number: 2", "Number: 3", "Number: 4", "Number: 5"}
for i := range results {
if results[i] != expected[i] {
t.Errorf("expected result %v at index %d, got %#v", expected[i], i, results[i])
}
}
}
func TestRun_Error(t *testing.T) {
items := []int{1, 2, 3, 4, 5}
batchSize := 3
process := func(n int) (string, error) {
// Simulate an error when processing number 3
if n == 3 {
return "", errors.New("error on item 3")
}
return fmt.Sprintf("Number: %d", n), nil
}
results, err := pbatch.Run(items, batchSize, pbatch.STOP_ON_ERROR, process)
if err == nil {
t.Fatal("expected error, got nil")
}
if err.Error() != "error on item 3" {
t.Errorf("expected error 'error on item 3', got %v", err)
}
// Results should be nil due to the early return on error
if results != nil {
t.Errorf("expected results to be nil, got %v", results)
}
}
func TestRun_EmptySlice(t *testing.T) {
items := []int{}
batchSize := 3
process := func(n int) (string, error) {
return fmt.Sprintf("Number: %d", n), nil
}
results, err := pbatch.Run(items, batchSize, pbatch.STOP_ON_ERROR, process)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if len(results) != 0 {
t.Errorf("expected empty results, got %v", results)
}
}
func TestRun_BatchSizeGreaterThanItems(t *testing.T) {
items := []int{1, 2}
batchSize := 10
process := func(n int) (string, error) {
return fmt.Sprintf("Number: %d", n), nil
}
results, err := pbatch.Run(items, batchSize, pbatch.STOP_ON_ERROR, process)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
expected := []string{"Number: 1", "Number: 2"}
for i := range results {
if results[i] != expected[i] {
t.Errorf("expected result %v at index %d, got %v", expected[i], i, results[i])
}
}
}
func TestRun_WithDelay(t *testing.T) {
items := []int{1, 2, 3}
batchSize := 2
process := func(n int) (string, error) {
time.Sleep(100 * time.Millisecond)
return fmt.Sprintf("Number: %d", n), nil
}
start := time.Now()
results, err := pbatch.Run(items, batchSize, pbatch.STOP_ON_ERROR, process)
elapsed := time.Since(start)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
expected := []string{"Number: 1", "Number: 2", "Number: 3"}
for i := range results {
if results[i] != expected[i] {
t.Errorf("expected result %v at index %d, got %v", expected[i], i, results[i])
}
}
if elapsed < 100*time.Millisecond || elapsed > 300*time.Millisecond {
t.Errorf("expected processing to take around 100-300ms, but took %v", elapsed)
}
}
func TestRun_WithDelay_Visual(t *testing.T) {
items := []int{1, 2, 3, 4, 5}
batchSize := 2
process := func(n int) (string, error) {
time.Sleep(1000 * time.Millisecond)
t.Logf("Processing item %d\n", n)
return fmt.Sprintf("Number: %d", n), nil
}
start := time.Now()
results, err := pbatch.Run(items, batchSize, pbatch.STOP_ON_ERROR, process)
elapsed := time.Since(start)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
expected := []string{"Number: 1", "Number: 2", "Number: 3", "Number: 4", "Number: 5"}
for i := range results {
if results[i] != expected[i] {
t.Errorf("expected result %v at index %d, got %v", expected[i], i, results[i])
}
}
if elapsed < 2500*time.Millisecond || elapsed > 3500*time.Millisecond {
t.Errorf("expected processing to take around 2500-3500ms, but took %v", elapsed)
}
}
func TestRun_ContinueOnErrors_Aggregate(t *testing.T) {
items := []int{1, 2, 3, 4, 5}
batchSize := 2
process := func(n int) (string, error) {
// Simulate an error for odd numbers
if n%2 != 0 {
return "", fmt.Errorf("error processing item %d", n)
}
return fmt.Sprintf("Processed: %d", n), nil
}
results, err := pbatch.Run(items, batchSize, pbatch.CONTINUE_ON_ERROR, process)
// Check if all items were processed
expectedResults := []string{"", "Processed: 2", "", "Processed: 4", ""}
for i, res := range results {
if res != expectedResults[i] {
t.Errorf("expected result %v at index %d, got %v", expectedResults[i], i, res)
}
}
// Check if the error contains all the expected error messages
if err == nil {
t.Fatal("expected aggregated error, got nil")
}
expectedErrors := []string{
"error processing item 1",
"error processing item 3",
"error processing item 5",
}
for _, expected := range expectedErrors {
if !strings.Contains(err.Error(), expected) {
t.Errorf("expected error message to contain %v, but got %v", expected, err.Error())
}
}
}
func TestRun_ContinueOnErrors_AllFail(t *testing.T) {
items := []int{1, 3, 5}
batchSize := 2
process := func(n int) (string, error) {
// All items will result in an error
return "", fmt.Errorf("error processing item %d", n)
}
results, err := pbatch.Run(items, batchSize, pbatch.CONTINUE_ON_ERROR, process)
// Results should all be empty strings as all items fail processing
expectedResults := []string{"", "", ""}
for i, res := range results {
if res != expectedResults[i] {
t.Errorf("expected result %v at index %d, got %v", expectedResults[i], i, res)
}
}
// Check if the error contains all the expected error messages
if err == nil {
t.Fatal("expected aggregated error, got nil")
}
expectedErrors := []string{
"error processing item 1",
"error processing item 3",
"error processing item 5",
}
for _, expected := range expectedErrors {
if !strings.Contains(err.Error(), expected) {
t.Errorf("expected error message to contain %v, but got %v", expected, err.Error())
}
}
}
func TestRun_ContinueOnErrors_SomeFail(t *testing.T) {
items := []int{1, 2, 3, 4, 5}
batchSize := 3
process := func(n int) (string, error) {
// Return error for odd numbers, but process even numbers successfully
if n%2 != 0 {
return "", errors.New("failed on odd number")
}
return fmt.Sprintf("Success: %d", n), nil
}
results, err := pbatch.Run(items, batchSize, pbatch.CONTINUE_ON_ERROR, process)
// Results should contain the processed results for even numbers, empty for odd numbers
expectedResults := []string{"", "Success: 2", "", "Success: 4", ""}
for i, res := range results {
if res != expectedResults[i] {
t.Errorf("expected result %v at index %d, got %v", expectedResults[i], i, res)
}
}
// Check that the error contains all the errors for odd numbers
if err == nil {
t.Fatal("expected aggregated error, got nil")
}
expectedErrorMessages := []string{
"failed on odd number",
}
for _, expected := range expectedErrorMessages {
if !strings.Contains(err.Error(), expected) {
t.Errorf("expected error message to contain %v, but got %v", expected, err.Error())
}
}
}
func TestRun_ContinueOnErrors_AllSuccess(t *testing.T) {
items := []int{2, 4, 6, 8}
batchSize := 3
process := func(n int) (string, error) {
return fmt.Sprintf("Success: %d", n), nil
}
results, err := pbatch.Run(items, batchSize, pbatch.CONTINUE_ON_ERROR, process)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
expectedResults := []string{"Success: 2", "Success: 4", "Success: 6", "Success: 8"}
for i, res := range results {
if res != expectedResults[i] {
t.Errorf("expected result %v at index %d, got %v", expectedResults[i], i, res)
}
}
}
func TestBatchErrorAggregation(t *testing.T) {
items := []int{1, 2, 3, 4, 5}
batchSize := 2
// Define a process function that will fail for odd numbers
process := func(n int) (string, error) {
if n%2 != 0 {
return "", fmt.Errorf("error processing item %d", n)
}
return fmt.Sprintf("Processed: %d", n), nil
}
// Use CONTINUE_ON_ERROR to aggregate all errors
_, err := pbatch.Run(items, batchSize, pbatch.CONTINUE_ON_ERROR, process)
// Ensure that an error was returned
if err == nil {
t.Fatal("expected a BatchError, got nil")
}
// Check if the error is a BatchError
if !pbatch.IsBatchError(err) {
t.Fatalf("expected error to be a BatchError, got %T", err)
}
// Unwrap the BatchError to access individual errors
errors := pbatch.UnwrapBatchError(err)
// Expected errors for all odd items
expectedErrors := []string{
"error processing item 1",
"error processing item 3",
"error processing item 5",
}
// Ensure that the length of unwrapped errors matches expected errors
if len(errors) != len(expectedErrors) {
t.Fatalf("expected %d errors, got %d", len(expectedErrors), len(errors))
}
// Validate each individual error. Order is not guaranteed, so we check each error against all expected errors
for i, individualErr := range errors {
found := false
for _, expected := range expectedErrors {
if individualErr.Error() == expected {
found = true
break
}
}
if !found {
t.Errorf("unexpected error %q at index %d", individualErr.Error(), i)
}
}
}