-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_slice_action_test.go
More file actions
67 lines (57 loc) · 1.78 KB
/
parallel_slice_action_test.go
File metadata and controls
67 lines (57 loc) · 1.78 KB
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
package chain
import (
"context"
"errors"
"github.com/JSYoo5B/chain/internal/logger"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"testing"
)
func TestParallelSliceAction(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
double := NewSimpleAction(
"double",
func(ctx context.Context, input int) (int, error) {
logger.Debugf(ctx, "doubling %d", input)
return input * 2, nil
})
positiveDouble := NewSimpleAction(
"positiveDouble",
func(ctx context.Context, input int) (int, error) {
logger.Debugf(ctx, "doubling %d (only positives)", input)
if input < 0 {
return 0, errors.New("negative input")
}
return input * 2, nil
})
divide10 := NewSimpleAction(
"divide10",
func(ctx context.Context, input int) (int, error) {
logger.Debugf(ctx, "dividing 10 with %d", input)
return 10 / input, nil
})
t.Run("simple parallel iteration", func(t *testing.T) {
doubles := AsParallelSliceAction("MapDouble", double)
input := []int{1, 2, 3, 4, 5}
expected := []int{2, 4, 6, 8, 10}
output, err := doubles.Run(context.Background(), input)
assert.NoError(t, err)
assert.Equal(t, expected, output)
})
t.Run("error in parallel iteration continues", func(t *testing.T) {
doubles := AsParallelSliceAction("MapDoubleContinue", positiveDouble)
input := []int{1, 2, -1, 4, 5}
expected := []int{2, 4, 0, 8, 10}
output, err := doubles.Run(context.Background(), input)
assert.Error(t, err)
assert.Equal(t, expected, output)
})
t.Run("panic in parallel iteration", func(t *testing.T) {
divides := AsParallelSliceAction("MapDivide10", divide10)
input := []int{10, 5, 2, 0, 1}
expected := []int{1, 2, 5, 0, 10}
output, err := divides.Run(context.Background(), input)
assert.Error(t, err)
assert.Equal(t, expected, output)
})
}