-
Notifications
You must be signed in to change notification settings - Fork 20
/
consumer_test.go
336 lines (299 loc) · 7.95 KB
/
consumer_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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package funnel
import (
"bytes"
"io/ioutil"
"math/rand"
"os"
"path"
"strings"
"syscall"
"testing"
)
func TestRollover(t *testing.T) {
dir, c := setupTest(t)
defer os.RemoveAll(dir)
f, err := os.Open("testdata/file_84lines")
if err != nil {
t.Fatal(err)
return
}
defer f.Close()
c.Start(f)
// testing results
files := readTestDir(t, dir)
sep := []byte{'\n'}
if len(files) != 3 {
t.Errorf("Incorrect no. of files created. Expected 3, Got %d", len(files))
}
for i, file := range files {
data, err := ioutil.ReadFile(path.Join(dir, file.Name()))
if err != nil {
t.Fatal(err)
continue
}
numLines := bytes.Count(data, sep)
// First 2 files will have 40 lines
// last one will have 4 lines
if i < 2 {
if numLines != 40 {
t.Errorf("Incorrect no. of lines created in file #%d. Expected 40, Got %d", i, numLines)
}
} else {
if numLines != 4 {
t.Errorf("Incorrect no. of lines created in file #%d. Expected 4, Got %d", i, numLines)
}
}
}
}
func TestHugeLine(t *testing.T) {
dir, c := setupTest(t)
defer os.RemoveAll(dir)
// This file also contains arabic, indian and tibetan characters
// to test any ascii-utf8 codec incompatibility
targetBytes, err := ioutil.ReadFile("testdata/file_bigline")
if err != nil {
t.Fatal(err)
return
}
r := bytes.NewReader(targetBytes)
c.Start(r)
// testing results
files := readTestDir(t, dir)
if len(files) != 1 {
t.Errorf("Incorrect no. of files created. Expected 1, Got %d", len(files))
}
for _, file := range files {
data, err := ioutil.ReadFile(path.Join(dir, file.Name()))
if err != nil {
t.Fatal(err)
continue
}
// removing the newline character at the end
cmp := bytes.Compare(data, targetBytes)
if cmp != 0 {
t.Errorf("Incorrect string found. Expected- %s, Found- %s", string(targetBytes), string(data))
}
}
}
func TestNewLines(t *testing.T) {
dir, c := setupTest(t)
defer os.RemoveAll(dir)
// This file also contains arabic, indian and tibetan characters
// to test any ascii-utf8 codec incompatibility
targetBytes, err := ioutil.ReadFile("testdata/file_newline")
if err != nil {
t.Fatal(err)
return
}
r := bytes.NewReader(targetBytes)
c.Start(r)
// testing results
files := readTestDir(t, dir)
if len(files) != 1 {
t.Errorf("Incorrect no. of files created. Expected 1, Got %d", len(files))
}
for _, file := range files {
data, err := ioutil.ReadFile(path.Join(dir, file.Name()))
if err != nil {
t.Fatal(err)
continue
}
cmp := bytes.Compare(data, targetBytes)
if cmp != 0 {
t.Errorf("Incorrect string found. Expected- %s, Found- %s", string(targetBytes), string(data))
}
}
}
func TestEmptyString(t *testing.T) {
dir, c := setupTest(t)
defer os.RemoveAll(dir)
r := strings.NewReader("")
c.Start(r)
// testing results
files := readTestDir(t, dir)
if len(files) != 1 {
t.Errorf("Incorrect no. of files created. Expected 1, Got %d", len(files))
}
for _, file := range files {
data, err := ioutil.ReadFile(path.Join(dir, file.Name()))
if err != nil {
t.Fatal(err)
continue
}
cmp := bytes.Compare(data, []byte(""))
if cmp != 0 {
t.Errorf("Incorrect string found. Expected- %s, Found- %s", "", string(data))
}
}
}
func TestRolloverSerial(t *testing.T) {
dir, c := setupTest(t)
c.Config.FileRenamePolicy = "serial"
defer os.RemoveAll(dir)
f, err := os.Open("testdata/file_84lines")
if err != nil {
t.Fatal(err)
return
}
defer f.Close()
c.Start(f)
// testing results
files := readTestDir(t, dir)
sep := []byte{'\n'}
if len(files) != 3 {
t.Errorf("Incorrect no. of files created. Expected 3, Got %d", len(files))
}
for i, file := range files {
data, err := ioutil.ReadFile(path.Join(dir, file.Name()))
if err != nil {
t.Fatal(err)
continue
}
numLines := bytes.Count(data, sep)
// First file will have 4 lines
// rest will have 40 lines
if i == 0 {
if numLines != 4 {
t.Errorf("Incorrect no. of lines created in file #%d. Expected 4, Got %d", i, numLines)
}
} else {
if numLines != 40 {
t.Errorf("Incorrect no. of lines created in file #%d. Expected 40, Got %d", i, numLines)
}
}
}
}
func TestSendInterruptSerial(t *testing.T) {
// TODO
}
// Commenting this unless I can fix the flakiness
// the line2 sometimes gets written after the config change. Need to fix that.
/*func TestConfigReload(t *testing.T) {
dir, c := setupTest(t)
c.Config.FileRenamePolicy = "serial"
defer os.RemoveAll(dir)
// Setting up this reader, writer pipe because we want to write some,
// reload config and then again write some
rdr, wtr := io.Pipe()
var wg sync.WaitGroup
wg.Add(1)
go func() {
c.Start(rdr)
wg.Done()
}()
line1 := "hello this is a line\n"
line2 := "hello this is another line\n"
wtr.Write([]byte(line1))
wtr.Write([]byte(line2))
c.ReloadChan <- &Config{
DirName: dir,
ActiveFileName: "out.log",
RotationMaxLines: 40,
RotationMaxBytes: 1000000,
PrependValue: "[agniva]",
FileRenamePolicy: "serial",
MaxAge: int64(1 * 60 * 60),
MaxCount: 500,
Target: "file",
}
wtr.Write([]byte("trying again with a line\n"))
wtr.Write([]byte("trying again with another line"))
wtr.Close()
wg.Wait()
// testing results
files := readTestDir(t, dir)
if len(files) != 2 {
t.Errorf("Incorrect no. of files created. Expected 1, Got %d", len(files))
}
data, err := ioutil.ReadFile(path.Join(dir, "out.log.1"))
if err != nil {
t.Fatal(err)
return
}
cmp := bytes.Compare(data, []byte("[agniva]trying again with a line\n[agniva]trying again with another line"))
if cmp != 0 {
t.Errorf("Incorrect string found. Expected- %s, Found- %s", "[agniva]trying again with a line\n[agniva]trying again with another line", string(data))
}
data2, err := ioutil.ReadFile(path.Join(dir, "out.log.2"))
if err != nil {
t.Fatal(err)
return
}
cmp2 := bytes.Compare(data2, []byte(line1+line2))
if cmp2 != 0 {
t.Errorf("Incorrect string found. Expected- %s, Found- %s", line1+line2, string(data2))
}
}*/
// Benchmarking different file creation and status flags to check write speed
func benchmarkFileIO(b *testing.B, flags int) {
dir, _ := ioutil.TempDir("", "test")
f, _ := os.OpenFile(path.Join(dir, "testspeed"),
flags,
0644)
defer f.Sync()
defer f.Close()
defer os.Remove(f.Name())
defer os.RemoveAll(dir)
for n := 0; n < b.N; n++ {
f.Write(randStringBytes(50))
}
}
func BenchmarkFileIO_Append(b *testing.B) {
benchmarkFileIO(b, os.O_CREATE|os.O_WRONLY|os.O_APPEND)
}
func BenchmarkFileIO_Normal(b *testing.B) {
benchmarkFileIO(b, os.O_CREATE|os.O_WRONLY)
}
func BenchmarkFileIO_Sync(b *testing.B) {
benchmarkFileIO(b, os.O_CREATE|os.O_WRONLY|os.O_SYNC)
}
func BenchmarkFileIO_DSync(b *testing.B) {
benchmarkFileIO(b, os.O_CREATE|os.O_WRONLY|syscall.O_DSYNC)
}
func BenchmarkFileIO_AppendSync(b *testing.B) {
benchmarkFileIO(b, os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_SYNC)
}
func BenchmarkFileIO_AppendDSync(b *testing.B) {
benchmarkFileIO(b, os.O_CREATE|os.O_WRONLY|os.O_APPEND|syscall.O_DSYNC)
}
// Internal helper functions
func setupTest(t *testing.T) (string, *Consumer) {
dir, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatal(err)
return "", nil
}
c := &Consumer{
Config: &Config{
DirName: dir,
ActiveFileName: "out.log",
RotationMaxLines: 40,
RotationMaxBytes: 1000000,
FlushingTimeIntervalSecs: 5,
FileRenamePolicy: "timestamp",
MaxAge: int64(1 * 60 * 60),
MaxCount: 500,
Target: "file",
},
LineProcessor: &NoProcessor{},
ReloadChan: make(chan *Config),
}
return dir, c
}
func readTestDir(t *testing.T, dir string) []os.FileInfo {
files, err := ioutil.ReadDir(dir)
if err != nil {
t.Fatal(err)
return nil
}
return files
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func randStringBytes(n int) []byte {
b := make([]byte, n)
for i := 0; i < n-1; i++ {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
b[n-1] = '\n'
return b
}