From: Nathan Youngman Date: Sun, 22 Jun 2014 05:10:24 +0000 (-0600) Subject: test for kevent buffer data race from @tilaks X-Git-Tag: v1.7.2~306 X-Git-Url: https://go.fuhry.dev/?a=commitdiff_plain;h=3ccc938ff5e27a322e5aa11a6c6e7e43cc4e9ec5;p=fsnotify.git test for kevent buffer data race from @tilaks `go test -test.run=TestConcurrentRemovalOfWatch -test.cpu=1,1,1,1,1 -race` from CL https://codereview.appspot.com/103300045/ --- diff --git a/fsnotify_darwin_test.go b/fsnotify_darwin_test.go new file mode 100644 index 0000000..845b097 --- /dev/null +++ b/fsnotify_darwin_test.go @@ -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 +}