This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
tail_test.go
66 lines (57 loc) · 1.83 KB
/
tail_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
package dendrite
import (
"encoding/json"
"io/ioutil"
"os"
"path"
"testing"
"time"
)
type FixedTimeProvider struct{}
func (*FixedTimeProvider) Now() time.Time {
return time.Unix(1234567890, 0)
}
var parser Parser
var offsetFile string
var tail *Tail
var output chan Record
var line = "{\"_file\":{\"Type\":0,\"Treatment\":0,\"Value\":\"solr.txt\"},\"_group\":{\"Type\":0,\"Treatment\":0,\"Value\":\"foo\"},\"_hostname\":{\"Type\":0,\"Treatment\":0,\"Value\":\"host.local\"},\"_offset\":{\"Type\":1,\"Treatment\":0,\"Value\":0},\"_time\":{\"Type\":3,\"Treatment\":0,\"Value\":1234567890},\"line\":{\"Type\":0,\"Treatment\":0,\"Value\":\"INFO: [1234567898765] webapp=/solr path=/select params={start=0&q=*:*&wt=ruby&fq=type:User&rows=30} hits=3186235 status=0 QTime=1\"}}"
func _tail_init() {
StandardTimeProvider = new(FixedTimeProvider)
output = make(chan Record, 100)
offsetFile = path.Join(os.TempDir(), "test.txt")
_ = os.Remove(offsetFile)
parser = NewRegexpParser("host.local", "foo", "solr.txt", output, "(?P<line>.*[^\r])\r?\n", nil, 32768)
tail = NewTail(parser, -1, "testdata/solr.txt", offsetFile, 0)
}
func TestStartsAtZero(t *testing.T) {
_tail_init()
if tail.Offset() != 0 {
t.Error("initial offset wasn't zero")
}
}
func TestStartsAtOffset(t *testing.T) {
_tail_init()
ioutil.WriteFile(offsetFile, []byte("747\n"), 0777)
tail = NewTail(parser, -1, "testdata/solr.txt", offsetFile, 0)
if tail.Offset() != 747 {
t.Errorf("initial offset was %d, not 747", tail.Offset())
}
}
func TestReading(t *testing.T) {
_tail_init()
go tail.Poll()
rec := <-output
json, _ := json.Marshal(rec)
if string(json) != line {
t.Errorf("Oops, diff between\n %s\n %s", string(json), line)
}
}
func TestOffsetUpdated(t *testing.T) {
_tail_init()
go tail.Poll()
_ = <-output
if tail.Offset() == 0 {
t.Error("offset was zero")
}
}