From: Adrien Bustany Date: Thu, 16 Jan 2014 16:51:14 +0000 (+0100) Subject: Add tests for the IsAttrib method X-Git-Tag: v1.7.2~327^2 X-Git-Url: https://go.fuhry.dev/?a=commitdiff_plain;h=8de07484f2bf54b1b8faa0e665d37a5ed31410c0;p=fsnotify.git Add tests for the IsAttrib method --- diff --git a/fsnotify_test.go b/fsnotify_test.go index fdb66b6..b22ab10 100644 --- a/fsnotify_test.go +++ b/fsnotify_test.go @@ -987,7 +987,11 @@ func TestFsnotifyAttrib(t *testing.T) { // Receive events on the event channel on a separate goroutine eventstream := watcher.Event + // The modifyReceived counter counts IsModify events that are not IsAttrib, + // and the attribReceived counts IsAttrib events (which are also IsModify as + // a consequence). var modifyReceived counter + var attribReceived counter done := make(chan bool) go func() { for event := range eventstream { @@ -996,6 +1000,9 @@ func TestFsnotifyAttrib(t *testing.T) { if event.IsModify() { modifyReceived.increment() } + if event.IsAttrib() { + attribReceived.increment() + } t.Logf("event received: %s", event) } else { t.Logf("unexpected event received: %s", event) @@ -1029,10 +1036,54 @@ func TestFsnotifyAttrib(t *testing.T) { } // We expect this event to be received almost immediately, but let's wait 500 ms to be sure + // Creating/writing a file changes also the mtime, so IsAttrib should be set to true here time.Sleep(500 * time.Millisecond) if modifyReceived.value() == 0 { t.Fatal("fsnotify modify events have not received after 500 ms") } + if attribReceived.value() == 0 { + t.Fatal("fsnotify attribute events have not received after 500 ms") + } + + // Modifying the contents of the file does not set the attrib flag (although eg. the mtime + // might have been modified). + modifyReceived.reset() + attribReceived.reset() + + f, err = os.OpenFile(testFile, os.O_WRONLY, 0) + if err != nil { + t.Fatalf("reopening test file failed: %s", err) + } + + f.WriteString("more data") + f.Sync() + f.Close() + + time.Sleep(500 * time.Millisecond) + + if modifyReceived.value() != 1 { + t.Fatal("didn't receive a modify event after changing test file contents") + } + + if attribReceived.value() != 0 { + t.Fatal("did receive an unexpected attrib event after changing test file contents") + } + + modifyReceived.reset() + attribReceived.reset() + + // Doing a chmod on the file should trigger an event with the "attrib" flag set (the contents + // of the file are not changed though) + err = os.Chmod(testFile, 0600) + if err != nil { + t.Fatalf("chmod failed: %s", err) + } + + time.Sleep(500 * time.Millisecond) + + if attribReceived.value() != 1 { + t.Fatal("didn't receive an attribute change after 500ms") + } // Try closing the fsnotify instance t.Log("calling Close()")