Skip to content

Commit 390d5cf

Browse files
nicksndeloof
authored andcommitted
watch: add tests for ignores and number of watches (docker#1838)
1 parent 7f6e189 commit 390d5cf

File tree

4 files changed

+113
-28
lines changed

4 files changed

+113
-28
lines changed

pkg/watch/notify.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package watch
22

3-
import "github.com/windmilleng/tilt/internal/logger"
3+
import (
4+
"expvar"
5+
6+
"github.com/windmilleng/tilt/internal/logger"
7+
)
8+
9+
var (
10+
numberOfWatches = expvar.NewInt("watch.naive.numberOfWatches")
11+
)
412

513
type FileEvent struct {
614
Path string

pkg/watch/notify_test.go

Lines changed: 96 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/stretchr/testify/assert"
1616

17+
"github.com/windmilleng/tilt/internal/dockerignore"
1718
"github.com/windmilleng/tilt/internal/logger"
1819
"github.com/windmilleng/tilt/internal/testutils/tempdir"
1920
)
@@ -412,36 +413,99 @@ func TestWatchNonexistentDirectory(t *testing.T) {
412413
}
413414
}
414415

415-
// doesn't work on linux
416-
// func TestWatchNonexistentFileInNonexistentDirectory(t *testing.T) {
417-
// f := newNotifyFixture(t)
418-
// defer f.tearDown()
416+
func TestWatchNonexistentFileInNonexistentDirectory(t *testing.T) {
417+
f := newNotifyFixture(t)
418+
defer f.tearDown()
419+
420+
root := f.JoinPath("root")
421+
err := os.Mkdir(root, 0777)
422+
if err != nil {
423+
t.Fatal(err)
424+
}
425+
parent := f.JoinPath("parent")
426+
file := f.JoinPath("parent", "a")
427+
428+
f.watch(file)
429+
f.assertEvents()
419430

420-
// root := f.JoinPath("root")
421-
// err := os.Mkdir(root, 0777)
422-
// if err != nil {
423-
// t.Fatal(err)
424-
// }
425-
// parent := f.JoinPath("parent")
426-
// file := f.JoinPath("parent", "a")
431+
err = os.Mkdir(parent, 0777)
432+
if err != nil {
433+
t.Fatal(err)
434+
}
427435

428-
// f.watch(file)
429-
// f.assertEvents()
436+
f.assertEvents()
437+
f.WriteFile(file, "hello")
438+
f.assertEvents(file)
439+
}
430440

431-
// err = os.Mkdir(parent, 0777)
432-
// if err != nil {
433-
// t.Fatal(err)
434-
// }
441+
func TestWatchCountInnerFile(t *testing.T) {
442+
f := newNotifyFixture(t)
443+
defer f.tearDown()
435444

436-
// f.assertEvents()
437-
// f.WriteFile(file, "hello")
438-
// f.assertEvents(file)
439-
// }
445+
root := f.paths[0]
446+
a := f.JoinPath(root, "a")
447+
b := f.JoinPath(a, "b")
448+
file := f.JoinPath(b, "bigFile")
449+
f.WriteFile(file, "hello")
450+
f.assertEvents(a, b, file)
451+
452+
expectedWatches := 3
453+
if runtime.GOOS == "darwin" {
454+
expectedWatches = 1
455+
}
456+
assert.Equal(t, expectedWatches, int(numberOfWatches.Value()))
457+
}
458+
459+
func TestWatchCountInnerFileWithIgnore(t *testing.T) {
460+
f := newNotifyFixture(t)
461+
defer f.tearDown()
462+
463+
root := f.paths[0]
464+
ignore, _ := dockerignore.NewDockerPatternMatcher(root, []string{
465+
"a",
466+
"!a/b",
467+
})
468+
f.setIgnore(ignore)
469+
470+
a := f.JoinPath(root, "a")
471+
b := f.JoinPath(a, "b")
472+
file := f.JoinPath(b, "bigFile")
473+
f.WriteFile(file, "hello")
474+
f.assertEvents(b, file)
475+
476+
expectedWatches := 3
477+
if runtime.GOOS == "darwin" {
478+
expectedWatches = 1
479+
}
480+
assert.Equal(t, expectedWatches, int(numberOfWatches.Value()))
481+
}
482+
483+
func TestIgnore(t *testing.T) {
484+
f := newNotifyFixture(t)
485+
defer f.tearDown()
486+
487+
root := f.paths[0]
488+
ignore, _ := dockerignore.NewDockerPatternMatcher(root, []string{"a/b"})
489+
f.setIgnore(ignore)
490+
491+
a := f.JoinPath(root, "a")
492+
b := f.JoinPath(a, "b")
493+
file := f.JoinPath(b, "bigFile")
494+
f.WriteFile(file, "hello")
495+
f.assertEvents(a)
496+
497+
expectedWatches := 3
498+
if runtime.GOOS == "darwin" {
499+
expectedWatches = 1
500+
}
501+
assert.Equal(t, expectedWatches, int(numberOfWatches.Value()))
502+
}
440503

