-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipeline_test.go
147 lines (122 loc) · 3.03 KB
/
pipeline_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
package pipeline_test
import (
"fmt"
"sync/atomic"
"testing"
"github.com/mhmtszr/pipeline"
)
type (
Square struct{}
Add struct{}
Multiply struct{}
)
func (s Square) Execute(context *int, next func(context *int) error) error {
*context = (*context) * (*context)
return next(context)
}
func (m Multiply) Execute(_ *int, _ func(context *int) error) error {
return fmt.Errorf("multiplyStepError")
}
func (s Square) ConcurrentExecute(_ *atomic.Uint64) error {
return fmt.Errorf("errorTest")
}
func (a Add) Execute(context *int, next func(context *int) error) error {
*context = (*context) + (*context)
return next(context)
}
func (a Add) ConcurrentExecute(context *atomic.Uint64) error {
context.Add(context.Load())
return nil
}
func TestPipeline(t *testing.T) {
p := pipeline.Builder[*int]{}.UsePipelineStep(Square{}).UsePipelineStep(Add{}).Build()
nm := 3
want := 18
err := p.Execute(&nm)
if err != nil {
t.Errorf("got error %s", err.Error())
}
if nm != 18 {
t.Errorf("got %d, wanted %d", nm, want)
}
}
func TestConditionalPipeline(t *testing.T) {
p := pipeline.Builder[*int]{}.
UseConditionalStepBuilder(
pipeline.NewConditionalStepBuilder[*int]().
Condition(func(context *int) bool {
return *context == 3
}).
IfTrue(Square{}).
IfFalse(Add{}),
).UsePipelineStep(Add{}).Build()
nm := 3
want := 18
_ = p.Execute(&nm)
if nm != 18 {
t.Errorf("got %d, wanted %d", nm, want)
}
nm = 4
want = 16
_ = p.Execute(&nm)
if nm != 16 {
t.Errorf("got %d, wanted %d", nm, want)
}
}
func TestConditionalPipelineStepError(t *testing.T) {
p := pipeline.Builder[*int]{}.
UseConditionalStepBuilder(
pipeline.NewConditionalStepBuilder[*int]().
Condition(func(context *int) bool {
return *context == 3
}).
IfTrue(Multiply{}),
).UsePipelineStep(Add{}).Build()
nm := 3
wantErr := "multiplyStepError"
err := p.Execute(&nm)
if err == nil || err.Error() != wantErr {
t.Errorf("got %s, wanted %s", err.Error(), wantErr)
}
}
func TestConditionalPipelineErrorWithoutCondition(t *testing.T) {
p := pipeline.Builder[*int]{}.
UseConditionalStepBuilder(
pipeline.NewConditionalStepBuilder[*int]().
IfTrue(Square{}).
IfFalse(Add{}),
).UsePipelineStep(Add{}).Build()
nm := 3
err := p.Execute(&nm)
wantErr := "condition not found in conditional step"
if err == nil || err.Error() != wantErr {
t.Errorf("got %s, wanted %s", err.Error(), wantErr)
}
}
func TestConcurrentStep(t *testing.T) {
p := pipeline.Builder[*atomic.Uint64]{}.
UseConcurrentPipelineSteps(
Add{}, Add{},
).Build()
want := 20
var nmb atomic.Uint64
nmb.Add(5)
_ = p.Execute(&nmb)
load := nmb.Load()
if load != uint64(want) {
t.Errorf("got %d, wanted %d", load, want)
}
}
func TestConcurrentStepError(t *testing.T) {
p := pipeline.Builder[*atomic.Uint64]{}.
UseConcurrentPipelineSteps(
Add{}, Square{},
).Build()
var nmb atomic.Uint64
nmb.Add(5)
err := p.Execute(&nmb)
wantErr := "errorTest"
if err == nil || err.Error() != wantErr {
t.Errorf("got %s, wanted %s", err.Error(), wantErr)
}
}