Skip to content

Commit 0094175

Browse files
committed
Test to confirm events are received only after AddWatch
- irrespective of whether listener has started or stopped
1 parent 57f8918 commit 0094175

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

fanotify_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,60 @@ func TestMultipleEvents(t *testing.T) {
410410
}
411411
}
412412

413+
func TestAddPathBeforeWatchStart(t *testing.T) {
414+
l, err := NewListener("/", false, PermissionNone)
415+
assert.Nil(t, err)
416+
assert.NotNil(t, l)
417+
go l.Start()
418+
defer l.Stop()
419+
420+
watchDir := t.TempDir()
421+
422+
testFile := fmt.Sprintf("%s/test.txt", watchDir)
423+
pid, err := runAsCmd("touch", testFile) // create file
424+
assert.Nil(t, err)
425+
select {
426+
case <-time.After(100 * time.Millisecond):
427+
t.Logf("FileCreated Event not received as expected")
428+
case event := <-l.Events:
429+
t.Errorf("Timeout Error: Unexpected FileCreated event received (%s)", event)
430+
}
431+
touchPid := pid
432+
433+
eventTypes := FileModified.Or(FileDeleted)
434+
l.AddWatch(watchDir, eventTypes)
435+
// modify file
436+
os.WriteFile(testFile, []byte("test string"), 0666)
437+
pid = os.Getpid()
438+
select {
439+
case <-time.After(100 * time.Millisecond):
440+
t.Error("Timeout Error: FileModified event not received")
441+
case event := <-l.Events:
442+
assert.Equal(t, fmt.Sprintf("%s/%s", event.Path, event.FileName), testFile)
443+
assert.Equal(t, event.Pid, pid)
444+
assert.True(t, event.EventTypes.Has(FileModified))
445+
t.Logf("Received: (%s)", event)
446+
}
447+
448+
t.Logf("Pids: Self(%d), Touch(%d)", pid, touchPid)
449+
// NOTE: os.WriteFile sends two modify events; so draining them
450+
for len(l.Events) > 0 {
451+
e := <-l.Events
452+
t.Logf("Drain-Event: (%s)", e)
453+
}
454+
pid, err = runAsCmd("rm", "-f", testFile)
455+
assert.Nil(t, err)
456+
select {
457+
case <-time.After(100 * time.Millisecond):
458+
t.Error("Timeout Error: FileDeleted event not received")
459+
case event := <-l.Events:
460+
assert.Equal(t, fmt.Sprintf("%s/%s", event.Path, event.FileName), testFile)
461+
assert.Equal(t, event.Pid, pid)
462+
assert.True(t, event.EventTypes.Has(FileDeleted))
463+
t.Logf("Received: (%s)", event)
464+
}
465+
}
466+
413467
// FileCreated and FileClosed combination does not raise any events
414468
func TestWithCapSysAdmMarkCreateCloseBug(t *testing.T) {
415469
if *bug {

0 commit comments

Comments
 (0)