Skip to content

Commit

Permalink
WATCHER: Add more poller tests. Add additional logging in watcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
Cian911 committed Jan 12, 2022
1 parent b370025 commit 215e551
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
31 changes: 31 additions & 0 deletions watcher/poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package watcher
import (
"testing"
"time"

"github.com/cian911/switchboard/event"
)

var (
Expand All @@ -26,10 +28,39 @@ 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 {
return &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(),
}
}
3 changes: 2 additions & 1 deletion watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 215e551

Please sign in to comment.