-
Notifications
You must be signed in to change notification settings - Fork 2
/
reader_test.go
107 lines (87 loc) · 2.76 KB
/
reader_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
package snitch_test
import (
"os"
"strconv"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/xjewer/snitch"
"github.com/xjewer/snitch/lib/config"
"github.com/xjewer/snitch/lib/simplelog"
)
func Test_NewFileReaderWithZeroOffset(t *testing.T) {
a := assert.New(t)
const offsetFile = "./reader_test_offset1"
// starts with second line
err := cleanupOffsetFile(offsetFile, 0)
a.Nil(err)
expectations := []string{
"[22/Sep/2017:01:56:25 +0300] 200 192.168.1.1:80 : 127.0.0.1:8000 504 : 200 0.7 : 0.002 https POST /test /test Error hostname",
"[22/Sep/2017:01:56:26 +0300] 200 192.168.1.1:80 : 127.0.0.1:8000 504 : 200 0.7 : 0.002 https POST /test /test Error hostname",
"[22/Sep/2017:01:56:39 +0300] 200 192.168.1.1:80 200 0.036 https POST /test /test OK hostname",
"[22/Sep/2017:01:56:27 +0300] 200 192.168.1.1:80 : 127.0.0.1:8000 504 : 200 0.7 : 0.002 https POST /test /test Error hostname",
"[22/Sep/2017:01:56:40 +0300] 200 192.168.1.1:80 200 0.036 https POST /test /test OK hostname",
}
checkResults(a, expectations, offsetFile)
}
func Test_NewFileReaderWithOffset(t *testing.T) {
a := assert.New(t)
const offsetFile = "./reader_test_offset2"
// starts with second line
err := cleanupOffsetFile(offsetFile, 125)
a.Nil(err)
expectations := []string{
"[22/Sep/2017:01:56:26 +0300] 200 192.168.1.1:80 : 127.0.0.1:8000 504 : 200 0.7 : 0.002 https POST /test /test Error hostname",
"[22/Sep/2017:01:56:39 +0300] 200 192.168.1.1:80 200 0.036 https POST /test /test OK hostname",
"[22/Sep/2017:01:56:27 +0300] 200 192.168.1.1:80 : 127.0.0.1:8000 504 : 200 0.7 : 0.002 https POST /test /test Error hostname",
"[22/Sep/2017:01:56:40 +0300] 200 192.168.1.1:80 200 0.036 https POST /test /test OK hostname",
}
checkResults(a, expectations, offsetFile)
}
func checkResults(a *assert.Assertions, strs []string, offsetFile string) {
var wg sync.WaitGroup
cfg := config.Source{
Name: "test",
ReOpen: true,
File: "./reader_test.log",
OffsetFile: offsetFile,
MustExists: true,
}
l := &simplelog.NoopLogger{}
reader, err := snitch.NewFileReader(cfg, l)
if err != nil {
a.FailNow(err.Error())
}
lines := make(chan *snitch.Line)
wg.Add(1)
go func() {
reader.GetLines(lines)
wg.Done()
}()
read := func() {
for _, e := range strs {
select {
case l, ok := <-lines:
if !ok {
return
}
a.Equal(e, l.GetText(), "Strings should be equal")
}
}
}
read()
reader.Close()
close(lines)
wg.Wait()
os.Remove(offsetFile)
}
func cleanupOffsetFile(file string, offset int) error {
// savePosition saves the last position to specific file
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()
f.WriteString(strconv.Itoa(offset))
return nil
}