| 1 |
package filenotify |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
"runtime" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
qt "github.com/frankban/quicktest" |
| 12 |
"github.com/fsnotify/fsnotify" |
| 13 |
"github.com/gohugoio/hugo/htesting" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
subdir1 = "subdir1" |
| 18 |
subdir2 = "subdir2" |
| 19 |
watchWaitTime = 200 * time.Millisecond |
| 20 |
) |
| 21 |
|
| 22 |
var ( |
| 23 |
isMacOs = runtime.GOOS == "darwin" |
| 24 |
isCI = htesting.IsCI() |
| 25 |
) |
| 26 |
|
| 27 |
func TestPollerAddRemove(t *testing.T) { |
| 28 |
c := qt.New(t) |
| 29 |
w := NewPollingWatcher(watchWaitTime) |
| 30 |
|
| 31 |
c.Assert(w.Add("foo"), qt.Not(qt.IsNil)) |
| 32 |
c.Assert(w.Remove("foo"), qt.Not(qt.IsNil)) |
| 33 |
|
| 34 |
f, err := os.CreateTemp("", "asdf") |
| 35 |
if err != nil { |
| 36 |
t.Fatal(err) |
| 37 |
} |
| 38 |
c.Cleanup(func() { |
| 39 |
c.Assert(w.Close(), qt.IsNil) |
| 40 |
os.Remove(f.Name()) |
| 41 |
}) |
| 42 |
c.Assert(w.Add(f.Name()), qt.IsNil) |
| 43 |
c.Assert(w.Remove(f.Name()), qt.IsNil) |
| 44 |
} |
| 45 |
|
| 46 |
func TestPollerEvent(t *testing.T) { |
| 47 |
t.Skip("flaky test") // TODO(bep) |
| 48 |
c := qt.New(t) |
| 49 |
|
| 50 |
for _, poll := range []bool{true, false} { |
| 51 |
if !(poll || isMacOs) || isCI { |
| 52 |
// Only run the fsnotify tests on MacOS locally. |
| 53 |
continue |
| 54 |
} |
| 55 |
method := "fsnotify" |
| 56 |
if poll { |
| 57 |
method = "poll" |
| 58 |
} |
| 59 |
|
| 60 |
c.Run(fmt.Sprintf("%s, Watch dir", method), func(c *qt.C) { |
| 61 |
dir, w := preparePollTest(c, poll) |
| 62 |
subdir := filepath.Join(dir, subdir1) |
| 63 |
c.Assert(w.Add(subdir), qt.IsNil) |
| 64 |
|
| 65 |
filename := filepath.Join(subdir, "file1") |
| 66 |
|
| 67 |
// Write to one file. |
| 68 |
c.Assert(os.WriteFile(filename, []byte("changed"), 0o600), qt.IsNil) |
| 69 |
|
| 70 |
var expected []fsnotify.Event |
| 71 |
|
| 72 |
if poll { |
| 73 |
expected = append(expected, fsnotify.Event{Name: filename, Op: fsnotify.Write}) |
| 74 |
assertEvents(c, w, expected...) |
| 75 |
} else { |
| 76 |
// fsnotify sometimes emits Chmod before Write, |
| 77 |
// which is hard to test, so skip it here. |
| 78 |
drainEvents(c, w) |
| 79 |
} |
| 80 |
|
| 81 |
// Remove one file. |
| 82 |
filename = filepath.Join(subdir, "file2") |
| 83 |
c.Assert(os.Remove(filename), qt.IsNil) |
| 84 |
assertEvents(c, w, fsnotify.Event{Name: filename, Op: fsnotify.Remove}) |
| 85 |
|
| 86 |
// Add one file. |
| 87 |
filename = filepath.Join(subdir, "file3") |
| 88 |
c.Assert(os.WriteFile(filename, []byte("new"), 0o600), qt.IsNil) |
| 89 |
assertEvents(c, w, fsnotify.Event{Name: filename, Op: fsnotify.Create}) |
| 90 |
|
| 91 |
// Remove entire directory. |
| 92 |
subdir = filepath.Join(dir, subdir2) |
| 93 |
c.Assert(w.Add(subdir), qt.IsNil) |
| 94 |
|
| 95 |
c.Assert(os.RemoveAll(subdir), qt.IsNil) |
| 96 |
|
| 97 |
expected = expected[:0] |
| 98 |
|
| 99 |
// This looks like a bug in fsnotify on MacOS. There are |
| 100 |
// 3 files in this directory, yet we get Remove events |
| 101 |
// for one of them + the directory. |
| 102 |
if !poll { |
| 103 |
expected = append(expected, fsnotify.Event{Name: filepath.Join(subdir, "file2"), Op: fsnotify.Remove}) |
| 104 |
} |
| 105 |
expected = append(expected, fsnotify.Event{Name: subdir, Op: fsnotify.Remove}) |
| 106 |
assertEvents(c, w, expected...) |
| 107 |
}) |
| 108 |
|
| 109 |
c.Run(fmt.Sprintf("%s, Add should not trigger event", method), func(c *qt.C) { |
| 110 |
dir, w := preparePollTest(c, poll) |
| 111 |
subdir := filepath.Join(dir, subdir1) |
| 112 |
w.Add(subdir) |
| 113 |
assertEvents(c, w) |
| 114 |
// Create a new sub directory and add it to the watcher. |
| 115 |
subdir = filepath.Join(dir, subdir1, subdir2) |
| 116 |
c.Assert(os.Mkdir(subdir, 0o777), qt.IsNil) |
| 117 |
w.Add(subdir) |
| 118 |
// This should create only one event. |
| 119 |
assertEvents(c, w, fsnotify.Event{Name: subdir, Op: fsnotify.Create}) |
| 120 |
}) |
| 121 |
|
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
func TestPollerClose(t *testing.T) { |
| 126 |
c := qt.New(t) |
| 127 |
w := NewPollingWatcher(watchWaitTime) |
| 128 |
f1, err := os.CreateTemp("", "f1") |
| 129 |
c.Assert(err, qt.IsNil) |
| 130 |
defer os.Remove(f1.Name()) |
| 131 |
f2, err := os.CreateTemp("", "f2") |
| 132 |
c.Assert(err, qt.IsNil) |
| 133 |
filename1 := f1.Name() |
| 134 |
filename2 := f2.Name() |
| 135 |
f1.Close() |
| 136 |
f2.Close() |
| 137 |
|
| 138 |
c.Assert(w.Add(filename1), qt.IsNil) |
| 139 |
c.Assert(w.Add(filename2), qt.IsNil) |
| 140 |
c.Assert(w.Close(), qt.IsNil) |
| 141 |
c.Assert(w.Close(), qt.IsNil) |
| 142 |
c.Assert(os.WriteFile(filename1, []byte("new"), 0o600), qt.IsNil) |
| 143 |
c.Assert(os.WriteFile(filename2, []byte("new"), 0o600), qt.IsNil) |
| 144 |
// No more event as the watchers are closed. |
| 145 |
assertEvents(c, w) |
| 146 |
|
| 147 |
f2, err = os.CreateTemp("", "f2") |
| 148 |
c.Assert(err, qt.IsNil) |
| 149 |
|
| 150 |
defer os.Remove(f2.Name()) |
| 151 |
|
| 152 |
c.Assert(w.Add(f2.Name()), qt.Not(qt.IsNil)) |
| 153 |
} |
| 154 |
|
| 155 |
func TestCheckChange(t *testing.T) { |
| 156 |
c := qt.New(t) |
| 157 |
|
| 158 |
dir := prepareTestDirWithSomeFiles(c, "check-change") |
| 159 |
|
| 160 |
stat := func(s ...string) os.FileInfo { |
| 161 |
fi, err := os.Stat(filepath.Join(append([]string{dir}, s...)...)) |
| 162 |
c.Assert(err, qt.IsNil) |
| 163 |
return fi |
| 164 |
} |
| 165 |
|
| 166 |
f0, f1, f2 := stat(subdir2, "file0"), stat(subdir2, "file1"), stat(subdir2, "file2") |
| 167 |
d1 := stat(subdir1) |
| 168 |
|
| 169 |
// Note that on Windows, only the 0200 bit (owner writable) of mode is used. |
| 170 |
c.Assert(os.Chmod(filepath.Join(filepath.Join(dir, subdir2, "file1")), 0o400), qt.IsNil) |
| 171 |
f1_2 := stat(subdir2, "file1") |
| 172 |
|
| 173 |
c.Assert(os.WriteFile(filepath.Join(filepath.Join(dir, subdir2, "file2")), []byte("changed"), 0o600), qt.IsNil) |
| 174 |
f2_2 := stat(subdir2, "file2") |
| 175 |
|
| 176 |
c.Assert(checkChange(f0, nil), qt.Equals, fsnotify.Remove) |
| 177 |
c.Assert(checkChange(nil, f0), qt.Equals, fsnotify.Create) |
| 178 |
c.Assert(checkChange(f1, f1_2), qt.Equals, fsnotify.Chmod) |
| 179 |
c.Assert(checkChange(f2, f2_2), qt.Equals, fsnotify.Write) |
| 180 |
c.Assert(checkChange(nil, nil), qt.Equals, fsnotify.Op(0)) |
| 181 |
c.Assert(checkChange(d1, f1), qt.Equals, fsnotify.Op(0)) |
| 182 |
c.Assert(checkChange(f1, d1), qt.Equals, fsnotify.Op(0)) |
| 183 |
} |
| 184 |
|
| 185 |
func BenchmarkPoller(b *testing.B) { |
| 186 |
runBench := func(b *testing.B, item *itemToWatch) { |
| 187 |
b.ResetTimer() |
| 188 |
for b.Loop() { |
| 189 |
evs, err := item.checkForChanges() |
| 190 |
if err != nil { |
| 191 |
b.Fatal(err) |
| 192 |
} |
| 193 |
if len(evs) != 0 { |
| 194 |
b.Fatal("got events") |
| 195 |
} |
| 196 |
|
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
b.Run("Check for changes in dir", func(b *testing.B) { |
| 201 |
c := qt.New(b) |
| 202 |
dir := prepareTestDirWithSomeFiles(c, "bench-check") |
| 203 |
item, err := newItemToWatch(dir) |
| 204 |
c.Assert(err, qt.IsNil) |
| 205 |
runBench(b, item) |
| 206 |
}) |
| 207 |
|
| 208 |
b.Run("Check for changes in file", func(b *testing.B) { |
| 209 |
c := qt.New(b) |
| 210 |
dir := prepareTestDirWithSomeFiles(c, "bench-check-file") |
| 211 |
filename := filepath.Join(dir, subdir1, "file1") |
| 212 |
item, err := newItemToWatch(filename) |
| 213 |
c.Assert(err, qt.IsNil) |
| 214 |
runBench(b, item) |
| 215 |
}) |
| 216 |
} |
| 217 |
|
| 218 |
func prepareTestDirWithSomeFiles(c *qt.C, id string) string { |
| 219 |
dir := c.TB.TempDir() |
| 220 |
c.Assert(os.MkdirAll(filepath.Join(dir, subdir1), 0o777), qt.IsNil) |
| 221 |
c.Assert(os.MkdirAll(filepath.Join(dir, subdir2), 0o777), qt.IsNil) |
| 222 |
|
| 223 |
for i := range 3 { |
| 224 |
c.Assert(os.WriteFile(filepath.Join(dir, subdir1, fmt.Sprintf("file%d", i)), []byte("hello1"), 0o600), qt.IsNil) |
| 225 |
} |
| 226 |
|
| 227 |
for i := range 3 { |
| 228 |
c.Assert(os.WriteFile(filepath.Join(dir, subdir2, fmt.Sprintf("file%d", i)), []byte("hello2"), 0o600), qt.IsNil) |
| 229 |
} |
| 230 |
|
| 231 |
c.Cleanup(func() { |
| 232 |
os.RemoveAll(dir) |
| 233 |
}) |
| 234 |
|
| 235 |
return dir |
| 236 |
} |
| 237 |
|
| 238 |
func preparePollTest(c *qt.C, poll bool) (string, FileWatcher) { |
| 239 |
var w FileWatcher |
| 240 |
if poll { |
| 241 |
w = NewPollingWatcher(watchWaitTime) |
| 242 |
} else { |
| 243 |
var err error |
| 244 |
w, err = NewEventWatcher() |
| 245 |
c.Assert(err, qt.IsNil) |
| 246 |
} |
| 247 |
|
| 248 |
dir := prepareTestDirWithSomeFiles(c, fmt.Sprint(poll)) |
| 249 |
|
| 250 |
c.Cleanup(func() { |
| 251 |
w.Close() |
| 252 |
}) |
| 253 |
return dir, w |
| 254 |
} |
| 255 |
|
| 256 |
func assertEvents(c *qt.C, w FileWatcher, evs ...fsnotify.Event) { |
| 257 |
c.Helper() |
| 258 |
i := 0 |
| 259 |
check := func() error { |
| 260 |
for { |
| 261 |
select { |
| 262 |
case got := <-w.Events(): |
| 263 |
if i > len(evs)-1 { |
| 264 |
return fmt.Errorf("got too many event(s): %q", got) |
| 265 |
} |
| 266 |
expected := evs[i] |
| 267 |
i++ |
| 268 |
if expected.Name != got.Name { |
| 269 |
return fmt.Errorf("got wrong filename, expected %q: %v", expected.Name, got.Name) |
| 270 |
} else if got.Op&expected.Op != expected.Op { |
| 271 |
return fmt.Errorf("got wrong event type, expected %q: %v", expected.Op, got.Op) |
| 272 |
} |
| 273 |
case e := <-w.Errors(): |
| 274 |
return fmt.Errorf("got unexpected error waiting for events %v", e) |
| 275 |
case <-time.After(watchWaitTime + (watchWaitTime / 2)): |
| 276 |
return nil |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
c.Assert(check(), qt.IsNil) |
| 281 |
c.Assert(i, qt.Equals, len(evs)) |
| 282 |
} |
| 283 |
|
| 284 |
func drainEvents(c *qt.C, w FileWatcher) { |
| 285 |
c.Helper() |
| 286 |
check := func() error { |
| 287 |
for { |
| 288 |
select { |
| 289 |
case <-w.Events(): |
| 290 |
case e := <-w.Errors(): |
| 291 |
return fmt.Errorf("got unexpected error waiting for events %v", e) |
| 292 |
case <-time.After(watchWaitTime * 2): |
| 293 |
return nil |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
c.Assert(check(), qt.IsNil) |
| 298 |
} |