add tests and remove unused method in inotify

This commit is contained in:
Dominic Breuker
2018-03-14 08:49:54 +01:00
parent fb7dff2d13
commit 1f52ae340c
2 changed files with 63 additions and 12 deletions

View File

@@ -130,18 +130,6 @@ func (i *Inotify) NumWatchers() int {
return len(i.Watchers)
}
func (i *Inotify) String() string {
if len(i.Watchers) < 20 {
dirs := make([]string, 0)
for _, w := range i.Watchers {
dirs = append(dirs, w.Dir)
}
return fmt.Sprintf("Watching: %v", dirs)
} else {
return fmt.Sprintf("Watching %d directories", len(i.Watchers))
}
}
func getMaxWatchers() (int, error) {
b, err := ioutil.ReadFile(maximumWatchersFile)
if err != nil {

View File

@@ -0,0 +1,63 @@
package psscanner
import (
"testing"
"time"
)
const timeout = 100 * time.Millisecond
// refresh
func TestRun(t *testing.T) {
tests := []struct {
pids []int
events []string
}{
{pids: []int{1, 2, 3}, events: []string{
"UID=??? PID=3 | the-command",
"UID=??? PID=2 | the-command",
"UID=??? PID=1 | the-command",
}},
}
for _, tt := range tests {
restoreGetPIDs := mockPidList(tt.pids)
restoreCmdLineReader := mockCmdLineReader([]byte("the-command"), nil)
restoreProcStatusReader := mockProcStatusReader([]byte(""), nil) // don't mock read value since it's not worth it
pss := NewPSScanner()
triggerCh := make(chan struct{})
eventCh, errCh := pss.Run(triggerCh)
// does nothing without triggering
select {
case e := <-eventCh:
t.Errorf("Received event before trigger: %s", e)
case err := <-errCh:
t.Errorf("Received error before trigger: %v", err)
case <-time.After(timeout):
// ok
}
triggerCh <- struct{}{}
// received event after the trigger
for i := 0; i < 3; i++ {
select {
case <-time.After(timeout):
t.Errorf("did not receive event in time")
case e := <-eventCh:
if e != tt.events[i] {
t.Errorf("Wrong event received: got '%s' but wanted '%s'", e, tt.events[i])
}
case err := <-errCh:
t.Errorf("Received unexpected error: %v", err)
}
}
restoreProcStatusReader()
restoreCmdLineReader()
restoreGetPIDs()
}
}