-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstep_test.go
185 lines (151 loc) · 4.07 KB
/
step_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
package pipeline_test
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/saantiaguilera/go-pipeline"
)
type (
mockStep[I, O any] struct {
mock.Mock
}
noopStep[T any] struct{}
)
var (
stepMux = sync.Mutex{}
)
func (m *mockStep[I, O]) ID() string {
args := m.Called()
return args.String(0)
}
func (m *mockStep[I, O]) Name() string {
args := m.Called()
return args.String(0)
}
func (m *mockStep[I, O]) Run(ctx context.Context, in I) (O, error) {
args := m.Called(ctx, in)
if args.Get(0) == nil {
return *new(O), args.Error(1)
}
return args.Get(0).(O), args.Error(1)
}
func (m *mockStep[I, O]) Draw(g pipeline.Graph) {
_ = m.Called(g)
}
func (s noopStep[T]) Draw(pipeline.Graph) {
// nothing
}
func (s noopStep[T]) Run(_ context.Context, in T) (T, error) {
return in, nil
}
func NewStep[I, O any](data int, arr **[]int) pipeline.Step[I, O] {
step := new(mockStep[I, O])
step.On("Run", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
stepMux.Lock()
tmp := append(**arr, data)
*arr = &tmp
stepMux.Unlock()
time.Sleep(time.Duration(100/(data+1)) * time.Millisecond) // Force a trap / yield
}).Return(*new(O), nil).Once()
return step
}
// The following example shows a simple unit step that runs a unit of work
// with a given input and yields a result of a different type or an error
// depending on the execution
//
// This example uses dummy data to showcase as simple as possible this scenario.
//
// Note: we use several UnitStep to showcase as it allows us to
// easily run dummy code, but it could use any type of step you want
// as long as it implements pipeline.Step[I, O]
func ExampleUnitStep() {
type (
InData any
OutData any
)
step := pipeline.NewUnitStep(
"do_something",
func(ctx context.Context, in InData) (OutData, error) {
// do something with input
return OutData(in), nil
},
)
out, err := step.Run(context.Background(), InData("example"))
fmt.Println(out, err)
// output: example <nil>
}
// Benchmark for traversing a unit step. This is simply used so that future changes can
// easily reflect how they affected the performance
//
// goos: darwin
// goarch: amd64
// pkg: github.com/saantiaguilera/go-pipeline
// cpu: Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz
// BenchmarkUnitStep-8 5496482 168.5 ns/op 0 B/op 0 allocs/op
func BenchmarkUnitStep(b *testing.B) {
var err error
s := pipeline.NewUnitStep(
"name",
func(ctx context.Context, a any) (any, error) {
return a, nil
},
)
ctx := context.Background()
in := 0
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StartTimer()
_, err = s.Run(ctx, in)
b.StopTimer()
if err != nil {
b.Fail()
}
}
}
func TestUnitStep_GivenAName_WhenGettingItsName_ThenItsTheExpected(t *testing.T) {
expectedName := "test_name"
step := pipeline.NewUnitStep[any, any](expectedName, nil)
name := step.Name()
assert.Equal(t, expectedName, name)
}
func TestUnitStep_GivenARunFunc_WhenRunning_ThenItsCalled(t *testing.T) {
called := false
run := func(context.Context, any) (any, error) {
called = true
return nil, nil
}
step := pipeline.NewUnitStep("", run)
_, _ = step.Run(context.Background(), 1)
assert.True(t, called)
}
func TestUnitStep_GivenARunFuncThatErrors_WhenRunning_ThenErrorIsReturned(t *testing.T) {
expectedErr := errors.New("some error")
run := func(context.Context, any) (any, error) {
return nil, expectedErr
}
step := pipeline.NewUnitStep("", run)
_, err := step.Run(context.Background(), 1)
assert.Equal(t, expectedErr, err)
}
func TestUnitStep_GivenOne_ThenHasID(t *testing.T) {
step := pipeline.NewUnitStep[any, any]("", nil)
id := step.ID()
assert.NotEmpty(t, id)
}
func TestUnitStep_GivenACancelledContext_WhenRunning_ThenFailsWithoutRunning(t *testing.T) {
called := false
step := pipeline.NewUnitStep("", func(ctx context.Context, t any) (any, error) {
called = true
return nil, nil
})
ctx, canc := context.WithCancel(context.Background())
canc()
v, _ := step.Run(ctx, 1)
assert.Nil(t, v)
assert.False(t, called)
}