-
Notifications
You must be signed in to change notification settings - Fork 7
/
stream_internal_test.go
255 lines (202 loc) · 7.53 KB
/
stream_internal_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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package streams
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestStreamBuilder_Source(t *testing.T) {
source := &streamSource{}
builder := NewStreamBuilder()
stream := builder.Source("test", source)
assert.Len(t, stream.parents, 1)
assert.IsType(t, &SourceNode{}, stream.parents[0])
assert.IsType(t, stream.parents[0].(*SourceNode).Name(), "test")
}
func TestStreamBuilder_Build(t *testing.T) {
proc := &streamProcessor{}
source := &streamSource{}
builder := NewStreamBuilder()
builder.Source("test", source).Process("test1", proc)
top, errs := builder.Build()
assert.Len(t, errs, 0)
assert.Len(t, top.Sources(), 1)
assert.Len(t, top.Processors(), 1)
assert.Contains(t, top.Sources(), source)
}
func TestStream_Filter(t *testing.T) {
source := &streamSource{}
builder := NewStreamBuilder()
stream := builder.Source("source", source).
Filter("test", PredicateFunc(func(msg Message) (bool, error) {
return true, nil
}))
assert.Len(t, stream.parents, 1)
assert.IsType(t, &ProcessorNode{}, stream.parents[0])
assert.Equal(t, stream.parents[0].(*ProcessorNode).name, "test")
assert.IsType(t, &FilterProcessor{}, stream.parents[0].(*ProcessorNode).processor)
}
func TestStream_FilterFunc(t *testing.T) {
source := &streamSource{}
builder := NewStreamBuilder()
stream := builder.Source("source", source).
FilterFunc("test", func(msg Message) (bool, error) {
return true, nil
})
assert.Len(t, stream.parents, 1)
assert.IsType(t, &ProcessorNode{}, stream.parents[0])
assert.Equal(t, stream.parents[0].(*ProcessorNode).name, "test")
assert.IsType(t, &FilterProcessor{}, stream.parents[0].(*ProcessorNode).processor)
}
func TestStream_Branch(t *testing.T) {
source := &streamSource{}
builder := NewStreamBuilder()
streams := builder.Source("source", source).Branch(
"test",
PredicateFunc(func(msg Message) (bool, error) {
return true, nil
}),
PredicateFunc(func(msg Message) (bool, error) {
return true, nil
}),
)
assert.Len(t, streams, 2)
assert.Len(t, streams[0].parents, 1)
assert.IsType(t, &ProcessorNode{}, streams[0].parents[0])
assert.Equal(t, streams[0].parents[0].(*ProcessorNode).name, "test")
assert.IsType(t, &BranchProcessor{}, streams[0].parents[0].(*ProcessorNode).processor)
assert.Len(t, streams[1].parents, 1)
assert.IsType(t, &ProcessorNode{}, streams[1].parents[0])
assert.Equal(t, streams[1].parents[0].(*ProcessorNode).name, "test")
assert.IsType(t, &BranchProcessor{}, streams[1].parents[0].(*ProcessorNode).processor)
}
func TestStream_BranchFunc(t *testing.T) {
source := &streamSource{}
builder := NewStreamBuilder()
streams := builder.Source("source", source).BranchFunc(
"test",
func(msg Message) (bool, error) {
return true, nil
},
func(msg Message) (bool, error) {
return true, nil
},
)
assert.Len(t, streams, 2)
assert.Len(t, streams[0].parents, 1)
assert.IsType(t, &ProcessorNode{}, streams[0].parents[0])
assert.Equal(t, streams[0].parents[0].(*ProcessorNode).name, "test")
assert.IsType(t, &BranchProcessor{}, streams[0].parents[0].(*ProcessorNode).processor)
assert.Len(t, streams[1].parents, 1)
assert.IsType(t, &ProcessorNode{}, streams[1].parents[0])
assert.Equal(t, streams[1].parents[0].(*ProcessorNode).name, "test")
assert.IsType(t, &BranchProcessor{}, streams[1].parents[0].(*ProcessorNode).processor)
}
func TestStream_Map(t *testing.T) {
source := &streamSource{}
builder := NewStreamBuilder()
stream := builder.Source("source", source).
Map("test", MapperFunc(func(msg Message) (Message, error) {
return EmptyMessage, nil
}))
assert.Len(t, stream.parents, 1)
assert.IsType(t, &ProcessorNode{}, stream.parents[0])
assert.Equal(t, stream.parents[0].(*ProcessorNode).name, "test")
assert.IsType(t, &MapProcessor{}, stream.parents[0].(*ProcessorNode).processor)
}
func TestStream_MapFunc(t *testing.T) {
source := &streamSource{}
builder := NewStreamBuilder()
stream := builder.Source("source", source).
MapFunc("test", func(msg Message) (Message, error) {
return EmptyMessage, nil
})
assert.Len(t, stream.parents, 1)
assert.IsType(t, &ProcessorNode{}, stream.parents[0])
assert.Equal(t, stream.parents[0].(*ProcessorNode).name, "test")
assert.IsType(t, &MapProcessor{}, stream.parents[0].(*ProcessorNode).processor)
}
func TestStream_FlatMap(t *testing.T) {
source := &streamSource{}
builder := NewStreamBuilder()
stream := builder.Source("source", source).
FlatMap("test", FlatMapperFunc(func(msg Message) ([]Message, error) {
return nil, nil
}))
assert.Len(t, stream.parents, 1)
assert.IsType(t, &ProcessorNode{}, stream.parents[0])
assert.Equal(t, stream.parents[0].(*ProcessorNode).name, "test")
assert.IsType(t, &FlatMapProcessor{}, stream.parents[0].(*ProcessorNode).processor)
}
func TestStream_FlatMapFunc(t *testing.T) {
source := &streamSource{}
builder := NewStreamBuilder()
stream := builder.Source("source", source).
FlatMapFunc("test", func(msg Message) ([]Message, error) {
return nil, nil
})
assert.Len(t, stream.parents, 1)
assert.IsType(t, &ProcessorNode{}, stream.parents[0])
assert.Equal(t, stream.parents[0].(*ProcessorNode).name, "test")
assert.IsType(t, &FlatMapProcessor{}, stream.parents[0].(*ProcessorNode).processor)
}
func TestStream_Merge(t *testing.T) {
source := &streamSource{}
builder := NewStreamBuilder()
stream1 := builder.Source("source1", source)
stream2 := builder.Source("source2", source)
stream := stream2.Merge("test", stream1)
assert.Len(t, stream.parents, 1)
assert.IsType(t, &ProcessorNode{}, stream.parents[0])
assert.Equal(t, stream.parents[0].(*ProcessorNode).name, "test")
assert.IsType(t, &MergeProcessor{}, stream.parents[0].(*ProcessorNode).processor)
}
func TestStream_Print(t *testing.T) {
source := &streamSource{}
builder := NewStreamBuilder()
stream := builder.Source("source", source).Print("test")
assert.Len(t, stream.parents, 1)
assert.IsType(t, &ProcessorNode{}, stream.parents[0])
assert.Equal(t, stream.parents[0].(*ProcessorNode).name, "test")
assert.IsType(t, &PrintProcessor{}, stream.parents[0].(*ProcessorNode).processor)
}
func TestStream_Process(t *testing.T) {
proc := &streamProcessor{}
source := &streamSource{}
builder := NewStreamBuilder()
stream := builder.Source("source", source).Process("test", proc)
assert.Len(t, stream.parents, 1)
assert.IsType(t, &ProcessorNode{}, stream.parents[0])
assert.Equal(t, stream.parents[0].(*ProcessorNode).name, "test")
assert.Equal(t, stream.parents[0].(*ProcessorNode).processor, proc)
}
func TestStream_FanOut(t *testing.T) {
source := &streamSource{}
builder := NewStreamBuilder()
streams := builder.Source("source", source).FanOut("test", 2)
assert.Len(t, streams, 2)
assert.Len(t, streams[0].parents, 1)
assert.IsType(t, &ProcessorNode{}, streams[0].parents[0])
assert.Equal(t, streams[0].parents[0].(*ProcessorNode).name, "test")
assert.IsType(t, &FanOutProcessor{}, streams[0].parents[0].(*ProcessorNode).processor)
assert.Len(t, streams[1].parents, 1)
assert.IsType(t, &ProcessorNode{}, streams[1].parents[0])
assert.Equal(t, streams[1].parents[0].(*ProcessorNode).name, "test")
assert.IsType(t, &FanOutProcessor{}, streams[1].parents[0].(*ProcessorNode).processor)
}
type streamSource struct{}
func (s streamSource) Consume() (Message, error) {
return EmptyMessage, nil
}
func (s streamSource) Commit(v interface{}) error {
return nil
}
func (s streamSource) Close() error {
return nil
}
type streamProcessor struct{}
func (p streamProcessor) WithPipe(Pipe) {}
func (p streamProcessor) Process(msg Message) error {
return nil
}
func (p streamProcessor) Close() error {
return nil
}