Skip to content

Commit

Permalink
WIP: Add test cases for Observe method for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Cian911 committed Apr 3, 2022
1 parent dd75000 commit a1810b6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 15 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.PHONY: build build-arm build-debian run
.PHONY: test-all test-watcher test-event test-utils test-cmd lint-all vet-all
.PHONY: test-all test-watcher test-watcher-observe test-event test-utils test-cmd lint-all vet-all

VERSION := test-build
BUILD := $$(git log -1 --pretty=%h)
Expand All @@ -25,6 +25,9 @@ run:
test-watcher:
@gotest -v ./watcher

test-watcher-observe:
@gotest -v ./watcher -test.run TestObserve

test-event:
@gotest -v ./event

Expand Down
5 changes: 3 additions & 2 deletions watcher/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestEventSetup(t *testing.T) *event.Event {
}

// SimulateMultipleEvents takes a list of operations as args
// ["CREATE", "WRITE", "IN_CLOSE_WRITE"]
// ["CREATE", "WRITE", "CLOSEWRITE"]
func TestSimulateMultipleEvents(operationList []string, t *testing.T) []event.Event {
eventList := []event.Event{}

Expand All @@ -51,7 +51,8 @@ func TestSimulateMultipleEvents(operationList []string, t *testing.T) []event.Ev

func TestProducerConsumer() (Producer, Consumer) {
var pw Producer = &PathWatcher{
Path: HelperPath,
Path: HelperPath,
Queue: NewQueue(),
}

pattern, _ := utils.ValidateRegexPattern(HelperPattern)
Expand Down
26 changes: 14 additions & 12 deletions watcher/watcher_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (pw *PathWatcher) Unregister(consumer *Consumer) {

// Observe the producer
func (pw *PathWatcher) Observe(pollInterval int) {
eventQueue := NewQueue()
pw.Poll(1)

watcher, err := fsnotify.NewWatcher()
if err != nil {
Expand Down Expand Up @@ -190,7 +190,7 @@ func (pw *PathWatcher) Observe(pollInterval int) {
// Check for CREATE event
// -> If no event follows this for x seconds & is not in special file list
// -> Process event
// CHeck for IN_CLOSE_WRITE event
// Check for IN_CLOSE_WRITE event
// -> If no event follows this for x seconds & is not in special file list
// -> Process event

Expand All @@ -201,22 +201,24 @@ func (pw *PathWatcher) Observe(pollInterval int) {

if specialWatchedFileExts[ev.Ext] == 1 {
log.Println("Adding event to queue.")
eventQueue.Add(*ev)
pw.Queue.Add(*ev)
} else {
log.Printf("Notifying consumers: %v\n", ev)
pw.Notify(ev.Path, ev.Operation)
}
} else if event.Op.String() == "CREATE" {
log.Println("HERE")
createEvent := newEvent(event.Name, event.Op.String())
log.Printf("CREATE EVENT: %v\n", createEvent)

for hsh, ev := range eventQueue.Queue {
log.Printf("COMPARISON: %v\n\n", utils.CompareFilePaths(ev.Path, createEvent.Path))
if utils.CompareFilePaths(ev.Path, createEvent.Path) {
pw.Notify(createEvent.Path, createEvent.Operation)
eventQueue.Remove(hsh)
}
}
// Add the event to the queue and let the poller handle it
pw.Queue.Add(*createEvent)

/* for hsh, ev := range eventQueue.Queue { */
/* log.Printf("COMPARISON: %v\n\n", utils.CompareFilePaths(ev.Path, createEvent.Path)) */
/* if utils.CompareFilePaths(ev.Path, createEvent.Path) { */
/* pw.Notify(createEvent.Path, createEvent.Operation) */
/* eventQueue.Remove(hsh) */
/* } */
/* } */
}
case err := <-watcher.Errors:
log.Printf("Watcher encountered an error when observing %s: %v", pw.Path, err)
Expand Down
53 changes: 53 additions & 0 deletions watcher/watcher_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,62 @@
package watcher

import (
"os"
"testing"
"time"

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

func TestObserve(t *testing.T) {
t.Run("CLOSEWRITE", func(t *testing.T) {
HelperPath, _ = os.MkdirTemp("/home/cian/", "test1")
HelperDestination, _ = os.MkdirTemp("/home/cian/", "test2")
HelperExt = ".txt"

pw, pc := TestProducerConsumer()
pw.Register(&pc)

go pw.Observe(1)
<-time.After(1 * time.Second)
// Fire event
h, err := os.Create(HelperPath + "/sample2.txt")
if err != nil {
t.Fatalf("Failed to create file in testdir: %v", err)
}
h.Close()
os.OpenFile(HelperPath+"/sample.txt", 0, os.FileMode(int(0777)))
<-time.After(3 * time.Second)

files, _ := utils.ScanFilesInDir(HelperDestination)

if len(files) != 1 {
t.Errorf("CLOSEWRITE event was not processed - want: %d, got: %d", 1, len(files))
}
})

t.Run("CREATE", func(t *testing.T) {
HelperPath, _ = os.MkdirTemp("/home/cian/", "test1")
HelperDestination, _ = os.MkdirTemp("/home/cian/", "test2")
HelperExt = ".txt"

pw, pc := TestProducerConsumer()
pw.Register(&pc)

go pw.Observe(1)
<-time.After(3 * time.Second)
// Fire event
os.Create(HelperPath + "/sample2.txt")
<-time.After(3 * time.Second)

files, _ := utils.ScanFilesInDir(HelperDestination)

if len(files) != 1 {
t.Errorf("CREATE event was not processed - want: %d, got: %d", 1, len(files))
}
})

t.Run("WRITE", func(t *testing.T) {

})
}

0 comments on commit a1810b6

Please sign in to comment.