From: Chris Howey Date: Wed, 19 Oct 2011 23:43:27 +0000 (-0700) Subject: Tests based on receiving events we want X-Git-Tag: v1.7.2~453 X-Git-Url: https://go.fuhry.dev/?a=commitdiff_plain;h=7aed328fc153118aa99b930fd1ca3690f6aa1857;p=fsnotify.git Tests based on receiving events we want --- diff --git a/fsnotify_test.go b/fsnotify_test.go index 4759b8b..36de542 100644 --- a/fsnotify_test.go +++ b/fsnotify_test.go @@ -11,7 +11,7 @@ import ( "exec" ) -func TestFsnotifyEvents(t *testing.T) { +func TestFsnotifyDirOnly(t *testing.T) { // Create an fsnotify watcher instance and initialize it watcher, err := NewWatcher() if err != nil { @@ -38,6 +38,8 @@ func TestFsnotifyEvents(t *testing.T) { // Receive events on the event channel on a separate goroutine eventstream := watcher.Event var eventsReceived = 0 + var createReceived = 0 + var deleteReceived = 0 done := make(chan bool) go func() { for event := range eventstream { @@ -45,6 +47,12 @@ func TestFsnotifyEvents(t *testing.T) { if event.Name == testDir || event.Name == testFile { eventsReceived++ t.Logf("event received: %s", event) + if event.IsDelete() { + deleteReceived++ + } + if event.IsDelete() { + createReceived++ + } } else { t.Logf("unexpected event received: %s", event) } @@ -61,12 +69,6 @@ func TestFsnotifyEvents(t *testing.T) { } f.Sync() - // Add a watch for testFile - err = watcher.Watch(testFile) - if err != nil { - t.Fatalf("Watcher.Watch() failed: %s", err) - } - f.WriteString("data") f.Sync() f.Close() @@ -79,84 +81,13 @@ func TestFsnotifyEvents(t *testing.T) { t.Fatal("fsnotify event hasn't been received after 500 ms") } - t.Logf("Received %d events.", eventsReceived) - - // Try closing the fsnotify instance - t.Log("calling Close()") - watcher.Close() - t.Log("waiting for the event channel to become closed...") - select { - case <-done: - t.Log("event channel closed") - case <-time.After(1e9): - t.Fatal("event stream was not closed after 1 second") - } -} - -func TestFsnotifyDirOnly(t *testing.T) { - // Create an fsnotify watcher instance and initialize it - watcher, err := NewWatcher() - if err != nil { - t.Fatalf("NewWatcher() failed: %s", err) - } - - const testDir string = "_test" - - // Add a watch for testDir - err = watcher.Watch(testDir) - if err != nil { - t.Fatalf("Watcher.Watch() failed: %s", err) - } - - // Receive errors on the error channel on a separate goroutine - go func() { - for err := range watcher.Error { - t.Fatalf("error received: %s", err) - } - }() - - const testFile string = "_test/TestFsnotifyEvents.testfile" - - // Receive events on the event channel on a separate goroutine - eventstream := watcher.Event - var eventsReceived = 0 - done := make(chan bool) - go func() { - for event := range eventstream { - // Only count relevant events - if event.Name == testDir || event.Name == testFile { - eventsReceived++ - t.Logf("event received: %s", event) - } else { - t.Logf("unexpected event received: %s", event) - } - } - done <- true - }() - - // Create a file - // This should add at least one event to the fsnotify event queue - var f *os.File - f, err = os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666) - if err != nil { - t.Fatalf("creating test file failed: %s", err) + if createReceived == 0 { + t.Fatal("fsnotify create events have not been received after 500 ms") } - f.Sync() - - f.WriteString("data") - f.Sync() - f.Close() - - os.Remove(testFile) - - // We expect this event to be received almost immediately, but let's wait 500 ms to be sure - time.Sleep(500e6) // 500 ms - if eventsReceived == 0 { - t.Fatal("fsnotify event hasn't been received after 500 ms") + if deleteReceived == 0 { + t.Fatal("fsnotify delete events have not been received after 500 ms") } - t.Logf("Received %d events.", eventsReceived) - // Try closing the fsnotify instance t.Log("calling Close()") watcher.Close() @@ -197,12 +128,16 @@ func TestFsnotifyRename(t *testing.T) { // Receive events on the event channel on a separate goroutine eventstream := watcher.Event var eventsReceived = 0 + var renameReceived = 0 done := make(chan bool) go func() { for event := range eventstream { // Only count relevant events if event.Name == testDir || event.Name == testFile || event.Name == testFileRenamed { eventsReceived++ + if event.IsRename() { + renameReceived++ + } t.Logf("event received: %s", event) } else { t.Logf("unexpected event received: %s", event) @@ -244,7 +179,9 @@ func TestFsnotifyRename(t *testing.T) { t.Fatal("fsnotify event hasn't been received after 500 ms") } - t.Logf("Received %d events.", eventsReceived) + if renameReceived == 0 { + t.Fatal("fsnotify rename events have not been received after 500 ms") + } // Try closing the fsnotify instance t.Log("calling Close()") @@ -285,12 +222,16 @@ func TestFsnotifyAttrib(t *testing.T) { // Receive events on the event channel on a separate goroutine eventstream := watcher.Event var eventsReceived = 0 + var attribReceived = 0 done := make(chan bool) go func() { for event := range eventstream { // Only count relevant events if event.Name == testDir || event.Name == testFile { eventsReceived++ + if event.IsAttribute() { + attribReceived++ + } t.Logf("event received: %s", event) } else { t.Logf("unexpected event received: %s", event) @@ -332,7 +273,9 @@ func TestFsnotifyAttrib(t *testing.T) { t.Fatal("fsnotify event hasn't been received after 500 ms") } - t.Logf("Received %d events.", eventsReceived) + if attribReceived == 0 { + t.Fatal("fsnotify attribute events have not received after 500 ms") + } // Try closing the fsnotify instance t.Log("calling Close()")