watch: fix a flaky test by ignoring spurious events correctly (#162)

This commit is contained in:
Nick Santos 2018-08-23 11:56:00 -04:00 committed by Nicolas De loof
parent 3850a34114
commit 4801d2b1a4
2 changed files with 4 additions and 3 deletions

View File

@ -231,6 +231,7 @@ func TestSingleFile(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
f.fsync()
d2 := []byte("hello\nworld\n") d2 := []byte("hello\nworld\n")
err = ioutil.WriteFile(path, d2, 0644) err = ioutil.WriteFile(path, d2, 0644)

View File

@ -34,7 +34,7 @@ func (d *darwinNotify) isTrackingPath(path string) bool {
} }
func (d *darwinNotify) loop() { func (d *darwinNotify) loop() {
ignoredSpuriousEvent := false ignoredSpuriousEvents := make(map[string]bool, 0)
for { for {
select { select {
case <-d.stop: case <-d.stop:
@ -50,8 +50,8 @@ func (d *darwinNotify) loop() {
// ignore the first event that says the watched directory // ignore the first event that says the watched directory
// has been created. these are fired spuriously on initiation. // has been created. these are fired spuriously on initiation.
if e.Flags&fsevents.ItemCreated == fsevents.ItemCreated { if e.Flags&fsevents.ItemCreated == fsevents.ItemCreated {
if d.isTrackingPath(e.Path) && !ignoredSpuriousEvent { if !ignoredSpuriousEvents[e.Path] && d.isTrackingPath(e.Path) {
ignoredSpuriousEvent = true ignoredSpuriousEvents[e.Path] = true
continue continue
} }
} }