github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/exp/fsnotify/fsnotify_symlink_test.go (about) 1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build freebsd openbsd netbsd darwin linux 6 7 package fsnotify 8 9 import ( 10 "os" 11 "path/filepath" 12 "testing" 13 "time" 14 ) 15 16 func TestFsnotifyFakeSymlink(t *testing.T) { 17 watcher := newWatcher(t) 18 19 // Create directory to watch 20 testDir := tempMkdir(t) 21 defer os.RemoveAll(testDir) 22 23 var errorsReceived counter 24 // Receive errors on the error channel on a separate goroutine 25 go func() { 26 for errors := range watcher.Error { 27 t.Logf("Received error: %s", errors) 28 errorsReceived.increment() 29 } 30 }() 31 32 // Count the CREATE events received 33 var createEventsReceived, otherEventsReceived counter 34 go func() { 35 for ev := range watcher.Event { 36 t.Logf("event received: %s", ev) 37 if ev.IsCreate() { 38 createEventsReceived.increment() 39 } else { 40 otherEventsReceived.increment() 41 } 42 } 43 }() 44 45 addWatch(t, watcher, testDir) 46 47 if err := os.Symlink(filepath.Join(testDir, "zzz"), filepath.Join(testDir, "zzznew")); err != nil { 48 t.Fatalf("Failed to create bogus symlink: %s", err) 49 } 50 t.Logf("Created bogus symlink") 51 52 // We expect this event to be received almost immediately, but let's wait 500 ms to be sure 53 time.Sleep(500 * time.Millisecond) 54 55 // Should not be error, just no events for broken links (watching nothing) 56 if errorsReceived.value() > 0 { 57 t.Fatal("fsnotify errors have been received.") 58 } 59 if otherEventsReceived.value() > 0 { 60 t.Fatal("fsnotify other events received on the broken link") 61 } 62 63 // Except for 1 create event (for the link itself) 64 if createEventsReceived.value() == 0 { 65 t.Fatal("fsnotify create events were not received after 500 ms") 66 } 67 if createEventsReceived.value() > 1 { 68 t.Fatal("fsnotify more create events received than expected") 69 } 70 71 // Try closing the fsnotify instance 72 t.Log("calling Close()") 73 watcher.Close() 74 }