1 //===-- Unittests for epoll_pwait -----------------------------------------===//
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_pwait.h"
14 #include "src/unistd/close.h"
15 #include "src/unistd/pipe.h"
16
17 #include "test/UnitTest/ErrnoSetterMatcher.h"
18 #include "test/UnitTest/Test.h"
19
20 using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
21
TEST(LlvmLibcEpollPwaitTest,Basic)22 TEST(LlvmLibcEpollPwaitTest, Basic) {
23 int epfd = LIBC_NAMESPACE::epoll_create1(0);
24 ASSERT_GT(epfd, 0);
25 ASSERT_ERRNO_SUCCESS();
26
27 int pipefd[2];
28
29 ASSERT_THAT(LIBC_NAMESPACE::pipe(pipefd), Succeeds());
30
31 epoll_event event;
32 event.events = EPOLLOUT;
33 event.data.fd = pipefd[0];
34
35 ASSERT_THAT(LIBC_NAMESPACE::epoll_ctl(epfd, EPOLL_CTL_ADD, pipefd[0], &event),
36 Succeeds());
37
38 // Timeout of 0 causes immediate return. We just need to check that the
39 // interface works, we're not testing the kernel behavior here.
40 ASSERT_THAT(LIBC_NAMESPACE::epoll_pwait(epfd, &event, 1, 0, nullptr),
41 Succeeds());
42
43 ASSERT_THAT(LIBC_NAMESPACE::epoll_pwait(-1, &event, 1, 0, nullptr),
44 Fails(EBADF));
45
46 ASSERT_THAT(LIBC_NAMESPACE::epoll_ctl(epfd, EPOLL_CTL_DEL, pipefd[0], &event),
47 Succeeds());
48
49 ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[0]), Succeeds());
50 ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds());
51 ASSERT_THAT(LIBC_NAMESPACE::close(epfd), Succeeds());
52 }
53