mirror of
https://github.com/DominicBreuker/pspy.git
synced 2025-12-21 11:44:51 +00:00
add tests and remove unused method in inotify
This commit is contained in:
@@ -130,18 +130,6 @@ func (i *Inotify) NumWatchers() int {
|
|||||||
return len(i.Watchers)
|
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) {
|
func getMaxWatchers() (int, error) {
|
||||||
b, err := ioutil.ReadFile(maximumWatchersFile)
|
b, err := ioutil.ReadFile(maximumWatchersFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
63
internal/psscanner/psscanner_test.go
Normal file
63
internal/psscanner/psscanner_test.go
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user