use the single tracker instance
This commit is contained in:
parent
48bc495430
commit
244ac5d165
15
tail.go
15
tail.go
|
@ -64,6 +64,8 @@ type Tail struct {
|
|||
|
||||
file *os.File
|
||||
reader *bufio.Reader
|
||||
tracker *watch.InotifyTracker
|
||||
|
||||
watcher watch.FileWatcher
|
||||
changes *watch.FileChanges
|
||||
|
||||
|
@ -100,7 +102,12 @@ func TailFile(filename string, config Config) (*Tail, error) {
|
|||
if t.Poll {
|
||||
t.watcher = watch.NewPollingFileWatcher(filename)
|
||||
} 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 {
|
||||
|
@ -382,6 +389,8 @@ func (tail *Tail) sendLine(line string) bool {
|
|||
// 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
|
||||
// automatically remove inotify watches after the process exits.
|
||||
func Cleanup() {
|
||||
watch.Cleanup()
|
||||
func (tail *Tail) Cleanup() {
|
||||
if tail.tracker != nil {
|
||||
tail.tracker.CloseAll()
|
||||
}
|
||||
}
|
||||
|
|
26
tail_test.go
26
tail_test.go
|
@ -41,7 +41,7 @@ func TestMustExist(t *testing.T) {
|
|||
t.Error("MustExist:true on an existing file is violated")
|
||||
}
|
||||
tail.Stop()
|
||||
Cleanup()
|
||||
tail.Cleanup()
|
||||
}
|
||||
|
||||
func TestStop(t *testing.T) {
|
||||
|
@ -52,7 +52,7 @@ func TestStop(t *testing.T) {
|
|||
if tail.Stop() != nil {
|
||||
t.Error("Should be stoped successfully")
|
||||
}
|
||||
Cleanup()
|
||||
tail.Cleanup()
|
||||
}
|
||||
|
||||
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)
|
||||
t.RemoveFile("test.txt")
|
||||
tail.Stop()
|
||||
Cleanup()
|
||||
tail.Cleanup()
|
||||
}
|
||||
|
||||
func TestMaxLineSizeFollow(_t *testing.T) {
|
||||
|
@ -90,7 +90,7 @@ func TestOver4096ByteLine(_t *testing.T) {
|
|||
<-time.After(100 * time.Millisecond)
|
||||
t.RemoveFile("test.txt")
|
||||
tail.Stop()
|
||||
Cleanup()
|
||||
tail.Cleanup()
|
||||
}
|
||||
func TestOver4096ByteLineWithSetMaxLineSize(_t *testing.T) {
|
||||
t := NewTailTest("Over4096ByteLineMaxLineSize", _t)
|
||||
|
@ -104,7 +104,7 @@ func TestOver4096ByteLineWithSetMaxLineSize(_t *testing.T) {
|
|||
<-time.After(100 * time.Millisecond)
|
||||
t.RemoveFile("test.txt")
|
||||
// tail.Stop()
|
||||
Cleanup()
|
||||
tail.Cleanup()
|
||||
}
|
||||
|
||||
func TestLocationFull(_t *testing.T) {
|
||||
|
@ -118,7 +118,7 @@ func TestLocationFull(_t *testing.T) {
|
|||
<-time.After(100 * time.Millisecond)
|
||||
t.RemoveFile("test.txt")
|
||||
tail.Stop()
|
||||
Cleanup()
|
||||
tail.Cleanup()
|
||||
}
|
||||
|
||||
func TestLocationFullDontFollow(_t *testing.T) {
|
||||
|
@ -133,7 +133,7 @@ func TestLocationFullDontFollow(_t *testing.T) {
|
|||
<-time.After(100 * time.Millisecond)
|
||||
|
||||
tail.Stop()
|
||||
Cleanup()
|
||||
tail.Cleanup()
|
||||
}
|
||||
|
||||
func TestLocationEnd(_t *testing.T) {
|
||||
|
@ -150,7 +150,7 @@ func TestLocationEnd(_t *testing.T) {
|
|||
<-time.After(100 * time.Millisecond)
|
||||
t.RemoveFile("test.txt")
|
||||
tail.Stop()
|
||||
Cleanup()
|
||||
tail.Cleanup()
|
||||
}
|
||||
|
||||
func TestLocationMiddle(_t *testing.T) {
|
||||
|
@ -168,7 +168,7 @@ func TestLocationMiddle(_t *testing.T) {
|
|||
<-time.After(100 * time.Millisecond)
|
||||
t.RemoveFile("test.txt")
|
||||
tail.Stop()
|
||||
Cleanup()
|
||||
tail.Cleanup()
|
||||
}
|
||||
|
||||
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
|
||||
// test environment.
|
||||
// tail.Stop()
|
||||
Cleanup()
|
||||
tail.Cleanup()
|
||||
}
|
||||
|
||||
// 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
|
||||
// test environment.
|
||||
// tail.Stop()
|
||||
Cleanup()
|
||||
tail.Cleanup()
|
||||
}
|
||||
|
||||
// The use of polling file watcher could affect file rotation
|
||||
|
@ -294,7 +294,7 @@ func TestRateLimiting(_t *testing.T) {
|
|||
t.RemoveFile("test.txt")
|
||||
|
||||
// tail.Stop()
|
||||
Cleanup()
|
||||
tail.Cleanup()
|
||||
}
|
||||
|
||||
func TestTell(_t *testing.T) {
|
||||
|
@ -328,7 +328,7 @@ func TestTell(_t *testing.T) {
|
|||
}
|
||||
t.RemoveFile("test.txt")
|
||||
tail.Done()
|
||||
Cleanup()
|
||||
tail.Cleanup()
|
||||
}
|
||||
|
||||
// Test library
|
||||
|
|
Loading…
Reference in New Issue