浏览代码

watch: skip setup if nothing is being watched (#1861)

Nick Santos 6 年之前
父节点
当前提交
37647bc600
共有 3 个文件被更改,包括 20 次插入0 次删除
  1. 12 0
      pkg/watch/notify_test.go
  2. 4 0
      pkg/watch/watcher_darwin.go
  3. 4 0
      pkg/watch/watcher_naive.go

+ 12 - 0
pkg/watch/notify_test.go

@@ -28,6 +28,14 @@ func TestNoEvents(t *testing.T) {
 	f.assertEvents()
 	f.assertEvents()
 }
 }
 
 
+func TestNoWatches(t *testing.T) {
+	f := newNotifyFixture(t)
+	defer f.tearDown()
+	f.paths = nil
+	f.rebuildWatcher()
+	f.assertEvents()
+}
+
 func TestEventOrdering(t *testing.T) {
 func TestEventOrdering(t *testing.T) {
 	f := newNotifyFixture(t)
 	f := newNotifyFixture(t)
 	defer f.tearDown()
 	defer f.tearDown()
@@ -586,6 +594,10 @@ func (f *notifyFixture) consumeEventsInBackground(ctx context.Context) chan erro
 }
 }
 
 
 func (f *notifyFixture) fsync() {
 func (f *notifyFixture) fsync() {
+	if len(f.paths) == 0 {
+		return
+	}
+
 	syncPathBase := fmt.Sprintf("sync-%d.txt", time.Now().UnixNano())
 	syncPathBase := fmt.Sprintf("sync-%d.txt", time.Now().UnixNano())
 	syncPath := filepath.Join(f.paths[0], syncPathBase)
 	syncPath := filepath.Join(f.paths[0], syncPathBase)
 	anySyncPath := filepath.Join(f.paths[0], "sync-")
 	anySyncPath := filepath.Join(f.paths[0], "sync-")

+ 4 - 0
pkg/watch/watcher_darwin.go

@@ -89,6 +89,10 @@ func (d *darwinNotify) initAdd(name string) {
 }
 }
 
 
 func (d *darwinNotify) Start() error {
 func (d *darwinNotify) Start() error {
+	if len(d.stream.Paths) == 0 {
+		return nil
+	}
+
 	numberOfWatches.Add(int64(len(d.stream.Paths)))
 	numberOfWatches.Add(int64(len(d.stream.Paths)))
 
 
 	d.stream.Start()
 	d.stream.Start()

+ 4 - 0
pkg/watch/watcher_naive.go

@@ -35,6 +35,10 @@ type naiveNotify struct {
 }
 }
 
 
 func (d *naiveNotify) Start() error {
 func (d *naiveNotify) Start() error {
+	if len(d.notifyList) == 0 {
+		return nil
+	}
+
 	for name := range d.notifyList {
 	for name := range d.notifyList {
 		fi, err := os.Stat(name)
 		fi, err := os.Stat(name)
 		if err != nil && !os.IsNotExist(err) {
 		if err != nil && !os.IsNotExist(err) {