2 * Copyright 2012 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <gtest/gtest.h>
19 #include "folly/eventfd.h"
20 #include <glog/logging.h>
22 using namespace folly;
24 TEST(EventFD, Basic) {
25 int fd = eventfd(10, EFD_NONBLOCK);
29 // Running this twice -- once with the initial value, and once
31 for (int attempt = 0; attempt < 2; attempt++) {
33 r = read(fd, &val, sizeof(val));
35 EXPECT_EQ(sizeof(val), r);
37 r = read(fd, &val, sizeof(val));
39 EXPECT_EQ(EAGAIN, errno);
41 r = write(fd, &val, sizeof(val));
43 EXPECT_EQ(sizeof(val), r);
48 TEST(EventFD, Semaphore) {
49 int fd = eventfd(10, EFD_NONBLOCK | EFD_SEMAPHORE);
53 // Running this twice -- once with the initial value, and once
55 for (int attempt = 0; attempt < 2; attempt++) {
57 for (int i = 0; i < 10; i++) {
58 r = read(fd, &val, sizeof(val));
60 EXPECT_EQ(sizeof(val), r);
63 r = read(fd, &val, sizeof(val));
65 EXPECT_EQ(EAGAIN, errno);
67 r = write(fd, &val, sizeof(val));
69 EXPECT_EQ(sizeof(val), r);