-
-
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.
WATCHER: Make queue functional with poller
- Loading branch information
Showing
4 changed files
with
117 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,25 @@ | ||
package watcher | ||
|
||
import "time" | ||
import ( | ||
"log" | ||
"time" | ||
) | ||
|
||
func Poll(interval int) { | ||
ticker := time.NewTicker(interval * time.Second) | ||
// Poll polls the queue for valid events given an interval (in seconds) | ||
func (pw *PathWatcher) Poll(interval int) { | ||
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
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 |
---|---|---|
@@ -1 +1,48 @@ | ||
package watcher | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/cian911/switchboard/event" | ||
) | ||
|
||
func TestQueue(t *testing.T) { | ||
t.Run("It adds one event to the queue", func(t *testing.T) { | ||
q := setupQueue() | ||
ev := testEvent() | ||
|
||
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() | ||
|
||
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()) | ||
} | ||
}) | ||
} | ||
|
||
func setupQueue() *Q { | ||
return NewQueue() | ||
} | ||
|
||
func testEvent() *event.Event { | ||
return &event.Event{ | ||
File: "sample.txt", | ||
Path: "/var/sample.txt", | ||
Ext: ".txt", | ||
Timestamp: time.Now(), | ||
} | ||
} |
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