86 lines
1.5 KiB
Go
86 lines
1.5 KiB
Go
|
package inotify
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
|
"golang.org/x/sys/unix"
|
||
|
)
|
||
|
|
||
|
func TestPollerWithBadFd(t *testing.T) {
|
||
|
_, err := newPoller(-1)
|
||
|
if err != unix.EBADF {
|
||
|
t.Fatalf("Expected EBADF, got: %v", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPollerWithData(t *testing.T) {
|
||
|
var tfd [2]int
|
||
|
err := unix.Pipe(tfd[:])
|
||
|
if err != nil {
|
||
|
t.Fatalf("failed to create pipe: %v", err)
|
||
|
}
|
||
|
defer func() {
|
||
|
unix.Close(tfd[0])
|
||
|
unix.Close(tfd[1])
|
||
|
}()
|
||
|
poller, err := newPoller(tfd[0])
|
||
|
if err != nil {
|
||
|
t.Fatalf("Failed to create poller: %v", err)
|
||
|
}
|
||
|
oks := make(chan bool)
|
||
|
go func() {
|
||
|
ok, err := poller.wait()
|
||
|
if err != nil {
|
||
|
t.Fatalf("poller failed: %v", err)
|
||
|
}
|
||
|
oks <- ok
|
||
|
}()
|
||
|
|
||
|
select {
|
||
|
case <-time.After(1000 * time.Millisecond):
|
||
|
case <-oks:
|
||
|
t.Fatalf("poller did not wait")
|
||
|
}
|
||
|
|
||
|
msg := "poller"
|
||
|
buf := []byte(msg)
|
||
|
_, err = unix.Write(tfd[1], buf)
|
||
|
if err != nil {
|
||
|
t.Fatalf("Failed to write to pipe: %v", err)
|
||
|
}
|
||
|
|
||
|
ok := <-oks
|
||
|
if !ok {
|
||
|
t.Fatalf("expected true")
|
||
|
}
|
||
|
buf2 := make([]byte, 2048)
|
||
|
n, err := unix.Read(tfd[0], buf2)
|
||
|
if string(buf2[:n]) != msg {
|
||
|
t.Fatalf("read data error")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPollerWithClose(t *testing.T) {
|
||
|
var tfd [2]int
|
||
|
err := unix.Pipe(tfd[:])
|
||
|
if err != nil {
|
||
|
t.Fatalf("failed to create pipe: %v", err)
|
||
|
}
|
||
|
defer func() {
|
||
|
unix.Close(tfd[0])
|
||
|
}()
|
||
|
poller, err := newPoller(tfd[0])
|
||
|
if err != nil {
|
||
|
t.Fatalf("Failed to create poller: %v", err)
|
||
|
}
|
||
|
unix.Close(tfd[1])
|
||
|
ok, err := poller.wait()
|
||
|
if err == nil {
|
||
|
t.Fatalf("except poller")
|
||
|
}
|
||
|
if ok {
|
||
|
t.Fatalf("expected poller to return true")
|
||
|
}
|
||
|
}
|