use the single tracker instance

This commit is contained in:
Joel Reymont 2015-07-03 16:53:42 +02:00
parent 48bc495430
commit 244ac5d165
2 changed files with 25 additions and 16 deletions

15
tail.go
View File

@ -64,6 +64,8 @@ type Tail struct {
file *os.File file *os.File
reader *bufio.Reader reader *bufio.Reader
tracker *watch.InotifyTracker
watcher watch.FileWatcher watcher watch.FileWatcher
changes *watch.FileChanges changes *watch.FileChanges
@ -100,7 +102,12 @@ func TailFile(filename string, config Config) (*Tail, error) {
if t.Poll { if t.Poll {
t.watcher = watch.NewPollingFileWatcher(filename) t.watcher = watch.NewPollingFileWatcher(filename)
} else { } else {
t.watcher = watch.NewInotifyFileWatcher(filename) t.tracker = watch.NewInotifyTracker()
w, err := t.tracker.NewWatcher()
if err != nil {
return nil, err
}
t.watcher = watch.NewInotifyFileWatcher(filename, w)
} }
if t.MustExist { if t.MustExist {
@ -382,6 +389,8 @@ func (tail *Tail) sendLine(line string) bool {
// Cleanup removes inotify watches added by the tail package. This function is // Cleanup removes inotify watches added by the tail package. This function is
// meant to be invoked from a process's exit handler. Linux kernel may not // meant to be invoked from a process's exit handler. Linux kernel may not
// automatically remove inotify watches after the process exits. // automatically remove inotify watches after the process exits.
func Cleanup() { func (tail *Tail) Cleanup() {
watch.Cleanup() if tail.tracker != nil {
tail.tracker.CloseAll()
}
} }

View File

@ -41,7 +41,7 @@ func TestMustExist(t *testing.T) {
t.Error("MustExist:true on an existing file is violated") t.Error("MustExist:true on an existing file is violated")
} }
tail.Stop() tail.Stop()
Cleanup() tail.Cleanup()
} }
func TestStop(t *testing.T) { func TestStop(t *testing.T) {
@ -52,7 +52,7 @@ func TestStop(t *testing.T) {
if tail.Stop() != nil { if tail.Stop() != nil {
t.Error("Should be stoped successfully") t.Error("Should be stoped successfully")
} }
Cleanup() tail.Cleanup()
} }
func MaxLineSizeT(_t *testing.T, follow bool, fileContent string, expected []string) { func MaxLineSizeT(_t *testing.T, follow bool, fileContent string, expected []string) {
@ -66,7 +66,7 @@ func MaxLineSizeT(_t *testing.T, follow bool, fileContent string, expected []str
<-time.After(100 * time.Millisecond) <-time.After(100 * time.Millisecond)
t.RemoveFile("test.txt") t.RemoveFile("test.txt")
tail.Stop() tail.Stop()
Cleanup() tail.Cleanup()
} }
func TestMaxLineSizeFollow(_t *testing.T) { func TestMaxLineSizeFollow(_t *testing.T) {
@ -90,7 +90,7 @@ func TestOver4096ByteLine(_t *testing.T) {
<-time.After(100 * time.Millisecond) <-time.After(100 * time.Millisecond)
t.RemoveFile("test.txt") t.RemoveFile("test.txt")
tail.Stop() tail.Stop()
Cleanup() tail.Cleanup()
} }
func TestOver4096ByteLineWithSetMaxLineSize(_t *testing.T) { func TestOver4096ByteLineWithSetMaxLineSize(_t *testing.T) {
t := NewTailTest("Over4096ByteLineMaxLineSize", _t) t := NewTailTest("Over4096ByteLineMaxLineSize", _t)
@ -104,7 +104,7 @@ func TestOver4096ByteLineWithSetMaxLineSize(_t *testing.T) {
<-time.After(100 * time.Millisecond) <-time.After(100 * time.Millisecond)
t.RemoveFile("test.txt") t.RemoveFile("test.txt")
// tail.Stop() // tail.Stop()
Cleanup() tail.Cleanup()
} }
func TestLocationFull(_t *testing.T) { func TestLocationFull(_t *testing.T) {
@ -118,7 +118,7 @@ func TestLocationFull(_t *testing.T) {
<-time.After(100 * time.Millisecond) <-time.After(100 * time.Millisecond)
t.RemoveFile("test.txt") t.RemoveFile("test.txt")
tail.Stop() tail.Stop()
Cleanup() tail.Cleanup()
} }
func TestLocationFullDontFollow(_t *testing.T) { func TestLocationFullDontFollow(_t *testing.T) {
@ -133,7 +133,7 @@ func TestLocationFullDontFollow(_t *testing.T) {
<-time.After(100 * time.Millisecond) <-time.After(100 * time.Millisecond)
tail.Stop() tail.Stop()
Cleanup() tail.Cleanup()
} }
func TestLocationEnd(_t *testing.T) { func TestLocationEnd(_t *testing.T) {
@ -150,7 +150,7 @@ func TestLocationEnd(_t *testing.T) {
<-time.After(100 * time.Millisecond) <-time.After(100 * time.Millisecond)
t.RemoveFile("test.txt") t.RemoveFile("test.txt")
tail.Stop() tail.Stop()
Cleanup() tail.Cleanup()
} }
func TestLocationMiddle(_t *testing.T) { func TestLocationMiddle(_t *testing.T) {
@ -168,7 +168,7 @@ func TestLocationMiddle(_t *testing.T) {
<-time.After(100 * time.Millisecond) <-time.After(100 * time.Millisecond)
t.RemoveFile("test.txt") t.RemoveFile("test.txt")
tail.Stop() tail.Stop()
Cleanup() tail.Cleanup()
} }
func _TestReOpen(_t *testing.T, poll bool) { func _TestReOpen(_t *testing.T, poll bool) {
@ -211,7 +211,7 @@ func _TestReOpen(_t *testing.T, poll bool) {
// the reading of data written above. Timings can vary based on // the reading of data written above. Timings can vary based on
// test environment. // test environment.
// tail.Stop() // tail.Stop()
Cleanup() tail.Cleanup()
} }
// The use of polling file watcher could affect file rotation // The use of polling file watcher could affect file rotation
@ -254,7 +254,7 @@ func _TestReSeek(_t *testing.T, poll bool) {
// the reading of data written above. Timings can vary based on // the reading of data written above. Timings can vary based on
// test environment. // test environment.
// tail.Stop() // tail.Stop()
Cleanup() tail.Cleanup()
} }
// The use of polling file watcher could affect file rotation // The use of polling file watcher could affect file rotation
@ -294,7 +294,7 @@ func TestRateLimiting(_t *testing.T) {
t.RemoveFile("test.txt") t.RemoveFile("test.txt")
// tail.Stop() // tail.Stop()
Cleanup() tail.Cleanup()
} }
func TestTell(_t *testing.T) { func TestTell(_t *testing.T) {
@ -328,7 +328,7 @@ func TestTell(_t *testing.T) {
} }
t.RemoveFile("test.txt") t.RemoveFile("test.txt")
tail.Done() tail.Done()
Cleanup() tail.Cleanup()
} }
// Test library // Test library