-
Notifications
You must be signed in to change notification settings - Fork 20
/
line_processor_test.go
139 lines (118 loc) · 3.23 KB
/
line_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
package funnel
import (
"bytes"
"reflect"
"regexp"
"testing"
"text/template"
)
func TestGetLineProcessor(t *testing.T) {
cfg := &Config{
DirName: "something",
}
lp := GetLineProcessor(cfg)
if _, ok := lp.(*NoProcessor); !ok {
t.Errorf("Incorrect line processor returned. Expected *funnel.NoProcessor, Got %s", reflect.TypeOf(lp))
}
cfg = &Config{
DirName: "something",
PrependValue: "prepender] ",
}
lp = GetLineProcessor(cfg)
if _, ok := lp.(*SimpleLineProcessor); !ok {
t.Errorf("Incorrect line processor returned. Expected *funnel.SimpleLineProcessor, Got %s", reflect.TypeOf(lp))
}
cfg = &Config{
DirName: "something",
PrependValue: "prepender {{.Timestamp}}",
}
lp = GetLineProcessor(cfg)
if _, ok := lp.(*TemplateLineProcessor); !ok {
t.Errorf("Incorrect line processor returned. Expected *funnel.TemplateLineProcessor, Got %s", reflect.TypeOf(lp))
}
}
func TestNoProcessor(t *testing.T) {
lp := &NoProcessor{}
line := "write this line"
var b bytes.Buffer
err := lp.Write(&b, line)
if err != nil {
t.Fatal(err)
return
}
if b.String() != line {
t.Errorf("Did not match. Expected \"%s\", Got \"%s\"", line, b.String())
}
}
func TestSimpleProcessor(t *testing.T) {
lp := &SimpleLineProcessor{prependStr: "prepend this"}
line := "write this line"
var b bytes.Buffer
err := lp.Write(&b, line)
if err != nil {
t.Fatal(err)
return
}
if b.String() != lp.prependStr+line {
t.Errorf("Did not match. Expected \"%s\", Got \"%s\"", lp.prependStr+line, b.String())
}
}
func TestTemplateProcessor(t *testing.T) {
tpl := template.Must(template.New("line").Parse("[myapp {{.UnixTimestamp}}]- "))
lp := &TemplateLineProcessor{template: tpl}
line := "write this line"
var b bytes.Buffer
err := lp.Write(&b, line)
if err != nil {
t.Fatal(err)
}
regexStr := "[myapp [0-9]{19}]- " + line
matched, err := regexp.MatchString(regexStr, b.String())
if err != nil {
t.Fatal(err)
}
if !matched {
t.Errorf("Did not match. Expected \"%s\", Got \"%s\"", regexStr, b.String())
}
// Testing ISO8601 timestamp
b.Reset()
// 2018-01-03T10:53:03+0530
tpl = template.Must(template.New("line").Parse("[myapp {{.ISO8601Timestamp}}]- "))
lp = &TemplateLineProcessor{template: tpl}
line = "write this line"
err = lp.Write(&b, line)
if err != nil {
t.Fatal(err)
}
regexStr = "[myapp [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[Z|+|-]([0-9]{4})?]- " + line
matched, err = regexp.MatchString(regexStr, b.String())
if err != nil {
t.Fatal(err)
return
}
if !matched {
t.Errorf("Did not match. Expected \"%s\", Got \"%s\"", regexStr, b.String())
}
}
func BenchmarkNoProcessor(b *testing.B) {
lp := &NoProcessor{}
var buff bytes.Buffer
for n := 0; n < b.N; n++ {
lp.Write(&buff, string(randStringBytes(50)))
}
}
func BenchmarkSimpleProcessor(b *testing.B) {
lp := &SimpleLineProcessor{prependStr: "prepend this"}
var buff bytes.Buffer
for n := 0; n < b.N; n++ {
lp.Write(&buff, string(randStringBytes(50)))
}
}
func BenchmarkTemplateProcessor(b *testing.B) {
tpl := template.Must(template.New("line").Parse("[myapp {{.UnixTimestamp}}]- "))
lp := &TemplateLineProcessor{template: tpl}
var buff bytes.Buffer
for n := 0; n < b.N; n++ {
lp.Write(&buff, string(randStringBytes(50)))
}
}