-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpipeline_test.go
179 lines (153 loc) · 3.3 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
package pipeline
import (
"context"
"errors"
"fmt"
"math/rand"
"reflect"
"regexp"
"testing"
)
func TestDataFlow(t *testing.T) {
stages := make([]Stage, 10)
for i := 0; i < len(stages); i++ {
stages[i] = testStage{t: t}
}
src := &sourceStub{data: stringDataValues(3)}
sink := new(sinkStub)
p := NewPipeline(stages...)
if err := p.Execute(context.TODO(), src, sink); err != nil {
t.Errorf("Error executing the Pipeline: %v", err)
}
if !reflect.DeepEqual(sink.data, src.data) {
t.Errorf("Data does not match.\nWanted:%v\nGot:%v\n", src.data, sink.data)
}
}
func TestTaskErrorHandling(t *testing.T) {
err := errors.New("task error")
stages := make([]Stage, 10)
for i := 0; i < len(stages); i++ {
var sErr error
if i == 5 {
sErr = err
}
stages[i] = testStage{
t: t,
err: sErr,
}
}
src := &sourceStub{data: stringDataValues(3)}
sink := new(sinkStub)
p := NewPipeline(stages...)
re := regexp.MustCompile("(?s).*task error.*")
if err := p.Execute(context.TODO(), src, sink); err == nil || !re.MatchString(err.Error()) {
t.Errorf("Error did not match the expectation: %v", err)
}
}
func TestDataItemCount(t *testing.T) {
src := &sourceStub{data: stringDataValues(1000)}
sink := new(sinkStub)
p := NewPipeline(&randomStage{
id: "first",
t: t,
})
if err := p.Execute(context.TODO(), src, sink); err != nil {
t.Errorf("Error executing the Pipeline: %v", err)
}
if num := p.DataItemCount(); num != 0 {
t.Errorf("Pipeline execution finished with %d pending data items", num)
}
}
type randomStage struct {
id string
t *testing.T
err error
}
func (s randomStage) ID() string {
return s.id
}
func (s randomStage) Run(ctx context.Context, sp StageParams) {
// add data items to the stage queue
for i := 0; i < 1000; i++ {
SendData(ctx, "first", &stringData{val: fmt.Sprint(i)}, &taskParams{
pipeline: sp.Pipeline(),
registry: sp.Registry(),
})
}
for {
select {
case <-ctx.Done():
return
case <-sp.DataQueue().Signal():
if d, ok := sp.DataQueue().Next(); ok {
if data, ok := d.(Data); ok {
s.processData(ctx, data, sp)
}
}
case d, ok := <-sp.Input():
if !ok {
return
}
s.processData(ctx, d, sp)
}
}
}
func (s randomStage) processData(ctx context.Context, d Data, sp StageParams) {
if s.err != nil {
s.t.Logf("[stage %d] emit error: %v", sp.Position(), s.err)
sp.Error().Append(s.err)
return
}
if num := rand.Intn(2); num == 0 {
return
}
select {
case <-ctx.Done():
case sp.Output() <- d:
}
}
type testStage struct {
id string
t *testing.T
dropData bool
err error
}
func (s testStage) ID() string {
return s.id
}
func (s testStage) Run(ctx context.Context, sp StageParams) {
for {
select {
case <-ctx.Done():
return
case d, ok := <-sp.Input():
if !ok {
return
}
if s.err != nil {
sp.Error().Append(s.err)
return
}
if s.dropData {
continue
}
select {
case <-ctx.Done():
return
case sp.Output() <- d:
}
}
}
}
type stringData struct {
val string
}
func (s *stringData) Clone() Data { return &stringData{val: s.val} }
func (s *stringData) String() string { return s.val }
func stringDataValues(num int) []Data {
out := make([]Data, num)
for i := 0; i < len(out); i++ {
out[i] = &stringData{val: fmt.Sprint(i)}
}
return out
}