add an unlock() method to Synchronized<T>::LockedPtr
[folly.git] / folly / test / SynchronizedTest.cpp
index 3c80c192494bb9fe5c3b5a8961574a1e5b64865d..fb77721eeb0975167871c80148723dd258dfb428 100644 (file)
@@ -18,6 +18,7 @@
 
 // Test bed for folly/Synchronized.h
 
+#include <folly/LockTraitsBoost.h>
 #include <folly/Portability.h>
 #include <folly/RWSpinLock.h>
 #include <folly/SharedMutex.h>
@@ -26,7 +27,7 @@
 #include <folly/test/SynchronizedTestLib.h>
 #include <gtest/gtest.h>
 
-namespace {
+using namespace folly::sync_tests;
 
 template <class Mutex>
 class SynchronizedTest : public testing::Test {};
@@ -58,10 +59,30 @@ TYPED_TEST(SynchronizedTest, Basic) {
   testBasic<TypeParam>();
 }
 
+TYPED_TEST(SynchronizedTest, WithLock) {
+  testWithLock<TypeParam>();
+}
+
+TYPED_TEST(SynchronizedTest, Unlock) {
+  testUnlock<TypeParam>();
+}
+
+TYPED_TEST(SynchronizedTest, Deprecated) {
+  testDeprecated<TypeParam>();
+}
+
 TYPED_TEST(SynchronizedTest, Concurrency) {
   testConcurrency<TypeParam>();
 }
 
+TYPED_TEST(SynchronizedTest, AcquireLocked) {
+  testAcquireLocked<TypeParam>();
+}
+
+TYPED_TEST(SynchronizedTest, AcquireLockedWithConst) {
+  testAcquireLockedWithConst<TypeParam>();
+}
+
 TYPED_TEST(SynchronizedTest, DualLocking) {
   testDualLocking<TypeParam>();
 }
@@ -93,6 +114,10 @@ using SynchronizedTimedTestTypes = testing::Types<
     folly::SharedMutexWritePriority>;
 TYPED_TEST_CASE(SynchronizedTimedTest, SynchronizedTimedTestTypes);
 
+TYPED_TEST(SynchronizedTimedTest, Timed) {
+  testTimed<TypeParam>();
+}
+
 TYPED_TEST(SynchronizedTimedTest, TimedSynchronized) {
   testTimedSynchronized<TypeParam>();
 }
@@ -113,6 +138,10 @@ using SynchronizedTimedWithConstTestTypes = testing::Types<
 TYPED_TEST_CASE(
     SynchronizedTimedWithConstTest, SynchronizedTimedWithConstTestTypes);
 
+TYPED_TEST(SynchronizedTimedWithConstTest, TimedShared) {
+  testTimedShared<TypeParam>();
+}
+
 TYPED_TEST(SynchronizedTimedWithConstTest, TimedSynchronizeWithConst) {
   testTimedSynchronizedWithConst<TypeParam>();
 }
@@ -178,7 +207,7 @@ TEST_F(SynchronizedLockTest, SyncUnSync) {
   EXPECT_EQ((CountPair{2, 2}), FakeMutex::getLockUnlockCount());
 }
 
-// Nested SYNCHRONIZED UNSYNCHRONIZED test, 2 levels for each are used here
+// Nested SYNCHRONIZED UNSYNCHRONIZED test, 2 levels of synchronization
 TEST_F(SynchronizedLockTest, NestedSyncUnSync) {
   folly::Synchronized<std::vector<int>, FakeMutex> obj;
   EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
@@ -186,12 +215,15 @@ TEST_F(SynchronizedLockTest, NestedSyncUnSync) {
     EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
     SYNCHRONIZED(obj) {
       EXPECT_EQ((CountPair{2, 0}), FakeMutex::getLockUnlockCount());
+      // Note: UNSYNCHRONIZED has always been kind of broken here.
+      // The input parameter is ignored (other than to overwrite what the input
+      // variable name refers to), and it unlocks the most object acquired in
+      // the most recent SYNCHRONIZED scope.
       UNSYNCHRONIZED(obj) {
         EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
-        UNSYNCHRONIZED(obj) {
-          EXPECT_EQ((CountPair{2, 2}),
-                    FakeMutex::getLockUnlockCount());
-        }
+      }
+      EXPECT_EQ((CountPair{3, 1}), FakeMutex::getLockUnlockCount());
+      UNSYNCHRONIZED(obj) {
         EXPECT_EQ((CountPair{3, 2}), FakeMutex::getLockUnlockCount());
       }
       EXPECT_EQ((CountPair{4, 2}), FakeMutex::getLockUnlockCount());
@@ -201,7 +233,7 @@ TEST_F(SynchronizedLockTest, NestedSyncUnSync) {
   EXPECT_EQ((CountPair{4, 4}), FakeMutex::getLockUnlockCount());
 }
 
-// Different nesting behavior, UNSYNCHRONIZED called on differen depth of
+// Different nesting behavior, UNSYNCHRONIZED called on different depth of
 // SYNCHRONIZED
 TEST_F(SynchronizedLockTest, NestedSyncUnSync2) {
   folly::Synchronized<std::vector<int>, FakeMutex> obj;
@@ -223,4 +255,3 @@ TEST_F(SynchronizedLockTest, NestedSyncUnSync2) {
   }
   EXPECT_EQ((CountPair{4, 4}), FakeMutex::getLockUnlockCount());
 }
-}