1 | //===-- Unittests for epoll_pwait2 ----------------------------------------===// |
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 "hdr/types/struct_timespec.h" |
11 | #include "src/errno/libc_errno.h" |
12 | #include "src/sys/epoll/epoll_create1.h" |
13 | #include "src/sys/epoll/epoll_ctl.h" |
14 | #include "src/sys/epoll/epoll_pwait2.h" |
15 | #include "src/unistd/close.h" |
16 | #include "src/unistd/pipe.h" |
17 | #include "test/UnitTest/ErrnoSetterMatcher.h" |
18 | #include "test/UnitTest/Test.h" |
19 | |
20 | using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher; |
21 | |
22 | TEST(LlvmLibcEpollPwaitTest, Basic) { |
23 | int epfd = LIBC_NAMESPACE::epoll_create1(flags: 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 | timespec time_spec; |
36 | time_spec.tv_sec = 0; |
37 | time_spec.tv_nsec = 0; |
38 | |
39 | ASSERT_THAT(LIBC_NAMESPACE::epoll_ctl(epfd, EPOLL_CTL_ADD, pipefd[0], &event), |
40 | Succeeds()); |
41 | |
42 | // Timeout of 0 causes immediate return. We just need to check that the |
43 | // interface works, we're not testing the kernel behavior here. |
44 | ASSERT_THAT( |
45 | LIBC_NAMESPACE::epoll_pwait2(epfd, &event, 1, &time_spec, nullptr), |
46 | Succeeds()); |
47 | |
48 | ASSERT_THAT(LIBC_NAMESPACE::epoll_pwait2(-1, &event, 1, &time_spec, nullptr), |
49 | Fails(EBADF)); |
50 | |
51 | ASSERT_THAT(LIBC_NAMESPACE::epoll_ctl(epfd, EPOLL_CTL_DEL, pipefd[0], &event), |
52 | Succeeds()); |
53 | |
54 | ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[0]), Succeeds()); |
55 | ASSERT_THAT(LIBC_NAMESPACE::close(pipefd[1]), Succeeds()); |
56 | ASSERT_THAT(LIBC_NAMESPACE::close(epfd), Succeeds()); |
57 | } |
58 | |