-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Cian911/swb/add-queue-polling
Add polling
- Loading branch information
Showing
9 changed files
with
255 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package watcher | ||
|
||
import ( | ||
"log" | ||
"time" | ||
) | ||
|
||
// Poll polls the queue for valid events given an interval (in seconds) | ||
func (pw *PathWatcher) Poll(interval int) { | ||
go func() { | ||
ticker := time.NewTicker(time.Duration(interval) * time.Second) | ||
for { | ||
select { | ||
case <-ticker.C: | ||
log.Printf("Polling... - Queue Size: %d\n", pw.Queue.Size()) | ||
|
||
for hsh, ev := range pw.Queue.Queue { | ||
timeDiff := ev.Timestamp.Sub(time.Now()) | ||
if timeDiff < (time.Duration(-interval) * time.Second) { | ||
pw.Notify(ev.Path, ev.Operation) | ||
pw.Queue.Remove(hsh) | ||
} | ||
} | ||
} | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package watcher | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
var ( | ||
pollInterval = 1 | ||
) | ||
|
||
func TestPoller(t *testing.T) { | ||
t.Run("It successfully notifies of a new event", func(t *testing.T) { | ||
pw := setupPathwatcher("/tmp") | ||
pw.Poll(pollInterval) | ||
|
||
ev := eventSetup(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(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package watcher | ||
|
||
import ( | ||
"crypto/md5" | ||
"fmt" | ||
|
||
"github.com/cian911/switchboard/event" | ||
) | ||
|
||
// Q holds the Queue | ||
type Q struct { | ||
Queue map[string]event.Event | ||
} | ||
|
||
// NewQueue create a new Q object | ||
func NewQueue() *Q { | ||
return &Q{ | ||
Queue: make(map[string]event.Event), | ||
} | ||
} | ||
|
||
// Add adds to the queue | ||
func (q *Q) Add(ev event.Event) { | ||
q.Queue[Hash(ev)] = ev | ||
} | ||
|
||
// Retrieve get an item from the queue given a valid hash | ||
func (q *Q) Retrieve(hash string) event.Event { | ||
return q.Queue[hash] | ||
} | ||
|
||
// Remove removes an item from the queue | ||
func (q *Q) Remove(hash string) { | ||
delete(q.Queue, hash) | ||
} | ||
|
||
// Size returns the size of the queue | ||
func (q *Q) Size() int { | ||
return len(q.Queue) | ||
} | ||
|
||
// Empty returns a bool indicating if the queue is empty or not | ||
func (q *Q) Empty() bool { | ||
return len(q.Queue) == 0 | ||
} | ||
|
||
// Hash returns a md5 hash composed of an event File, Path, and Ext | ||
func Hash(ev event.Event) string { | ||
data := []byte(fmt.Sprintf("%s%s%s", ev.File, ev.Path, ev.Ext)) | ||
return fmt.Sprintf("%x", md5.Sum(data)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package watcher | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/cian911/switchboard/event" | ||
) | ||
|
||
var ( | ||
gFile = "sample.txt" | ||
gPath = "/var/sample.txt" | ||
gExt = ".txt" | ||
) | ||
|
||
func TestQueue(t *testing.T) { | ||
t.Run("It adds one event to the queue", func(t *testing.T) { | ||
q := setupQueue() | ||
ev := testEvent(gFile, gPath, gExt) | ||
|
||
q.Add(*ev) | ||
|
||
if q.Size() != 1 { | ||
t.Errorf("Could size did not increase as expected. want=%d, got=%d", 1, q.Size()) | ||
} | ||
}) | ||
|
||
t.Run("It updates the event in the queue", func(t *testing.T) { | ||
q := setupQueue() | ||
ev := testEvent(gFile, gPath, gExt) | ||
|
||
q.Add(*ev) | ||
q.Add(*ev) | ||
q.Add(*ev) | ||
|
||
if q.Size() != 1 { | ||
// Queue size should not increase | ||
t.Errorf("Could size did not increase as expected. want=%d, got=%d", 1, q.Size()) | ||
} | ||
}) | ||
|
||
t.Run("It gets an item from the queue", func(t *testing.T) { | ||
q := setupQueue() | ||
ev := testEvent(gFile, gPath, gExt) | ||
|
||
hash := Hash(*ev) | ||
q.Add(*ev) | ||
e := q.Retrieve(hash) | ||
|
||
if !reflect.DeepEqual(ev, &e) { | ||
t.Errorf("Events are not the same. want=%v, got=%v", ev, e) | ||
} | ||
}) | ||
|
||
t.Run("It removes an item from the queue", func(t *testing.T) { | ||
q := setupQueue() | ||
ev := testEvent(gFile, gPath, gExt) | ||
|
||
hash := Hash(*ev) | ||
q.Add(*ev) | ||
q.Remove(hash) | ||
|
||
if q.Size() != 0 { | ||
t.Errorf("Could size did not increase as expected. want=%d, got=%d", 0, q.Size()) | ||
} | ||
}) | ||
|
||
t.Run("It returns a unique hash for a given event", func(t *testing.T) { | ||
ev1 := testEvent(gFile, gPath, gExt) | ||
ev2 := testEvent("sample2.txt", "/var/sample2.txt", ".txt") | ||
|
||
h1 := Hash(*ev1) | ||
h2 := Hash(*ev2) | ||
|
||
if h1 == h2 { | ||
t.Errorf("Hashes are the same when they shouldn't be. want=%s, got=%s", h1, h2) | ||
} | ||
}) | ||
} | ||
|
||
func setupQueue() *Q { | ||
return NewQueue() | ||
} | ||
|
||
func testEvent(file, path, ext string) *event.Event { | ||
return &event.Event{ | ||
File: file, | ||
Path: path, | ||
Ext: ext, | ||
Timestamp: time.Now(), | ||
} | ||
|
||
} |
Oops, something went wrong.