From ec461b87f0f43d68823feec83919e6c292caa7a7 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Mon, 27 May 2013 15:28:50 -0700 Subject: [PATCH] implement PollingFileWatcher.BlockUntilExists --- tail_test.go | 8 +++++--- watch.go | 11 +++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/tail_test.go b/tail_test.go index 9f1d4e4..56af1e5 100644 --- a/tail_test.go +++ b/tail_test.go @@ -101,25 +101,27 @@ func _TestReOpen(_t *testing.T, poll bool) { <-time.After(100 * time.Millisecond) t.CreateFile("test.txt", "more\ndata\n") if poll { + // Give polling a chance to read the just-written lines (more; + // data), before we recreate the file again below. <-time.After(POLL_DURATION) } // rename must trigger reopen <-time.After(100 * time.Millisecond) - println("going to rename") t.RenameFile("test.txt", "test.txt.rotated") <-time.After(100 * time.Millisecond) - t.CreateFile("test.txt", "endofworld") if poll { + // This time, wait a bit before creating the file to test + // PollingFileWatcher's BlockUntilExists. <-time.After(POLL_DURATION) } + t.CreateFile("test.txt", "endofworld") // Delete after a reasonable delay, to give tail sufficient time // to read all lines. <-time.After(100 * time.Millisecond) t.RemoveFile("test.txt") - println("Stopping tail") tail.Stop() } diff --git a/watch.go b/watch.go index 70d7253..35fc923 100644 --- a/watch.go +++ b/watch.go @@ -105,8 +105,15 @@ var POLL_DURATION time.Duration // BlockUntilExists blocks until the file comes into existence. If the // file already exists, then block until it is created again. func (fw *PollingFileWatcher) BlockUntilExists() error { - panic("not implemented") - return nil + for { + if _, err := os.Stat(fw.Filename); err == nil { + return nil + }else if !os.IsNotExist(err) { + return err + } + time.Sleep(POLL_DURATION) + println("blocking..") + } } func (fw *PollingFileWatcher) ChangeEvents(origFi os.FileInfo) chan bool {