From c1f44814a5cd81a6d1cb589ef1e528bc5d305e07 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 5 Nov 2019 11:44:00 +0100 Subject: [PATCH] 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 Reviewed-by: Ian Lance Taylor --- unix/syscall_linux_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/unix/syscall_linux_test.go b/unix/syscall_linux_test.go index 791ba85b..d3161d68 100644 --- a/unix/syscall_linux_test.go +++ b/unix/syscall_linux_test.go @@ -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) + } +}