441504
type notifyFixture struct {
442505
out *bytes.Buffer
443506
*tempdir.TempDirFixture
444507
notify Notify
508+
ignore PathMatcher
445509
paths []string
446510
events []FileEvent
447511
}
@@ -451,23 +515,32 @@ func newNotifyFixture(t *testing.T) *notifyFixture {
451515
nf := &notifyFixture{
452516
TempDirFixture: tempdir.NewTempDirFixture(t),
453517
paths: []string{},
518+
ignore: EmptyMatcher{},
454519
out: out,
455520
}
456521
nf.watch(nf.TempDir("watched"))
457522
return nf
458523
}
459524

525+
func (f *notifyFixture) setIgnore(ignore PathMatcher) {
526+
f.ignore = ignore
527+
f.rebuildWatcher()
528+
}
529+
460530
func (f *notifyFixture) watch(path string) {
461531
f.paths = append(f.paths, path)
532+
f.rebuildWatcher()
533+
}
462534

535+
func (f *notifyFixture) rebuildWatcher() {
463536
// sync any outstanding events and close the old watcher
464537
if f.notify != nil {
465538
f.fsync()
466539
f.closeWatcher()
467540
}
468541

469542
// create a new watcher
470-
notify, err := NewWatcher(f.paths, EmptyMatcher{}, logger.NewLogger(logger.DebugLvl, f.out))
543+
notify, err := NewWatcher(f.paths, f.ignore, logger.NewLogger(logger.DebugLvl, f.out))
471544
if err != nil {
472545
f.T().Fatal(err)
473546
}
@@ -569,4 +642,5 @@ func (f *notifyFixture) closeWatcher() {
569642
func (f *notifyFixture) tearDown() {
570643
f.closeWatcher()
571644
f.TempDirFixture.TearDown()
645+
numberOfWatches.Set(0)
572646
}

pkg/watch/watcher_darwin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ func (d *darwinNotify) initAdd(name string) {
8989
}
9090

9191
func (d *darwinNotify) Start() error {
92+
numberOfWatches.Add(int64(len(d.stream.Paths)))
93+
9294
d.stream.Start()
9395

9496
go d.loop()
@@ -97,6 +99,8 @@ func (d *darwinNotify) Start() error {
9799
}
98100

99101
func (d *darwinNotify) Close() error {
102+
numberOfWatches.Add(int64(-len(d.stream.Paths)))
103+
100104
d.stream.Stop()
101105
close(d.errors)
102106
close(d.stop)

pkg/watch/watcher_naive.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package watch
44

55
import (
6-
"expvar"
76
"fmt"
87
"os"
98
"path/filepath"
@@ -32,12 +31,9 @@ type naiveNotify struct {
3231
events chan fsnotify.Event
3332
wrappedEvents chan FileEvent
3433
errors chan error
34+
numWatches int64
3535
}
3636

37-
var (
38-
numberOfWatches = expvar.NewInt("watch.naive.numberOfWatches")
39-
)
40-
4137
func (d *naiveNotify) Start() error {
4238
for name := range d.notifyList {
4339
fi, err := os.Stat(name)
@@ -108,6 +104,8 @@ func (d *naiveNotify) watchAncestorOfMissingPath(path string) error {
108104
}
109105

110106
func (d *naiveNotify) Close() error {
107+
numberOfWatches.Add(-d.numWatches)
108+
d.numWatches = 0
111109
return d.watcher.Close()
112110
}
113111

@@ -191,6 +189,7 @@ func (d *naiveNotify) add(path string) error {
191189
if err != nil {
192190
return err
193191
}
192+
d.numWatches++
194193
numberOfWatches.Add(1)
195194
return nil
196195
}

0 commit comments

Comments
 (0)