mirror of
https://github.com/DominicBreuker/pspy.git
synced 2025-12-21 03:34:50 +00:00
try some more tsting
This commit is contained in:
67
internal/fswatcher/inotify/inotify.go
Normal file
67
internal/fswatcher/inotify/inotify.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package inotify
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type InotifySyscalls interface {
|
||||
Init() (int, error)
|
||||
AddWatch(int, string) (int, error)
|
||||
Close(int) error
|
||||
}
|
||||
|
||||
type Inotify struct {
|
||||
FD int
|
||||
Watchers map[int]*Watcher
|
||||
sys InotifySyscalls
|
||||
}
|
||||
|
||||
func NewInotify(isys InotifySyscalls) (*Inotify, error) {
|
||||
fd, err := isys.Init()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("initializing inotify: %v", err)
|
||||
}
|
||||
|
||||
i := &Inotify{
|
||||
FD: fd,
|
||||
Watchers: make(map[int]*Watcher),
|
||||
sys: isys,
|
||||
}
|
||||
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func (i *Inotify) Watch(dir string) error {
|
||||
wd, err := i.sys.AddWatch(i.FD, dir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("adding watcher on %s: %v", dir, err)
|
||||
}
|
||||
i.Watchers[wd] = &Watcher{
|
||||
WD: wd,
|
||||
Dir: dir,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *Inotify) Close() error {
|
||||
if err := i.sys.Close(i.FD); err != nil {
|
||||
return fmt.Errorf("closing inotify file descriptor: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
49
internal/fswatcher/inotify/inotify_test.go
Normal file
49
internal/fswatcher/inotify/inotify_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package inotify
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewInotify(t *testing.T) {
|
||||
mis := &MockInotifySyscalls{fd: 1}
|
||||
|
||||
i, err := NewInotify(mis)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error")
|
||||
}
|
||||
if i.FD != mis.fd {
|
||||
t.Fatalf("Did not set FD of inotify object")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewInotifyError(t *testing.T) {
|
||||
mis := &MockInotifySyscalls{fd: -1}
|
||||
|
||||
_, err := NewInotify(mis)
|
||||
if err == nil || err.Error() != "initializing inotify: syscall error" {
|
||||
t.Fatalf("Expected syscall error but did not get: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// mock
|
||||
|
||||
type MockInotifySyscalls struct {
|
||||
fd int
|
||||
}
|
||||
|
||||
func (mis *MockInotifySyscalls) Init() (int, error) {
|
||||
if mis.fd >= 0 {
|
||||
return mis.fd, nil
|
||||
} else {
|
||||
return -1, errors.New("syscall error")
|
||||
}
|
||||
}
|
||||
|
||||
func (mis *MockInotifySyscalls) AddWatch(fd int, dir string) (int, error) {
|
||||
return 2, nil
|
||||
}
|
||||
|
||||
func (mis *MockInotifySyscalls) Close(fd int) error {
|
||||
return nil
|
||||
}
|
||||
36
internal/fswatcher/inotify/sys/syscalls.go
Normal file
36
internal/fswatcher/inotify/sys/syscalls.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// +build linux
|
||||
|
||||
package sys
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
const events = unix.IN_ALL_EVENTS
|
||||
|
||||
type InotifySyscallsUNIX struct{}
|
||||
|
||||
func (isu *InotifySyscallsUNIX) Init() (int, error) {
|
||||
fd, errno := unix.InotifyInit1(unix.IN_CLOEXEC)
|
||||
if fd < 0 {
|
||||
return fd, fmt.Errorf("errno: %d", errno)
|
||||
}
|
||||
return fd, nil
|
||||
}
|
||||
|
||||
func (isu *InotifySyscallsUNIX) AddWatch(fd int, dir string) (int, error) {
|
||||
wd, errno := unix.InotifyAddWatch(fd, dir, events)
|
||||
if wd < 0 {
|
||||
return wd, fmt.Errorf("errno: %d", errno)
|
||||
}
|
||||
return wd, nil
|
||||
}
|
||||
|
||||
func (isu *InotifySyscallsUNIX) Close(fd int) error {
|
||||
if err := unix.Close(fd); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
45
internal/fswatcher/inotify/sys/syscalls_test.go
Normal file
45
internal/fswatcher/inotify/sys/syscalls_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// +build linux
|
||||
|
||||
package sys
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSyscalls(t *testing.T) {
|
||||
is := &InotifySyscallsUNIX{}
|
||||
|
||||
fd, err := is.Init()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error for inotify init: %v", err)
|
||||
}
|
||||
|
||||
_, err = is.AddWatch(fd, "testdata")
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error adding watch to dir 'testdata': %v", err)
|
||||
}
|
||||
|
||||
err = is.Close(fd)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error closing inotify: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyscallsError(t *testing.T) {
|
||||
is := &InotifySyscallsUNIX{}
|
||||
|
||||
fd, err := is.Init()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error for inotify init: %v", err)
|
||||
}
|
||||
|
||||
_, err = is.AddWatch(fd, "non-existing-dir")
|
||||
if err == nil || err.Error() != "errno: 2" {
|
||||
t.Fatalf("Expected errno 2 for non-existing-dir but got: %v", err)
|
||||
}
|
||||
|
||||
err = is.Close(fd)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error closing inotify: %v", err)
|
||||
}
|
||||
}
|
||||
30
internal/fswatcher/inotify/watcher.go
Normal file
30
internal/fswatcher/inotify/watcher.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package inotify
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const maximumWatchersFile = "/proc/sys/fs/inotify/max_user_watches"
|
||||
|
||||
type Watcher struct {
|
||||
WD int
|
||||
Dir string
|
||||
}
|
||||
|
||||
func GetLimit() (int, error) {
|
||||
b, err := ioutil.ReadFile(maximumWatchersFile)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("reading from %s: %v", maximumWatchersFile, err)
|
||||
}
|
||||
|
||||
s := strings.TrimSpace(string(b))
|
||||
m, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("converting to integer: %v", err)
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
Reference in New Issue
Block a user