2 * Copyright 2015 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.
17 // @author: Andrei Alexandrescu (aalexandre)
19 // Test bed for folly/Synchronized.h
21 #include <folly/Synchronized.h>
22 #include <folly/RWSpinLock.h>
23 #include <folly/SharedMutex.h>
24 #include <folly/test/SynchronizedTestLib.h>
25 #include <gtest/gtest.h>
29 template <class Mutex>
30 class SynchronizedTest : public testing::Test {};
32 using SynchronizedTestTypes = testing::Types
33 < folly::SharedMutexReadPriority
34 , folly::SharedMutexWritePriority
36 , std::recursive_mutex
37 #ifdef FOLLY_SYNCHRONIZED_HAVE_TIMED_MUTEXES
39 , std::recursive_timed_mutex
42 , boost::recursive_mutex
43 #ifdef FOLLY_SYNCHRONIZED_HAVE_TIMED_MUTEXES
45 , boost::recursive_timed_mutex
48 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
49 , folly::RWTicketSpinLock32
50 , folly::RWTicketSpinLock64
53 TYPED_TEST_CASE(SynchronizedTest, SynchronizedTestTypes);
55 TYPED_TEST(SynchronizedTest, Basic) {
56 testBasic<TypeParam>();
59 TYPED_TEST(SynchronizedTest, Concurrency) {
60 testConcurrency<TypeParam>();
63 TYPED_TEST(SynchronizedTest, DualLocking) {
64 testDualLocking<TypeParam>();
67 TYPED_TEST(SynchronizedTest, DualLockingWithConst) {
68 testDualLockingWithConst<TypeParam>();
71 TYPED_TEST(SynchronizedTest, ConstCopy) {
72 testConstCopy<TypeParam>();
75 template <class Mutex>
76 class SynchronizedTimedTest : public testing::Test {};
78 using SynchronizedTimedTestTypes = testing::Types
79 < folly::SharedMutexReadPriority
80 , folly::SharedMutexWritePriority
81 #ifdef FOLLY_SYNCHRONIZED_HAVE_TIMED_MUTEXES
83 , std::recursive_timed_mutex
85 , boost::recursive_timed_mutex
88 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
89 , folly::RWTicketSpinLock32
90 , folly::RWTicketSpinLock64
93 TYPED_TEST_CASE(SynchronizedTimedTest, SynchronizedTimedTestTypes);
95 TYPED_TEST(SynchronizedTimedTest, TimedSynchronized) {
96 testTimedSynchronized<TypeParam>();
99 template <class Mutex>
100 class SynchronizedTimedWithConstTest : public testing::Test {};
102 using SynchronizedTimedWithConstTestTypes = testing::Types
103 < folly::SharedMutexReadPriority
104 , folly::SharedMutexWritePriority
105 #ifdef FOLLY_SYNCHRONIZED_HAVE_TIMED_MUTEXES
106 , boost::shared_mutex
108 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
109 , folly::RWTicketSpinLock32
110 , folly::RWTicketSpinLock64
114 SynchronizedTimedWithConstTest, SynchronizedTimedWithConstTestTypes);
116 TYPED_TEST(SynchronizedTimedWithConstTest, TimedSynchronizeWithConst) {
117 testTimedSynchronizedWithConst<TypeParam>();
120 TYPED_TEST(SynchronizedTest, InPlaceConstruction) {
121 testInPlaceConstruction<TypeParam>();
124 using CountPair = std::pair<int, int>;
125 // This class is specialized only to be uesed in SynchronizedLockTest
138 static CountPair getLockUnlockCount() {
139 return CountPair{lockCount_, unlockCount_};
142 static void resetLockUnlockCount() {
147 // Keep these two static for test access
148 // Keep them thread_local in case of tests are run in parallel within one
150 static thread_local int lockCount_;
151 static thread_local int unlockCount_;
153 // Adapters for Synchronized<>
154 friend void acquireReadWrite(FakeMutex& lock) { lock.lock(); }
155 friend void releaseReadWrite(FakeMutex& lock) { lock.unlock(); }
157 thread_local int FakeMutex::lockCount_{0};
158 thread_local int FakeMutex::unlockCount_{0};
160 // SynchronizedLockTest is used to verify the correct lock unlock behavior
161 // happens per design
162 class SynchronizedLockTest : public testing::Test {
164 void SetUp() override {
165 FakeMutex::resetLockUnlockCount();
169 // Single level of SYNCHRONIZED and UNSYNCHRONIZED, although nested test are
170 // super set of it, it is possible single level test passes while nested tests
172 TEST_F(SynchronizedLockTest, SyncUnSync) {
173 folly::Synchronized<std::vector<int>, FakeMutex> obj;
174 EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
176 EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
177 UNSYNCHRONIZED(obj) {
178 EXPECT_EQ((CountPair{1, 1}), FakeMutex::getLockUnlockCount());
180 EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
182 EXPECT_EQ((CountPair{2, 2}), FakeMutex::getLockUnlockCount());
185 // Nested SYNCHRONIZED UNSYNCHRONIZED test, 2 levels for each are used here
186 TEST_F(SynchronizedLockTest, NestedSyncUnSync) {
187 folly::Synchronized<std::vector<int>, FakeMutex> obj;
188 EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
189 SYNCHRONIZED(objCopy, obj) {
190 EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
192 EXPECT_EQ((CountPair{2, 0}), FakeMutex::getLockUnlockCount());
193 UNSYNCHRONIZED(obj) {
194 EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
195 UNSYNCHRONIZED(obj) {
196 EXPECT_EQ((CountPair{2, 2}),
197 FakeMutex::getLockUnlockCount());
199 EXPECT_EQ((CountPair{3, 2}), FakeMutex::getLockUnlockCount());
201 EXPECT_EQ((CountPair{4, 2}), FakeMutex::getLockUnlockCount());
203 EXPECT_EQ((CountPair{4, 3}), FakeMutex::getLockUnlockCount());
205 EXPECT_EQ((CountPair{4, 4}), FakeMutex::getLockUnlockCount());
208 // Different nesting behavior, UNSYNCHRONIZED called on differen depth of
210 TEST_F(SynchronizedLockTest, NestedSyncUnSync2) {
211 folly::Synchronized<std::vector<int>, FakeMutex> obj;
212 EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
213 SYNCHRONIZED(objCopy, obj) {
214 EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
216 EXPECT_EQ((CountPair{2, 0}), FakeMutex::getLockUnlockCount());
217 UNSYNCHRONIZED(obj) {
218 EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
220 EXPECT_EQ((CountPair{3, 1}), FakeMutex::getLockUnlockCount());
222 EXPECT_EQ((CountPair{3, 2}), FakeMutex::getLockUnlockCount());
223 UNSYNCHRONIZED(obj) {
224 EXPECT_EQ((CountPair{3, 3}), FakeMutex::getLockUnlockCount());
226 EXPECT_EQ((CountPair{4, 3}), FakeMutex::getLockUnlockCount());
228 EXPECT_EQ((CountPair{4, 4}), FakeMutex::getLockUnlockCount());