From 244ac5d165b8b545611925072a4c84a754600700 Mon Sep 17 00:00:00 2001 From: Joel Reymont Date: Fri, 3 Jul 2015 16:53:42 +0200 Subject: [PATCH] use the single tracker instance --- tail.go | 15 ++++++++++++--- tail_test.go | 26 +++++++++++++------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/tail.go b/tail.go index d746f4a..93d40b7 100644 --- a/tail.go +++ b/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() + } } diff --git a/tail_test.go b/tail_test.go index ffac3b5..6095f4f 100644 --- a/tail_test.go +++ b/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