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