Futex::futexWait returns FutexResult
[folly.git] / folly / test / MemoryIdlerTest.cpp
index bcf3fd6ccdd4f0d8fd2b343464f94ecc67ecba08..6945f22e87a4f360be7700b89c9383133fb3946e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2014-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 #include <folly/detail/MemoryIdler.h>
 
-#include <folly/Baton.h>
-#include <folly/portability/Windows.h>
+#include <folly/portability/GMock.h>
+#include <folly/portability/GTest.h>
+#include <folly/synchronization/Baton.h>
 
 #include <memory>
 #include <thread>
-#include <assert.h>
-#include <semaphore.h>
-
-#include <gmock/gmock.h>
-#include <gtest/gtest.h>
 
 using namespace folly;
 using namespace folly::detail;
@@ -97,14 +93,15 @@ namespace folly { namespace detail {
 /// method signatures differ from the real Futex because we have elided
 /// unused default params and collapsed templated methods into the
 /// used type
-template<>
+template <>
 struct Futex<MockAtom> {
-  MOCK_METHOD2(futexWait, bool(uint32_t, uint32_t));
+  MOCK_METHOD2(futexWait, FutexResult(uint32_t, uint32_t));
   MOCK_METHOD3(futexWaitUntil,
                FutexResult(uint32_t, const MockClock::time_point&, uint32_t));
 };
 
-}}
+} // namespace detail
+} // namespace folly
 
 TEST(MemoryIdler, futexWaitValueChangedEarly) {
   StrictMock<Futex<MockAtom>> fut;
@@ -117,7 +114,9 @@ TEST(MemoryIdler, futexWaitValueChangedEarly) {
   EXPECT_CALL(fut, futexWaitUntil(1, AllOf(Ge(begin + idleTimeout),
                                            Lt(begin + 2 * idleTimeout)), -1))
       .WillOnce(Return(FutexResult::VALUE_CHANGED));
-  EXPECT_FALSE((MemoryIdler::futexWait<MockAtom, MockClock>(fut, 1)));
+  EXPECT_EQ(
+      FutexResult::VALUE_CHANGED,
+      (MemoryIdler::futexWait<MockAtom, MockClock>(fut, 1)));
 }
 
 TEST(MemoryIdler, futexWaitValueChangedLate) {
@@ -132,8 +131,10 @@ TEST(MemoryIdler, futexWaitValueChangedLate) {
                                            Lt(begin + 2 * idleTimeout)), -1))
       .WillOnce(Return(FutexResult::TIMEDOUT));
   EXPECT_CALL(fut, futexWait(1, -1))
-      .WillOnce(Return(false));
-  EXPECT_FALSE((MemoryIdler::futexWait<MockAtom, MockClock>(fut, 1)));
+      .WillOnce(Return(FutexResult::VALUE_CHANGED));
+  EXPECT_EQ(
+      FutexResult::VALUE_CHANGED,
+      (MemoryIdler::futexWait<MockAtom, MockClock>(fut, 1)));
 }
 
 TEST(MemoryIdler, futexWaitAwokenEarly) {
@@ -146,7 +147,9 @@ TEST(MemoryIdler, futexWaitAwokenEarly) {
       .WillOnce(Return(begin));
   EXPECT_CALL(fut, futexWaitUntil(1, Ge(begin + idleTimeout), -1))
       .WillOnce(Return(FutexResult::AWOKEN));
-  EXPECT_TRUE((MemoryIdler::futexWait<MockAtom, MockClock>(fut, 1)));
+  EXPECT_EQ(
+      FutexResult::AWOKEN,
+      (MemoryIdler::futexWait<MockAtom, MockClock>(fut, 1)));
 }
 
 TEST(MemoryIdler, futexWaitAwokenLate) {
@@ -159,28 +162,31 @@ TEST(MemoryIdler, futexWaitAwokenLate) {
       .WillOnce(Return(begin));
   EXPECT_CALL(fut, futexWaitUntil(1, begin + idleTimeout, -1))
       .WillOnce(Return(FutexResult::TIMEDOUT));
-  EXPECT_CALL(fut, futexWait(1, -1))
-      .WillOnce(Return(true));
-  EXPECT_TRUE((MemoryIdler::futexWait<MockAtom, MockClock>(
-      fut, 1, -1, idleTimeout, 100, 0.0f)));
+  EXPECT_CALL(fut, futexWait(1, -1)).WillOnce(Return(FutexResult::AWOKEN));
+  EXPECT_EQ(
+      FutexResult::AWOKEN,
+      (MemoryIdler::futexWait<MockAtom, MockClock>(
+          fut, 1, -1, idleTimeout, 100, 0.0f)));
 }
 
 TEST(MemoryIdler, futexWaitImmediateFlush) {
   StrictMock<Futex<MockAtom>> fut;
   auto clock = MockClock::setup();
 
-  EXPECT_CALL(fut, futexWait(2, 0xff))
-      .WillOnce(Return(true));
-  EXPECT_TRUE((MemoryIdler::futexWait<MockAtom, MockClock>(
-      fut, 2, 0xff, std::chrono::seconds(0))));
+  EXPECT_CALL(fut, futexWait(2, 0xff)).WillOnce(Return(FutexResult::AWOKEN));
+  EXPECT_EQ(
+      FutexResult::AWOKEN,
+      (MemoryIdler::futexWait<MockAtom, MockClock>(
+          fut, 2, 0xff, std::chrono::seconds(0))));
 }
 
 TEST(MemoryIdler, futexWaitNeverFlush) {
   StrictMock<Futex<MockAtom>> fut;
   auto clock = MockClock::setup();
 
-  EXPECT_CALL(fut, futexWait(1, -1))
-      .WillOnce(Return(true));
-  EXPECT_TRUE((MemoryIdler::futexWait<MockAtom, MockClock>(
-      fut, 1, -1, MockClock::duration::max())));
+  EXPECT_CALL(fut, futexWait(1, -1)).WillOnce(Return(FutexResult::AWOKEN));
+  EXPECT_EQ(
+      FutexResult::AWOKEN,
+      (MemoryIdler::futexWait<MockAtom, MockClock>(
+          fut, 1, -1, MockClock::duration::max())));
 }