unix: add TestEpoll on linux

This tests the epoll functionality on linux and should hopefully help
catch GOARCHes where additional padding of EpollEvent is needed (see
e.g.  CL 189877).

Change-Id: Icd19746a60a63016a5d46535a2cc557ca7a0d474
Reviewed-on: https://go-review.googlesource.com/c/sys/+/205398
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Tobias Klauser
2019-11-05 11:44:00 +01:00
committed by Tobias Klauser
parent ac3223d801
commit c1f44814a5

View File

@@ -659,3 +659,40 @@ func openMountByID(mountID int) (f *os.File, err error) {
}
return nil, errors.New("mountID not found")
}
func TestEpoll(t *testing.T) {
efd, err := unix.EpollCreate1(unix.EPOLL_CLOEXEC)
if err != nil {
t.Fatalf("EpollCreate1: %v", err)
}
defer unix.Close(efd)
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
defer r.Close()
defer w.Close()
fd := int(r.Fd())
ev := unix.EpollEvent{Events: unix.EPOLLIN, Fd: int32(fd)}
err = unix.EpollCtl(efd, unix.EPOLL_CTL_ADD, fd, &ev)
if err != nil {
t.Fatalf("EpollCtl: %v", err)
}
if _, err := w.Write([]byte("HELLO GOPHER")); err != nil {
t.Fatal(err)
}
events := make([]unix.EpollEvent, 128)
n, err := unix.EpollWait(efd, events, 1)
if err != nil {
t.Fatalf("EpollWait: %v", err)
}
if n != 1 {
t.Logf("EpollWait: wrong number of events: got %v, expected 1", n)
}
}