Skip to content

Commit

Permalink
Merge pull request #8 from ploxiln/watcher_still_flaky
Browse files Browse the repository at this point in the history
tests: further deflake watcher tests for travis-ci
  • Loading branch information
ploxiln authored Nov 27, 2018
2 parents c0eb1ba + 487750f commit eebef9f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
12 changes: 9 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

[[constraint]]
name = "github.com/fsnotify/fsnotify"
version = "~1.2.0"
version = "^1.2.0"

[[constraint]]
branch = "master"
Expand Down
54 changes: 28 additions & 26 deletions watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func WaitForReplacement(filename string, watcher *fsnotify.Watcher) {
for i := 0; i < 20; i++ {
for i := 0; i < 50; i++ {
time.Sleep(100 * time.Millisecond)

if _, err := os.Stat(filename); err == nil {
Expand All @@ -25,39 +25,41 @@ func WaitForReplacement(filename string, watcher *fsnotify.Watcher) {
log.Printf("failed to resume watching for %s", filename)
}

func watchLoop(filename string, watcher *fsnotify.Watcher, done <-chan bool, action func()) {
for {
select {
case _ = <-done:
log.Printf("Shutting down watcher for: %s", filename)
watcher.Close()
return
case event := <-watcher.Events:
// On Arch Linux, it appears Chmod events precede Remove events,
// which causes a race between action() and the coming Remove event.
if event.Op == fsnotify.Chmod {
continue
}
if event.Op&(fsnotify.Remove|fsnotify.Rename) != 0 {
log.Printf("watching interrupted on event: %s", event)
watcher.Remove(filename)
WaitForReplacement(filename, watcher)
}
log.Printf("reloading after event: %s", event)
action()
case err := <-watcher.Errors:
log.Printf("error watching %s: %s", filename, err)
}
}
}

func WatchForUpdates(filename string, done <-chan bool, action func()) {
filename = filepath.Clean(filename)
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal("failed to create watcher for ", filename, ": ", err)
}
go func() {
for {
select {
case _ = <-done:
log.Printf("Shutting down watcher for: %s", filename)
watcher.Close()
return
case event := <-watcher.Events:
// On Arch Linux, it appears Chmod events precede Remove events,
// which causes a race between action() and the coming Remove event.
if event.Op == fsnotify.Chmod {
continue
}
if event.Op&(fsnotify.Remove|fsnotify.Rename) != 0 {
log.Printf("watching interrupted on event: %s", event)
watcher.Remove(filename)
WaitForReplacement(filename, watcher)
}
log.Printf("reloading after event: %s", event)
action()
case err := <-watcher.Errors:
log.Printf("error watching %s: %s", filename, err)
}
}
}()
if err = watcher.Add(filename); err != nil {
log.Fatal("failed to add ", filename, " to watcher: ", err)
}
go watchLoop(filename, watcher, done, action)
log.Printf("watching %s for updates", filename)
}

0 comments on commit eebef9f

Please sign in to comment.