github.com/ruphin/docker@v1.10.1/pkg/filenotify/poller_test.go (about) 1 package filenotify 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "testing" 8 "time" 9 10 "gopkg.in/fsnotify.v1" 11 ) 12 13 func TestPollerAddRemove(t *testing.T) { 14 w := NewPollingWatcher() 15 16 if err := w.Add("no-such-file"); err == nil { 17 t.Fatal("should have gotten error when adding a non-existent file") 18 } 19 if err := w.Remove("no-such-file"); err == nil { 20 t.Fatal("should have gotten error when removing non-existent watch") 21 } 22 23 f, err := ioutil.TempFile("", "asdf") 24 if err != nil { 25 t.Fatal(err) 26 } 27 defer os.RemoveAll(f.Name()) 28 29 if err := w.Add(f.Name()); err != nil { 30 t.Fatal(err) 31 } 32 33 if err := w.Remove(f.Name()); err != nil { 34 t.Fatal(err) 35 } 36 } 37 38 func TestPollerEvent(t *testing.T) { 39 w := NewPollingWatcher() 40 41 f, err := ioutil.TempFile("", "test-poller") 42 if err != nil { 43 t.Fatal("error creating temp file") 44 } 45 defer os.RemoveAll(f.Name()) 46 f.Close() 47 48 if err := w.Add(f.Name()); err != nil { 49 t.Fatal(err) 50 } 51 52 select { 53 case <-w.Events(): 54 t.Fatal("got event before anything happened") 55 case <-w.Errors(): 56 t.Fatal("got error before anything happened") 57 default: 58 } 59 60 if err := ioutil.WriteFile(f.Name(), []byte("hello"), 644); err != nil { 61 t.Fatal(err) 62 } 63 if err := assertEvent(w, fsnotify.Write); err != nil { 64 t.Fatal(err) 65 } 66 67 if err := os.Chmod(f.Name(), 600); err != nil { 68 t.Fatal(err) 69 } 70 if err := assertEvent(w, fsnotify.Chmod); err != nil { 71 t.Fatal(err) 72 } 73 74 if err := os.Remove(f.Name()); err != nil { 75 t.Fatal(err) 76 } 77 if err := assertEvent(w, fsnotify.Remove); err != nil { 78 t.Fatal(err) 79 } 80 } 81 82 func TestPollerClose(t *testing.T) { 83 w := NewPollingWatcher() 84 if err := w.Close(); err != nil { 85 t.Fatal(err) 86 } 87 // test double-close 88 if err := w.Close(); err != nil { 89 t.Fatal(err) 90 } 91 92 select { 93 case _, open := <-w.Events(): 94 if open { 95 t.Fatal("event chan should be closed") 96 } 97 default: 98 t.Fatal("event chan should be closed") 99 } 100 101 select { 102 case _, open := <-w.Errors(): 103 if open { 104 t.Fatal("errors chan should be closed") 105 } 106 default: 107 t.Fatal("errors chan should be closed") 108 } 109 110 f, err := ioutil.TempFile("", "asdf") 111 if err != nil { 112 t.Fatal(err) 113 } 114 defer os.RemoveAll(f.Name()) 115 if err := w.Add(f.Name()); err == nil { 116 t.Fatal("should have gotten error adding watch for closed watcher") 117 } 118 } 119 120 func assertEvent(w FileWatcher, eType fsnotify.Op) error { 121 var err error 122 select { 123 case e := <-w.Events(): 124 if e.Op != eType { 125 err = fmt.Errorf("got wrong event type, expected %q: %v", eType, e) 126 } 127 case e := <-w.Errors(): 128 err = fmt.Errorf("got unexpected error waiting for events %v: %v", eType, e) 129 case <-time.After(watchWaitTime * 3): 130 err = fmt.Errorf("timeout waiting for event %v", eType) 131 } 132 return err 133 }