diff --git a/watcher/poller_test.go b/watcher/poller_test.go index d47cf0c..9d953b5 100644 --- a/watcher/poller_test.go +++ b/watcher/poller_test.go @@ -3,6 +3,8 @@ package watcher import ( "testing" "time" + + "github.com/cian911/switchboard/event" ) var ( @@ -26,6 +28,23 @@ func TestPoller(t *testing.T) { t.Errorf("Queue size did not decrease. want=%d, got=%d", 0, pw.Queue.Size()) } }) + + t.Run("It successfully notifies on a new dir event", func(t *testing.T) { + pw := setupPathwatcher("/tmp") + pw.Poll(pollInterval) + + ev := setupNewDirEvent(t) + pw.Queue.Add(*ev) + + if pw.Queue.Size() != 1 { + t.Errorf("Queue size did not increase. want=%d, got=%d", 1, pw.Queue.Size()) + } + <-time.After(3 * time.Second) + + if pw.Queue.Size() != 0 { + t.Errorf("Queue size did not decrease. want=%d, got=%d", 0, pw.Queue.Size()) + } + }) } func setupPathwatcher(path string) *PathWatcher { @@ -33,3 +52,15 @@ func setupPathwatcher(path string) *PathWatcher { Queue: NewQueue(), } } + +func setupNewDirEvent(t *testing.T) *event.Event { + path := t.TempDir() + + return &event.Event{ + Path: path, + Destination: t.TempDir(), + Ext: ".txt", + Operation: "CREATE", + Timestamp: time.Now(), + } +} diff --git a/watcher/watcher.go b/watcher/watcher.go index 57c7d98..8e1e62c 100644 --- a/watcher/watcher.go +++ b/watcher/watcher.go @@ -70,7 +70,8 @@ func (pc *PathConsumer) Receive(path, ev string) { Operation: ev, } - if !e.IsNewDirEvent() && ev != pc.Ext && filepath.Dir(path) != pc.Path { + if !e.IsNewDirEvent() && e.Ext != pc.Ext && filepath.Dir(path) != pc.Path { + log.Printf("Not processing event - %v - %v", e, pc) // Do not process event for consumers not watching file return }