add cancelFunctionAndWait and cancelAllFunctionsAndWait to FunctionScheduler
[folly.git] / folly / experimental / test / LockFreeRingBufferTest.cpp
index 4b2447d3e1e6a915d41c9967ecc9119175614858..edd666b53f4f5986ae6042ee8e9efc89fd867fe3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-#include <gflags/gflags.h>
-#include <gtest/gtest.h>
 #include <iostream>
 #include <thread>
 
 #include <folly/detail/Futex.h>
 #include <folly/experimental/LockFreeRingBuffer.h>
 #include <folly/test/DeterministicSchedule.h>
+#include <folly/portability/GTest.h>
 
 namespace folly {
 
@@ -95,18 +94,16 @@ TEST(LockFreeRingBuffer, readsCanBlock) {
 
 // expose the cursor raw value via a wrapper type
 template<typename T, template<typename> class Atom>
-uint64_t value(const typename LockFreeRingBuffer<T, Atom>::Cursor&& rbcursor) {
+uint64_t value(const typename LockFreeRingBuffer<T, Atom>::Cursor& rbcursor) {
   typedef typename LockFreeRingBuffer<T,Atom>::Cursor RBCursor;
 
-  RBCursor cursor = std::move(rbcursor);
-
   struct ExposedCursor : RBCursor {
     ExposedCursor(const RBCursor& cursor): RBCursor(cursor) {}
     uint64_t value(){
       return this->ticket;
     }
   };
-  return ExposedCursor(cursor).value();
+  return ExposedCursor(rbcursor).value();
 }
 
 template<template<typename> class Atom>
@@ -243,4 +240,22 @@ TEST(LockFreeRingBuffer, cursorFromWrites) {
   EXPECT_EQ(3, cursorValue(rb.writeAndGetCursor(val)));
 }
 
+TEST(LockFreeRingBuffer, moveBackwardsCanFail) {
+  const int capacity = 3;
+  LockFreeRingBuffer<int> rb(capacity);
+
+  // Workaround for template deduction failure
+  auto (&cursorValue)(value<int, std::atomic>);
+
+  int val = 0xfaceb00c;
+  rb.write(val);
+  rb.write(val);
+
+  auto cursor = rb.currentHead(); // points to 2
+  EXPECT_EQ(2, cursorValue(cursor));
+  EXPECT_TRUE(cursor.moveBackward());
+  EXPECT_TRUE(cursor.moveBackward()); // now at 0
+  EXPECT_FALSE(cursor.moveBackward()); // moving back does nothing
+}
+
 } // namespace folly