/
integration_test.go
540 lines (472 loc) · 10.8 KB
/
integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !plan9 && !solaris
// +build !plan9,!solaris
package fsnotify
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync/atomic"
"testing"
"time"
)
// tempMkFile makes a temporary file.
func tempMkFile(t *testing.T, dir string) string {
f, err := os.CreateTemp(dir, "fsnotify")
if err != nil {
t.Fatalf("failed to create test file: %v", err)
}
defer f.Close()
return f.Name()
}
func TestWatch(t *testing.T) {
t.Parallel()
tests := []testCase{
{
"multiple creates",
func(t *testing.T, w *Watcher, tempDir string) {
file := filepath.Join(tempDir, "file")
addWatch(t, w, tempDir)
cat(t, "data", file)
rm(t, file)
touch(t, file) // Recreate the file
cat(t, "data", file) // Modify
cat(t, "data", file) // Modify
},
`
create /file
write /file
remove /file
create /file
write /file
write /file
`,
},
{
"dir only",
func(t *testing.T, w *Watcher, tempDir string) {
beforeWatch := filepath.Join(tempDir, "beforeWatch")
file := filepath.Join(tempDir, "file")
touch(t, beforeWatch)
addWatch(t, w, tempDir)
cat(t, "data", file)
rm(t, file)
rm(t, beforeWatch)
},
`
create /file
write /file
remove /file
remove /beforeWatch
`,
},
{
"subdir",
func(t *testing.T, w *Watcher, tempDir string) {
addWatch(t, w, tempDir)
file := filepath.Join(tempDir, "file")
dir := filepath.Join(tempDir, "sub")
dirfile := filepath.Join(tempDir, "sub", "file2")
mkdir(t, dir) // Create sub-directory
touch(t, file) // Create a file
touch(t, dirfile) // Create a file (Should not see this! we are not watching subdir)
time.Sleep(200 * time.Millisecond)
rmAll(t, dir) // Make sure receive deletes for both file and sub-directory
rm(t, file)
},
`
create /sub
create /file
remove /sub
remove /file
# Windows includes a write for the /sub dir too, two of them even(?)
windows:
create /sub
create /file
write /sub
write /sub
remove /sub
remove /file
`,
},
}
for _, tt := range tests {
tt.run(t)
}
}
func TestWatchRename(t *testing.T) {
t.Parallel()
tests := []testCase{
{
"rename file",
func(t *testing.T, w *Watcher, tmp string) {
file := filepath.Join(tmp, "file")
addWatch(t, w, tmp)
cat(t, "asd", file)
mv(t, file, tmp, "renamed")
},
`
create /file
write /file
rename /file
create /renamed
`,
},
{
"rename from unwatched directory",
func(t *testing.T, w *Watcher, tmp string) {
unwatched := t.TempDir()
addWatch(t, w, tmp)
touch(t, unwatched, "file")
mv(t, filepath.Join(unwatched, "file"), tmp, "file")
},
`
create /file
`,
},
{
"rename to unwatched directory",
func(t *testing.T, w *Watcher, tmp string) {
if runtime.GOOS == "netbsd" {
t.Skip("NetBSD behaviour is not fully correct") // TODO: investigate and fix.
}
unwatched := t.TempDir()
file := filepath.Join(tmp, "file")
renamed := filepath.Join(unwatched, "renamed")
addWatch(t, w, tmp)
cat(t, "data", file)
mv(t, file, renamed)
cat(t, "data", renamed) // Modify the file outside of the watched dir
touch(t, file) // Recreate the file that was moved
},
`
create /file
write /file
rename /file
create /file
# Windows has REMOVE /file, rather than CREATE /file
windows:
create /file
write /file
remove /file
create /file
`,
},
{
"rename overwriting existing file", func(t *testing.T, w *Watcher, tmp string) {
switch runtime.GOOS {
case "windows":
t.Skipf("os.Rename over existing file does not create an event on %q", runtime.GOOS)
}
touch(t, tmp, "renamed")
addWatch(t, w, tmp)
unwatched := t.TempDir()
file := filepath.Join(unwatched, "file")
touch(t, file)
mv(t, file, tmp, "renamed")
},
`
remove /renamed
create /renamed
# No remove event for Windows and Linux.
linux:
create /renamed
windows:
create /renamed
`,
},
}
for _, tt := range tests {
tt.run(t)
}
}
func TestWatchSymlink(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("symlinks don't work on Windows")
}
t.Parallel()
tests := []testCase{
{"create unresolvable symlink", func(t *testing.T, w *Watcher, tmp string) {
addWatch(t, w, tmp)
symlink(t, filepath.Join(tmp, "target"), tmp, "link")
}, `
create /link
`},
{"cyclic symlink", func(t *testing.T, w *Watcher, tmp string) {
if runtime.GOOS == "darwin" {
// This test is borked on macOS; it reports events outside the
// watched directory:
//
// create "/private/.../testwatchsymlinkcyclic_symlink3681444267/001/link"
// create "/link"
// write "/link"
// write "/private/.../testwatchsymlinkcyclic_symlink3681444267/001/link"
//
// kqueue.go does a lot of weird things with symlinks that I
// don't think are necessarily correct, but need to test a bit
// more.
t.Skip()
}
symlink(t, ".", tmp, "link")
addWatch(t, w, tmp)
rm(t, tmp, "link")
cat(t, "foo", tmp, "link")
}, `
write /link
create /link
linux:
remove /link
create /link
write /link
`},
}
for _, tt := range tests {
tt.run(t)
}
}
func TestWatchAttrib(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("attributes don't work on Windows")
}
t.Parallel()
tests := []testCase{
{
"chmod",
func(t *testing.T, w *Watcher, tmp string) {
file := filepath.Join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, file)
chmod(t, 0o700, file)
},
`
CHMOD "/file"
`,
},
{
"write does not trigger CHMOD",
func(t *testing.T, w *Watcher, tmp string) {
file := filepath.Join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, file)
chmod(t, 0o700, file)
cat(t, "more data", file)
},
`
CHMOD "/file"
WRITE "/file"
`,
},
{
"chmod after write",
func(t *testing.T, w *Watcher, tmp string) {
file := filepath.Join(tmp, "file")
cat(t, "data", file)
addWatch(t, w, file)
chmod(t, 0o700, file)
cat(t, "more data", file)
chmod(t, 0o600, file)
},
`
CHMOD "/file"
WRITE "/file"
CHMOD "/file"
`,
},
}
for _, tt := range tests {
tt.run(t)
}
}
func TestWatchRm(t *testing.T) {
tests := []testCase{
{
"remove watched directory",
func(t *testing.T, w *Watcher, tmp string) {
if runtime.GOOS == "openbsd" || runtime.GOOS == "netbsd" {
t.Skip("behavior is inconsistent on OpenBSD and NetBSD, and this test is flaky")
}
file := filepath.Join(tmp, "file")
touch(t, file)
addWatch(t, w, tmp)
rmAll(t, tmp)
},
`
# OpenBSD, NetBSD
remove /file
remove|write /
freebsd:
remove|write "/"
remove ""
create "."
darwin:
remove /file
remove|write /
linux:
remove /file
remove /
windows:
remove /file
remove /
`,
},
}
for _, tt := range tests {
tt.run(t)
}
}
func TestClose(t *testing.T) {
t.Parallel()
t.Run("close", func(t *testing.T) {
t.Parallel()
w := newWatcher(t)
if err := w.Close(); err != nil {
t.Fatal(err)
}
var done int32
go func() {
w.Close()
atomic.StoreInt32(&done, 1)
}()
eventSeparator()
if atomic.LoadInt32(&done) == 0 {
t.Fatal("double Close() test failed: second Close() call didn't return")
}
if err := w.Add(t.TempDir()); err == nil {
t.Fatal("expected error on Watch() after Close(), got nil")
}
})
// Make sure that Close() works even when the Events channel isn't being
// read.
t.Run("events not read", func(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
w := newWatcher(t, tmp)
touch(t, tmp, "file")
rm(t, tmp, "file")
if err := w.Close(); err != nil {
t.Fatal(err)
}
})
// Make sure that calling Close() while REMOVE events are emitted doesn't race.
t.Run("close while removing files", func(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
files := make([]string, 0, 200)
for i := 0; i < 200; i++ {
f := filepath.Join(tmp, fmt.Sprintf("file-%03d", i))
touch(t, f, noWait)
files = append(files, f)
}
w := newWatcher(t, tmp)
startC, stopC, errC := make(chan struct{}), make(chan struct{}), make(chan error)
go func() {
for {
select {
case <-w.Errors:
case <-w.Events:
case <-stopC:
return
}
}
}()
rmDone := make(chan struct{})
go func() {
<-startC
for _, f := range files {
rm(t, f, noWait)
}
rmDone <- struct{}{}
}()
go func() {
<-startC
errC <- w.Close()
}()
close(startC)
defer close(stopC)
if err := <-errC; err != nil {
t.Fatal(err)
}
<-rmDone
})
// Make sure Close() doesn't race when called more than once; hard to write
// a good reproducible test for this, but running it 150 times seems to
// reproduce it in ~75% of cases and isn't too slow (~0.06s on my system).
t.Run("double close", func(t *testing.T) {
t.Parallel()
for i := 0; i < 150; i++ {
w, err := NewWatcher()
if err != nil {
if strings.Contains(err.Error(), "too many") { // syscall.EMFILE
time.Sleep(100 * time.Millisecond)
continue
}
t.Fatal(err)
}
go w.Close()
go w.Close()
go w.Close()
}
})
}
func TestRemove(t *testing.T) {
t.Parallel()
t.Run("works", func(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
touch(t, tmp, "file")
w := newCollector(t)
w.collect(t)
addWatch(t, w.w, tmp)
if err := w.w.Remove(tmp); err != nil {
t.Fatal(err)
}
time.Sleep(200 * time.Millisecond)
cat(t, "data", tmp, "file")
chmod(t, 0o700, tmp, "file")
have := w.stop(t)
if len(have) > 0 {
t.Errorf("received events; expected none:\n%s", have)
}
})
// Make sure that concurrent calls to Remove() don't race.
t.Run("no race", func(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
touch(t, tmp, "file")
for i := 0; i < 10; i++ {
w := newWatcher(t)
defer w.Close()
addWatch(t, w, tmp)
done := make(chan struct{})
go func() {
defer func() { done <- struct{}{} }()
w.Remove(tmp)
}()
go func() {
defer func() { done <- struct{}{} }()
w.Remove(tmp)
}()
<-done
<-done
w.Close()
}
})
}
// Make sure Close() doesn't race; hard to write a good reproducible test for
// this, but running it 150 times seems to reproduce it in ~75% of cases and
// isn't too slow (~0.06s on my system).
func TestCloseRace(t *testing.T) {
for i := 0; i < 150; i++ {
w, err := NewWatcher()
if err != nil {
t.Fatal(err)
}
go w.Close()
go w.Close()
go w.Close()
}
}