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.
13 func TestInotifyEvents(t *testing.T) {
14 // Create an inotify watcher instance and initialize it
15 watcher, err := NewWatcher()
17 t.Fatalf("NewWatcher() failed: %s", err)
20 // Add a watch for "_test"
21 err = watcher.Watch("_test")
23 t.Fatalf("Watcher.Watch() failed: %s", err)
26 // Receive errors on the error channel on a separate goroutine
28 for err := range watcher.Error {
29 t.Fatalf("error received: %s", err)
33 const testFile string = "_test/TestInotifyEvents.testfile"
35 // Receive events on the event channel on a separate goroutine
36 eventstream := watcher.Event
37 var eventsReceived = 0
38 done := make(chan bool)
40 for event := range eventstream {
41 // Only count relevant events
42 if event.Name == testFile {
44 t.Logf("event received: %s", event)
46 t.Logf("unexpected event received: %s", event)
53 // This should add at least one event to the inotify event queue
54 _, err = os.OpenFile(testFile, os.O_WRONLY|os.O_CREATE, 0666)
56 t.Fatalf("creating test file failed: %s", err)
59 // We expect this event to be received almost immediately, but let's wait 1 s to be sure
60 time.Sleep(1000e6) // 1000 ms
61 if eventsReceived == 0 {
62 t.Fatal("inotify event hasn't been received after 1 second")
65 // Try closing the inotify instance
66 t.Log("calling Close()")
68 t.Log("waiting for the event channel to become closed...")
71 t.Log("event channel closed")
72 case <-time.After(1e9):
73 t.Fatal("event stream was not closed after 1 second")
78 func TestInotifyClose(t *testing.T) {
79 watcher, _ := NewWatcher()
88 time.Sleep(50e6) // 50 ms
90 t.Fatal("double Close() test failed: second Close() call didn't return")
93 err := watcher.Watch("_test")
95 t.Fatal("expected error on Watch() after Close(), got nil")