xref: /llvm-project/libc/test/src/sys/epoll/linux/epoll_wait_test.cpp (revision 5fb821560afd0543e4875c3c81d58f68f73eb03b)
1 //===-- Unittests for epoll_wait ------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "hdr/sys_epoll_macros.h"
9 #include "hdr/types/struct_epoll_event.h"
10 #include "src/errno/libc_errno.h"
11 #include "src/sys/epoll/epoll_create1.h"
12 #include "src/sys/epoll/epoll_ctl.h"
13 #include "src/sys/epoll/epoll_wait.h"
14 #include "src/unistd/close.h"
15 #include "src/unistd/pipe.h"
16 #include "test/UnitTest/ErrnoSetterMatcher.h"
17 #include "test/UnitTest/Test.h"
18 
19 using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
20 
TEST(LlvmLibcEpollWaitTest,Basic)21 TEST(LlvmLibcEpollWaitTest, Basic) {
22   int epfd = LIBC_NAMESPACE::epoll_create1(0);
23   ASSERT_GT(epfd, 0);
24   ASSERT_ERRNO_SUCCESS();
25 
26   int pipefd[2];
27 
28   ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds());
29 
30   epoll_event event;
31   event.events = EPOLLOUT;
32   event.data.fd = pipefd[0];
33 
34   ASSERT_THAT(LIBC_NAMESPACE::epoll_ctl(epfd, EPOLL_CTL_ADD, pipefd[0], &event),
35               Succeeds());
36 
37   // Timeout of 0 causes immediate return. We just need to check that the
38   // interface works, we're not testing the kernel behavior here.
39   ASSERT_THAT(LIBC_NAMESPACE::epoll_wait(epfd, &event, 1, 0), Succeeds());
40 
41   ASSERT_THAT(LIBC_NAMESPACE::epoll_wait(-1, &event, 1, 0), Fails(EBADF));
42 
43   ASSERT_THAT(LIBC_NAMESPACE::epoll_ctl(epfd, EPOLL_CTL_DEL, pipefd[0], &event),
44               Succeeds());
45 
46   ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[0]), Succeeds());
47   ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds());
48   ASSERT_THAT(LIBC_NAMESPACE::close(epfd), Succeeds());
49 }
50