implement PollingFileWatcher.BlockUntilExists

This commit is contained in:
Sridhar Ratnakumar 2013-05-27 15:28:50 -07:00
parent c5073c7f26
commit ec461b87f0
2 changed files with 14 additions and 5 deletions

View File

@ -101,25 +101,27 @@ func _TestReOpen(_t *testing.T, poll bool) {
<-time.After(100 * time.Millisecond) <-time.After(100 * time.Millisecond)
t.CreateFile("test.txt", "more\ndata\n") t.CreateFile("test.txt", "more\ndata\n")
if poll { 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) <-time.After(POLL_DURATION)
} }
// rename must trigger reopen // rename must trigger reopen
<-time.After(100 * time.Millisecond) <-time.After(100 * time.Millisecond)
println("going to rename")
t.RenameFile("test.txt", "test.txt.rotated") t.RenameFile("test.txt", "test.txt.rotated")
<-time.After(100 * time.Millisecond) <-time.After(100 * time.Millisecond)
t.CreateFile("test.txt", "endofworld")
if poll { if poll {
// This time, wait a bit before creating the file to test
// PollingFileWatcher's BlockUntilExists.
<-time.After(POLL_DURATION) <-time.After(POLL_DURATION)
} }
t.CreateFile("test.txt", "endofworld")
// Delete after a reasonable delay, to give tail sufficient time // Delete after a reasonable delay, to give tail sufficient time
// to read all lines. // to read all lines.
<-time.After(100 * time.Millisecond) <-time.After(100 * time.Millisecond)
t.RemoveFile("test.txt") t.RemoveFile("test.txt")
println("Stopping tail")
tail.Stop() tail.Stop()
} }

View File

@ -105,8 +105,15 @@ var POLL_DURATION time.Duration
// BlockUntilExists blocks until the file comes into existence. If the // BlockUntilExists blocks until the file comes into existence. If the
// file already exists, then block until it is created again. // file already exists, then block until it is created again.
func (fw *PollingFileWatcher) BlockUntilExists() error { func (fw *PollingFileWatcher) BlockUntilExists() error {
panic("not implemented") for {
if _, err := os.Stat(fw.Filename); err == nil {
return 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 { func (fw *PollingFileWatcher) ChangeEvents(origFi os.FileInfo) chan bool {