-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathpipeline_test.go
226 lines (186 loc) · 4.72 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
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
package pipeline
import (
"errors"
"fmt"
"log"
"math/rand"
"testing"
"time"
)
var tests = []pipeConfig{
{"TestNoStage", 70, []sg{}},
{"SingleStageEmptyStep", 100, []sg{{false, 0, 0}}},
{"SingleStageSingleStep", 100, []sg{{false, 1, 0}}},
{"TestSingleStageMultiStep", 100, []sg{{false, 2, 0}}},
{"TestMultiStageMultiStep", 100, []sg{{false, 2, 0}, {false, 2, 0}}},
{"TestMultiStageMultiStepConcurrent", 100, []sg{{true, 2, 0}, {true, 2, 0}}},
{"TestMultiStageMultiStepConcurrentMixed", 100, []sg{{true, 2, 0}, {false, 2, 0}}},
{"SingleStageSingleStepError", 100, []sg{{false, 0, 1}}},
{"TestSingleStageMultiStepError", 100, []sg{{false, 0, 2}}},
{"TestMultiStageMultiStepError", 100, []sg{{false, 0, 2}, {false, 0, 2}}},
{"TestMultiStageMultiStepConcurrentError", 100, []sg{{true, 0, 2}, {true, 0, 2}}},
{"TestMultiStageMultiStepConcurrentErrorMixed", -10, []sg{{true, 0, 2}, {false, 0, 2}}},
{"TestMultiStageMultiStepConcurrentErrorMixed2", 100, []sg{{true, 1, 2}, {false, 0, 2}}},
{"TestMultiStageMultiStepConcurrentErrorMixed3", -1, []sg{{true, 0, 2}, {true, 1, 2}}},
}
func TestPipeline(t *testing.T) {
for _, test := range tests {
testpipe, errorPipe := createPipeline(test)
go readPipeline(testpipe)
if errorPipe {
result := testpipe.Run()
if result.Error == nil {
log.Fatalf("Test failed: %v", test.pipelineName)
}
} else {
result := testpipe.Run()
if result.Error != nil {
log.Fatalf("Test failed: %v", test.pipelineName)
}
}
}
}
func BenchmarkPipeline(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
i := rand.Intn(len(tests))
testpipe, errorPipe := createPipeline(tests[i])
if errorPipe {
err := testpipe.Run()
if err == nil {
log.Fatalf("Test failed: %v", testpipe.Name)
}
} else {
err := testpipe.Run()
if err != nil {
log.Fatalf("Test failed: %v", testpipe.Name)
}
}
}
})
}
type TestStep struct {
StepContext
key string
}
func (t TestStep) Exec(request *Request) *Result {
t.Status("start step")
t.Status("executing test ")
time.Sleep(time.Millisecond * 200)
t.Status("end step")
return nil
}
func (t TestStep) Cancel() error {
t.Status("cancel step")
return nil
}
type TestStep2 struct {
StepContext
key string
}
func (t TestStep2) Exec(request *Request) *Result {
t.Status("start step")
t.Status("executing test 2")
time.Sleep(time.Millisecond * 200)
t.Status("end step")
return nil
}
func (t TestStep2) Cancel() error {
t.Status("cancel step")
return nil
}
type TestStepErr struct {
StepContext
key string
}
func (t TestStepErr) Exec(request *Request) *Result {
t.Status("start step")
t.Status("executing test err")
time.Sleep(time.Millisecond * 500)
t.Status("end step")
return &Result{Error: errors.New("test error 1")}
}
func (t TestStepErr) Cancel() error {
t.Status("cancel step")
return nil
}
type TestStepErr2 struct {
StepContext
key string
}
func (t TestStepErr2) Exec(request *Request) *Result {
t.Status("start step")
t.Status("executing test err 2")
time.Sleep(time.Millisecond * 200)
t.Status("end step")
return &Result{Error: errors.New("test error 2")}
}
func (t TestStepErr2) Cancel() error {
t.Status("cancel step")
return nil
}
type sg struct {
concurrent bool
steps int
errorSteps int
}
type pipeConfig struct {
pipelineName string
pipeLineOutBufferLen int
sgArr []sg
}
func createPipeline(testPipeConfig pipeConfig) (*Pipeline, bool) {
testpipe := New(testPipeConfig.pipelineName, testPipeConfig.pipeLineOutBufferLen)
errorPipeline := false
var testStages []*Stage
for i, sg := range testPipeConfig.sgArr {
// create stage
stage := NewStage(fmt.Sprintf("testStage%d", i), sg.concurrent, false)
// create normal steps
for j := 0; j < sg.steps; j++ {
if j == 0 {
stage.AddStep(&TestStep{key: testpipe.Name})
} else if j == 1 {
stage.AddStep(&TestStep2{key: testpipe.Name})
} else {
stage.AddStep(&TestStep2{key: testpipe.Name})
}
}
// create error steps
for k := 0; k < sg.errorSteps; k++ {
if k == 0 {
stage.AddStep(&TestStepErr{key: testpipe.Name})
} else if k == 1 {
stage.AddStep(&TestStepErr2{key: testpipe.Name})
} else {
stage.AddStep(&TestStepErr2{key: testpipe.Name})
}
errorPipeline = true
}
testStages = append(testStages, stage)
totalSteps := sg.errorSteps + sg.steps
if totalSteps == 0 {
errorPipeline = true
}
}
if len(testStages) == 0 {
errorPipeline = true
}
testpipe.AddStage(testStages...)
return testpipe, errorPipeline
}
func readPipeline(testpipe *Pipeline) {
out, err := testpipe.Out()
if err != nil {
return
}
loop:
for {
select {
case line := <-out:
fmt.Println(line)
case <-time.After(time.Second * 10):
break loop
}
}
}