summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/eventfd.cc
diff options
context:
space:
mode:
authorJamie Liu <jamieliu@google.com>2019-01-29 08:00:33 -0800
committerShentubot <shentubot@google.com>2019-01-29 08:01:48 -0800
commit57c202ead2d937e40c5d10da409504ac474165d1 (patch)
tree1e0d3365e744b554570563c0f4c661377f475187 /test/syscalls/linux/eventfd.cc
parent24cb2c0a7256cdb515c2fc2cfc90d130e2a405ef (diff)
Refactor out NewEventFD to a test utility.
PiperOrigin-RevId: 231404512 Change-Id: I31efcc23a0c4a48ef6fbba3ca07415d79290f55c
Diffstat (limited to 'test/syscalls/linux/eventfd.cc')
-rw-r--r--test/syscalls/linux/eventfd.cc92
1 files changed, 41 insertions, 51 deletions
diff --git a/test/syscalls/linux/eventfd.cc b/test/syscalls/linux/eventfd.cc
index ffcd20622..8111da30e 100644
--- a/test/syscalls/linux/eventfd.cc
+++ b/test/syscalls/linux/eventfd.cc
@@ -18,12 +18,13 @@
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
-#include <sys/eventfd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "gtest/gtest.h"
+#include "test/util/epoll_util.h"
+#include "test/util/eventfd_util.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"
@@ -33,21 +34,20 @@ namespace testing {
namespace {
TEST(EventfdTest, Nonblock) {
- int efd;
- ASSERT_THAT(efd = eventfd(0, EFD_NONBLOCK | EFD_SEMAPHORE),
- SyscallSucceeds());
+ FileDescriptor efd =
+ ASSERT_NO_ERRNO_AND_VALUE(NewEventFD(0, EFD_NONBLOCK | EFD_SEMAPHORE));
uint64_t l;
- ASSERT_THAT(read(efd, &l, sizeof(l)), SyscallFailsWithErrno(EAGAIN));
+ ASSERT_THAT(read(efd.get(), &l, sizeof(l)), SyscallFailsWithErrno(EAGAIN));
l = 1;
- ASSERT_THAT(write(efd, &l, sizeof(l)), SyscallSucceeds());
+ ASSERT_THAT(write(efd.get(), &l, sizeof(l)), SyscallSucceeds());
l = 0;
- ASSERT_THAT(read(efd, &l, sizeof(l)), SyscallSucceeds());
+ ASSERT_THAT(read(efd.get(), &l, sizeof(l)), SyscallSucceeds());
EXPECT_EQ(l, 1);
- ASSERT_THAT(read(efd, &l, sizeof(l)), SyscallFailsWithErrno(EAGAIN));
+ ASSERT_THAT(read(efd.get(), &l, sizeof(l)), SyscallFailsWithErrno(EAGAIN));
}
void* read_three_times(void* arg) {
@@ -60,8 +60,8 @@ void* read_three_times(void* arg) {
}
TEST(EventfdTest, BlockingWrite) {
- int efd;
- ASSERT_THAT(efd = eventfd(0, EFD_SEMAPHORE), SyscallSucceeds());
+ FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(NewEventFD(0, EFD_SEMAPHORE));
+ int efd = fd.get();
pthread_t p;
ASSERT_THAT(pthread_create(&p, nullptr, read_three_times,
@@ -82,58 +82,53 @@ TEST(EventfdTest, BlockingWrite) {
}
TEST(EventfdTest, SmallWrite) {
- int efd;
- ASSERT_THAT(efd = eventfd(0, EFD_NONBLOCK | EFD_SEMAPHORE),
- SyscallSucceeds());
+ FileDescriptor efd =
+ ASSERT_NO_ERRNO_AND_VALUE(NewEventFD(0, EFD_NONBLOCK | EFD_SEMAPHORE));
uint64_t l = 16;
- ASSERT_THAT(write(efd, &l, 4), SyscallFailsWithErrno(EINVAL));
+ ASSERT_THAT(write(efd.get(), &l, 4), SyscallFailsWithErrno(EINVAL));
}
TEST(EventfdTest, SmallRead) {
- int efd;
- ASSERT_THAT(efd = eventfd(0, EFD_NONBLOCK | EFD_SEMAPHORE),
- SyscallSucceeds());
+ FileDescriptor efd =
+ ASSERT_NO_ERRNO_AND_VALUE(NewEventFD(0, EFD_NONBLOCK | EFD_SEMAPHORE));
uint64_t l = 1;
- ASSERT_THAT(write(efd, &l, sizeof(l)), SyscallSucceeds());
+ ASSERT_THAT(write(efd.get(), &l, sizeof(l)), SyscallSucceeds());
l = 0;
- ASSERT_THAT(read(efd, &l, 4), SyscallFailsWithErrno(EINVAL));
+ ASSERT_THAT(read(efd.get(), &l, 4), SyscallFailsWithErrno(EINVAL));
}
TEST(EventfdTest, BigWrite) {
- int efd;
- ASSERT_THAT(efd = eventfd(0, EFD_NONBLOCK | EFD_SEMAPHORE),
- SyscallSucceeds());
+ FileDescriptor efd =
+ ASSERT_NO_ERRNO_AND_VALUE(NewEventFD(0, EFD_NONBLOCK | EFD_SEMAPHORE));
uint64_t big[16];
big[0] = 16;
- ASSERT_THAT(write(efd, big, sizeof(big)), SyscallSucceeds());
+ ASSERT_THAT(write(efd.get(), big, sizeof(big)), SyscallSucceeds());
}
TEST(EventfdTest, BigRead) {
- int efd;
- ASSERT_THAT(efd = eventfd(0, EFD_NONBLOCK | EFD_SEMAPHORE),
- SyscallSucceeds());
+ FileDescriptor efd =
+ ASSERT_NO_ERRNO_AND_VALUE(NewEventFD(0, EFD_NONBLOCK | EFD_SEMAPHORE));
uint64_t l = 1;
- ASSERT_THAT(write(efd, &l, sizeof(l)), SyscallSucceeds());
+ ASSERT_THAT(write(efd.get(), &l, sizeof(l)), SyscallSucceeds());
uint64_t big[16];
- ASSERT_THAT(read(efd, big, sizeof(big)), SyscallSucceeds());
+ ASSERT_THAT(read(efd.get(), big, sizeof(big)), SyscallSucceeds());
EXPECT_EQ(big[0], 1);
}
TEST(EventfdTest, BigWriteBigRead) {
- int efd;
- ASSERT_THAT(efd = eventfd(0, EFD_NONBLOCK | EFD_SEMAPHORE),
- SyscallSucceeds());
+ FileDescriptor efd =
+ ASSERT_NO_ERRNO_AND_VALUE(NewEventFD(0, EFD_NONBLOCK | EFD_SEMAPHORE));
uint64_t l[16];
l[0] = 16;
- ASSERT_THAT(write(efd, l, sizeof(l)), SyscallSucceeds());
- ASSERT_THAT(read(efd, l, sizeof(l)), SyscallSucceeds());
+ ASSERT_THAT(write(efd.get(), l, sizeof(l)), SyscallSucceeds());
+ ASSERT_THAT(read(efd.get(), l, sizeof(l)), SyscallSucceeds());
EXPECT_EQ(l[0], 1);
}
@@ -142,44 +137,39 @@ TEST(EventfdTest, NotifyNonZero_NoRandomSave) {
// Waits will time out at 10 seconds.
constexpr int kEpollTimeoutMs = 10000;
// Create an eventfd descriptor.
- int efd;
- ASSERT_THAT(efd = eventfd(7, EFD_SEMAPHORE | EFD_NONBLOCK),
- SyscallSucceeds());
+ FileDescriptor efd =
+ ASSERT_NO_ERRNO_AND_VALUE(NewEventFD(7, EFD_NONBLOCK | EFD_SEMAPHORE));
// Create an epoll fd to listen to efd.
- int epollfd;
- ASSERT_THAT(epollfd = epoll_create1(0), SyscallSucceeds());
+ FileDescriptor epollfd = ASSERT_NO_ERRNO_AND_VALUE(NewEpollFD());
// Add efd to epoll.
- struct epoll_event add_ev;
- add_ev.events = EPOLLIN | EPOLLET;
- add_ev.data.fd = efd;
- ASSERT_THAT(epoll_ctl(epollfd, EPOLL_CTL_ADD, efd, &add_ev),
- SyscallSucceeds());
+ ASSERT_NO_ERRNO(
+ RegisterEpollFD(epollfd.get(), efd.get(), EPOLLIN | EPOLLET, efd.get()));
// Use epoll to get a value from efd.
struct epoll_event out_ev;
- int wait_out = epoll_wait(epollfd, &out_ev, 1, kEpollTimeoutMs);
+ int wait_out = epoll_wait(epollfd.get(), &out_ev, 1, kEpollTimeoutMs);
EXPECT_EQ(wait_out, 1);
- EXPECT_EQ(efd, out_ev.data.fd);
+ EXPECT_EQ(efd.get(), out_ev.data.fd);
uint64_t val = 0;
- ASSERT_THAT(read(efd, &val, sizeof(val)), SyscallSucceeds());
+ ASSERT_THAT(read(efd.get(), &val, sizeof(val)), SyscallSucceeds());
EXPECT_EQ(val, 1);
// Start a thread that, after this thread blocks on epoll_wait, will write to
// efd. This is racy -- it's possible that this write will happen after
// epoll_wait times out.
- ScopedThread t([efd] {
+ ScopedThread t([&efd] {
sleep(5);
uint64_t val = 1;
- write(efd, &val, sizeof(val));
+ write(efd.get(), &val, sizeof(val));
});
// epoll_wait should return once the thread writes.
- wait_out = epoll_wait(epollfd, &out_ev, 1, kEpollTimeoutMs);
+ wait_out = epoll_wait(epollfd.get(), &out_ev, 1, kEpollTimeoutMs);
EXPECT_EQ(wait_out, 1);
- EXPECT_EQ(efd, out_ev.data.fd);
+ EXPECT_EQ(efd.get(), out_ev.data.fd);
val = 0;
- ASSERT_THAT(read(efd, &val, sizeof(val)), SyscallSucceeds());
+ ASSERT_THAT(read(efd.get(), &val, sizeof(val)), SyscallSucceeds());
EXPECT_EQ(val, 1);
}