-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprocessor_test.go
executable file
·285 lines (257 loc) · 7.54 KB
/
processor_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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package go4data
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/percybolmer/go4data/handlers"
"github.com/percybolmer/go4data/handlers/files"
"github.com/percybolmer/go4data/handlers/filters"
"github.com/percybolmer/go4data/handlers/parsers"
"github.com/percybolmer/go4data/handlers/terminal"
"github.com/percybolmer/go4data/metric"
"github.com/percybolmer/go4data/payload"
"github.com/percybolmer/go4data/property"
"github.com/percybolmer/go4data/pubsub"
)
type testHandler struct {
Name string
handlers.Handler
cfg *property.Configuration
}
func NewTestHandler() handlers.Handler {
return &testHandler{
cfg: property.NewConfiguration(),
}
}
func (th *testHandler) GetErrorChannel() chan error {
return nil
}
func (th *testHandler) ValidateConfiguration() (bool, []string) {
return true, nil
}
func (th *testHandler) SetMetricProvider(p metric.Provider, prefix string) error {
return errors.New("went sideways")
}
func (th *testHandler) GetConfiguration() *property.Configuration {
if th.cfg == nil {
th.cfg = property.NewConfiguration()
}
return th.cfg
}
func TestNewProcessor(t *testing.T) {
p1 := NewProcessor("test")
if p1.ID == 0 {
t.Fatal("should not be 0")
}
p2 := NewProcessor("test")
if p2.ID == p1.ID {
t.Fatal("Second ID should be not be duplicate ")
}
}
func TestStop(t *testing.T) {
testf := terminal.NewStdoutHandler()
p := NewProcessor("test")
p.SetHandler(testf)
err := p.Stop()
if !errors.Is(err, ErrProcessorAlreadyStopped) {
t.Fatal("Should detect stopped processor")
}
p.cancel = nil
p.Running = true
err = p.Stop()
if !errors.Is(err, ErrProcessorAlreadyStopped) {
t.Fatal("Should also detect bad cancel ")
}
p.Running = false
err = p.Start(context.Background())
if err != nil {
t.Fatal(err)
}
err = p.Stop()
if err != nil {
t.Fatal(err)
}
p.SetID(1)
if p.ID != 1 {
t.Fatal("Couldnt assign ID")
}
p.SetName("for the code coverage...")
if p.Name == "" {
t.Fatal("Bad naming")
}
}
func TestAddTopic(t *testing.T) {
p := NewProcessor("test")
err := p.AddTopics("test", "test2")
if err != nil {
t.Fatal("Should be no problem adding topics")
}
err = p.AddTopics("test")
if !errors.Is(err, ErrDuplicateTopic) {
t.Fatal("Should detect duplicate topics")
}
}
func TestStart(t *testing.T) {
// Register Printer
// Reset Register
testf := terminal.NewStdoutHandler()
cfg := testf.GetConfiguration()
// Add Properties here to test failure of them
cfg.AddProperty("test", "testing", true)
p := NewProcessor("test")
err := p.Start(nil)
if !errors.Is(err, ErrProcessorHasNoHandlerApplied) {
t.Fatal(err)
}
p.SetHandler(testf)
err = p.Start(nil)
if !errors.Is(err, ErrNilContext) {
t.Fatal(err)
}
// Invalid Properties
err = p.Start(context.Background())
if !errors.Is(err, ErrRequiredPropertiesNotFulfilled) {
t.Fatal(err)
}
cfg.SetProperty("test", true)
err = p.Start(context.Background())
if err != nil {
t.Fatal(err)
}
// Start again to see if it stops before starting a new since running is already running
err = p.Start(context.Background())
if err != nil {
t.Fatal(err)
}
p.Stop()
// Try broken Handler
test := NewTestHandler()
p.SetHandler(test)
err = p.Start(context.Background())
if err == nil {
t.Fatal("Error should have failed since testhandler is broken")
}
}
func TestPubSub(t *testing.T) {
defaultEngine, err := pubsub.EngineAsDefaultEngine()
if err != nil {
t.Fatal(err)
}
testf := terminal.NewStdoutHandler()
printer := NewProcessor("testf")
printer.SetHandler(testf)
printer.Subscribe("testtopic")
err = printer.Start(context.Background())
if err != nil {
t.Fatal(err)
}
// Now everything is setup, What sender sends will be printed by Printer
pay := &payload.BasePayload{
Source: "Test",
Payload: []byte(`Hello world`),
}
errs := pubsub.Publish("testtopic", pay)
if errs != nil {
t.Fatal(errs[0])
}
pubsub.Publish("topicthatbuffers", pay)
time.Sleep(1 * time.Second)
metricName := fmt.Sprintf("%s_%d_payloads_in", printer.Name, printer.ID)
printerMetric := printer.Metric.GetMetrics()
//t.Logf("%+v", printerMetric)
if printerMetric[metricName].Value != 1 {
t.Fatal("Wrong Printer metric value")
}
printer2 := NewProcessor("bufferProcessor")
printer2.SetHandler(testf)
printer2.Subscribe("topicthatbuffers")
printer2.Start(context.Background())
buffertopicInt, ok := defaultEngine.Topics.Load("topicthatbuffers")
if !ok {
t.Fatal("Didnt find buffer topic")
}
bufferTopic, ok := buffertopicInt.(*pubsub.Topic)
if !ok {
t.Fatal("Couldnt convert to topic")
}
if len(bufferTopic.Buffer.Flow) != 1 {
t.Fatal("Wrong buffer length in topicthatbuffers")
}
defaultEngine.DrainTopicsBuffer()
time.Sleep(1 * time.Second)
metricName = fmt.Sprintf("%s_%d_payloads_in", printer2.Name, printer2.ID)
printerMetric = printer2.Metric.GetMetrics()
if printerMetric[metricName].Value != 1 {
t.Fatal("Wrong Printer2 metric value")
}
}
func TestRealLifeCase(t *testing.T) {
// The idea here is to test a case of how it could be used by others
listDirProc := NewProcessor("listdir", "found_files")
readFileProc := NewProcessor("readfile", "file_data")
writeFileProc := NewProcessor("writefile")
csvReader := NewProcessor("csvReader", "map_reduce")
filter := NewProcessor("filter", "print_stdout")
printerProc := NewProcessor("printer", "printer_output")
printer2Proc := NewProcessor("printer2")
listDirProc.SetHandler(files.NewListDirectoryHandler())
printerProc.SetHandler(terminal.NewStdoutHandler())
writeFileProc.SetHandler(files.NewWriteFileHandler())
printer2Proc.SetHandler(terminal.NewStdoutHandler())
readFileProc.SetHandler(files.NewReadFileHandler())
csvReader.SetHandler(parsers.NewParseCSVHandler())
filter.SetHandler(filters.NewFilterHandler())
// Setup configurations - still a bit clonky, but LoadConfig should be impl soon
cfg := listDirProc.GetConfiguration()
err := cfg.SetProperty("path", "testing")
if err != nil {
t.Fatal(err)
}
err = cfg.SetProperty("buffertime", 5)
if err != nil {
t.Fatal(err)
}
filters := make(map[string][]string, 0)
filters["userinformation"] = append(filters["userinformation"], "username:percybolmer")
printerProc.GetConfiguration().SetProperty("forward", true)
readFileProc.GetConfiguration().SetProperty("remove_after", false)
writeFileProc.GetConfiguration().SetProperty("path", "testing/realexample")
writeFileProc.GetConfiguration().SetProperty("append", true)
writeFileProc.GetConfiguration().SetProperty("forward", false)
filter.GetConfiguration().SetProperty("filters", filters)
filter.GetConfiguration().SetProperty("strict", []string{"userinformation"})
// Fix Relationships
readFileProc.Subscribe("found_files")
csvReader.Subscribe("file_data")
filter.Subscribe("map_reduce")
printerProc.Subscribe("print_stdout")
printer2Proc.Subscribe("printer_output")
writeFileProc.Subscribe("printer_output")
// Startup
if err := listDirProc.Start(context.Background()); err != nil {
t.Fatal(err)
}
if err := readFileProc.Start(context.Background()); err != nil {
t.Fatal(err)
}
if err := csvReader.Start(context.Background()); err != nil {
t.Fatal(err)
}
if err := filter.Start(context.Background()); err != nil {
t.Fatal(err)
}
if err := printer2Proc.Start(context.Background()); err != nil {
t.Fatal(err)
}
if err := printerProc.Start(context.Background()); err != nil {
t.Fatal(err)
}
if err := writeFileProc.Start(context.Background()); err != nil {
t.Fatal(err)
}
time.Sleep(1 * time.Second)
//t.Logf("%+v", writeFileProc.Metric.GetMetric(fmt.Sprintf("%s_%d_payloads_in", writeFileProc.Name, writeFileProc.ID)))
// Compare metrics so that they Match
}