-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogfile_test.go
93 lines (85 loc) · 1.95 KB
/
logfile_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
package graphblast
import (
"errors"
"strings"
"testing"
)
func TestLogFileAdd(t *testing.T) {
lf := NewLogFile()
lf.Add("line1", nil)
if lf.Count != 1 {
t.Errorf("Add didn't increment the count")
}
if lf.Errors != 0 {
t.Errorf("Add incremented the error count")
}
if len(lf.Values) != 1 {
t.Errorf("Add didn't store the data")
}
if lf.Values["0"] != "line1" {
t.Errorf("Add didn't correctly store the right data")
}
}
func TestLogFileAddError(t *testing.T) {
lf := NewLogFile()
lf.Add("line1", errors.New("fail"))
if lf.Errors != 1 {
t.Errorf("Add with error didn't increment the error count")
}
if lf.Count != 0 {
t.Errorf("Add with error incremented the count")
}
if len(lf.Values) != 0 {
t.Errorf("Add with error stored some data")
}
}
func TestLogFileAddWindow(t *testing.T) {
lf := NewLogFile()
lf.Window = 1
lf.Add("line1", nil)
lf.Add("line2", nil)
if lf.Errors != 0 {
t.Errorf("Add incremented the error count")
}
if lf.Count != 2 {
t.Errorf("Add didn't increment the count")
}
if len(lf.Values) != 1 {
t.Errorf("Add didn't window the data")
}
if lf.Values["1"] != "line2" {
t.Errorf("Add didn't correctly drop data when windowing")
}
}
func TestLogFileChanged(t *testing.T) {
lf := NewLogFile()
changed, next := lf.Changed(0)
if changed {
t.Error("log should be unchanged")
}
if next != 0 {
t.Error("Changed should return indicator when nothing has changed")
}
lf.Add("line1", nil)
changed, next = lf.Changed(0)
if !changed {
t.Error("log should be changed")
}
if next <= 0 {
t.Error("Changed should return a new indicator after changes")
}
}
func TestLogFileRead(t *testing.T) {
reader := strings.NewReader("line1\nline2\n")
lf := NewLogFile()
lf.Read(reader)
if lf.Count != 2 {
t.Error("Read failed to read the input fully")
}
if lf.Errors != 0 {
t.Error("Read failed to read the input without errors")
}
if lf.Values["1"] != "line2" {
t.Error("Read failed to read the correct values")
}
}