]> go.fuhry.dev Git - fsnotify.git/commitdiff
Add tests for the IsAttrib method
authorAdrien Bustany <adrien@bustany.org>
Thu, 16 Jan 2014 16:51:14 +0000 (17:51 +0100)
committerAdrien Bustany <adrien@bustany.org>
Thu, 16 Jan 2014 17:08:05 +0000 (18:08 +0100)
fsnotify_test.go

index fdb66b66246b5ad8cb019c66e500023ff87819b7..b22ab1027356d1e3d895b230a022a042e5102a3a 100644 (file)
@@ -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()")