]> go.fuhry.dev Git - fsnotify.git/commitdiff
test for kevent buffer data race from @tilaks
authorNathan Youngman <git@nathany.com>
Sun, 22 Jun 2014 05:10:24 +0000 (23:10 -0600)
committerNathan Youngman <git@nathany.com>
Sat, 28 Jun 2014 20:07:50 +0000 (14:07 -0600)
`go test -test.run=TestConcurrentRemovalOfWatch -test.cpu=1,1,1,1,1 -race`

from CL https://codereview.appspot.com/103300045/

fsnotify_darwin_test.go [new file with mode: 0644]

diff --git a/fsnotify_darwin_test.go b/fsnotify_darwin_test.go
new file mode 100644 (file)
index 0000000..845b097
--- /dev/null
@@ -0,0 +1,53 @@
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build darwin
+
+package fsnotify
+
+import (
+       "os"
+       "path/filepath"
+       "testing"
+)
+
+// TestConcurrentRemovalOfWatch tests that concurrent calls to RemoveWatch do not race.
+// See https://codereview.appspot.com/103300045/
+// go test -test.run=TestConcurrentRemovalOfWatch -test.cpu=1,1,1,1,1 -race
+func TestConcurrentRemovalOfWatch(t *testing.T) {
+       // Create directory to watch
+       testDir := tempMkdir(t)
+       defer os.RemoveAll(testDir)
+
+       // Create a file before watching directory
+       testFileAlreadyExists := filepath.Join(testDir, "TestFsnotifyEventsExisting.testfile")
+       {
+               var f *os.File
+               f, err := os.OpenFile(testFileAlreadyExists, os.O_WRONLY|os.O_CREATE, 0666)
+               if err != nil {
+                       t.Fatalf("creating test file failed: %s", err)
+               }
+               f.Sync()
+               f.Close()
+       }
+
+       watcher := newWatcher(t)
+       defer watcher.Close()
+
+       addWatch(t, watcher, testDir)
+
+       // Test that RemoveWatch can be invoked concurrently, with no data races.
+       removed1 := make(chan struct{})
+       go func() {
+               defer close(removed1)
+               watcher.Remove(testDir)
+       }()
+       removed2 := make(chan struct{})
+       go func() {
+               close(removed2)
+               watcher.Remove(testDir)
+       }()
+       <-removed1
+       <-removed2
+}