improve Synchronized LockedPtr class, and add new lock() APIs
[folly.git] / folly / test / SynchronizedTest.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // @author: Andrei Alexandrescu (aalexandre)
18
19 // Test bed for folly/Synchronized.h
20
21 #include <folly/LockTraitsBoost.h>
22 #include <folly/Portability.h>
23 #include <folly/RWSpinLock.h>
24 #include <folly/SharedMutex.h>
25 #include <folly/SpinLock.h>
26 #include <folly/Synchronized.h>
27 #include <folly/test/SynchronizedTestLib.h>
28 #include <gtest/gtest.h>
29
30 using namespace folly::sync_tests;
31
32 template <class Mutex>
33 class SynchronizedTest : public testing::Test {};
34
35 using SynchronizedTestTypes = testing::Types<
36     folly::SharedMutexReadPriority,
37     folly::SharedMutexWritePriority,
38     std::mutex,
39     std::recursive_mutex,
40 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
41     std::timed_mutex,
42     std::recursive_timed_mutex,
43 #endif
44     boost::mutex,
45     boost::recursive_mutex,
46 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
47     boost::timed_mutex,
48     boost::recursive_timed_mutex,
49 #endif
50 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
51     folly::RWTicketSpinLock32,
52     folly::RWTicketSpinLock64,
53 #endif
54     boost::shared_mutex,
55     folly::SpinLock>;
56 TYPED_TEST_CASE(SynchronizedTest, SynchronizedTestTypes);
57
58 TYPED_TEST(SynchronizedTest, Basic) {
59   testBasic<TypeParam>();
60 }
61
62 TYPED_TEST(SynchronizedTest, Deprecated) {
63   testDeprecated<TypeParam>();
64 }
65
66 TYPED_TEST(SynchronizedTest, Concurrency) {
67   testConcurrency<TypeParam>();
68 }
69
70 TYPED_TEST(SynchronizedTest, AcquireLocked) {
71   testAcquireLocked<TypeParam>();
72 }
73
74 TYPED_TEST(SynchronizedTest, AcquireLockedWithConst) {
75   testAcquireLockedWithConst<TypeParam>();
76 }
77
78 TYPED_TEST(SynchronizedTest, DualLocking) {
79   testDualLocking<TypeParam>();
80 }
81
82 TYPED_TEST(SynchronizedTest, DualLockingWithConst) {
83   testDualLockingWithConst<TypeParam>();
84 }
85
86 TYPED_TEST(SynchronizedTest, ConstCopy) {
87   testConstCopy<TypeParam>();
88 }
89
90 template <class Mutex>
91 class SynchronizedTimedTest : public testing::Test {};
92
93 using SynchronizedTimedTestTypes = testing::Types<
94 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
95     std::timed_mutex,
96     std::recursive_timed_mutex,
97     boost::timed_mutex,
98     boost::recursive_timed_mutex,
99     boost::shared_mutex,
100 #endif
101 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
102     folly::RWTicketSpinLock32,
103     folly::RWTicketSpinLock64,
104 #endif
105     folly::SharedMutexReadPriority,
106     folly::SharedMutexWritePriority>;
107 TYPED_TEST_CASE(SynchronizedTimedTest, SynchronizedTimedTestTypes);
108
109 TYPED_TEST(SynchronizedTimedTest, Timed) {
110   testTimed<TypeParam>();
111 }
112
113 TYPED_TEST(SynchronizedTimedTest, TimedSynchronized) {
114   testTimedSynchronized<TypeParam>();
115 }
116
117 template <class Mutex>
118 class SynchronizedTimedWithConstTest : public testing::Test {};
119
120 using SynchronizedTimedWithConstTestTypes = testing::Types<
121 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
122     boost::shared_mutex,
123 #endif
124 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
125     folly::RWTicketSpinLock32,
126     folly::RWTicketSpinLock64,
127 #endif
128     folly::SharedMutexReadPriority,
129     folly::SharedMutexWritePriority>;
130 TYPED_TEST_CASE(
131     SynchronizedTimedWithConstTest, SynchronizedTimedWithConstTestTypes);
132
133 TYPED_TEST(SynchronizedTimedWithConstTest, TimedShared) {
134   testTimedShared<TypeParam>();
135 }
136
137 TYPED_TEST(SynchronizedTimedWithConstTest, TimedSynchronizeWithConst) {
138   testTimedSynchronizedWithConst<TypeParam>();
139 }
140
141 TYPED_TEST(SynchronizedTest, InPlaceConstruction) {
142   testInPlaceConstruction<TypeParam>();
143 }
144
145 using CountPair = std::pair<int, int>;
146 // This class is specialized only to be uesed in SynchronizedLockTest
147 class FakeMutex {
148  public:
149   bool lock() {
150     ++lockCount_;
151     return true;
152   }
153
154   bool unlock() {
155     ++unlockCount_;
156     return true;
157   }
158
159   static CountPair getLockUnlockCount() {
160     return CountPair{lockCount_, unlockCount_};
161   }
162
163   static void resetLockUnlockCount() {
164     lockCount_ = 0;
165     unlockCount_ = 0;
166   }
167  private:
168   // Keep these two static for test access
169   // Keep them thread_local in case of tests are run in parallel within one
170   // process
171   static FOLLY_TLS int lockCount_;
172   static FOLLY_TLS int unlockCount_;
173 };
174 FOLLY_TLS int FakeMutex::lockCount_{0};
175 FOLLY_TLS int FakeMutex::unlockCount_{0};
176
177 // SynchronizedLockTest is used to verify the correct lock unlock behavior
178 // happens per design
179 class SynchronizedLockTest : public testing::Test {
180  public:
181   void SetUp() override {
182     FakeMutex::resetLockUnlockCount();
183   }
184 };
185
186 // Single level of SYNCHRONIZED and UNSYNCHRONIZED, although nested test are
187 // super set of it, it is possible single level test passes while nested tests
188 // fail
189 TEST_F(SynchronizedLockTest, SyncUnSync) {
190   folly::Synchronized<std::vector<int>, FakeMutex> obj;
191   EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
192   SYNCHRONIZED(obj) {
193     EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
194     UNSYNCHRONIZED(obj) {
195       EXPECT_EQ((CountPair{1, 1}), FakeMutex::getLockUnlockCount());
196     }
197     EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
198   }
199   EXPECT_EQ((CountPair{2, 2}), FakeMutex::getLockUnlockCount());
200 }
201
202 // Nested SYNCHRONIZED UNSYNCHRONIZED test, 2 levels of synchronization
203 TEST_F(SynchronizedLockTest, NestedSyncUnSync) {
204   folly::Synchronized<std::vector<int>, FakeMutex> obj;
205   EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
206   SYNCHRONIZED(objCopy, obj) {
207     EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
208     SYNCHRONIZED(obj) {
209       EXPECT_EQ((CountPair{2, 0}), FakeMutex::getLockUnlockCount());
210       // Note: UNSYNCHRONIZED has always been kind of broken here.
211       // The input parameter is ignored (other than to overwrite what the input
212       // variable name refers to), and it unlocks the most object acquired in
213       // the most recent SYNCHRONIZED scope.
214       UNSYNCHRONIZED(obj) {
215         EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
216       }
217       EXPECT_EQ((CountPair{3, 1}), FakeMutex::getLockUnlockCount());
218       UNSYNCHRONIZED(obj) {
219         EXPECT_EQ((CountPair{3, 2}), FakeMutex::getLockUnlockCount());
220       }
221       EXPECT_EQ((CountPair{4, 2}), FakeMutex::getLockUnlockCount());
222     }
223     EXPECT_EQ((CountPair{4, 3}), FakeMutex::getLockUnlockCount());
224   }
225   EXPECT_EQ((CountPair{4, 4}), FakeMutex::getLockUnlockCount());
226 }
227
228 // Different nesting behavior, UNSYNCHRONIZED called on different depth of
229 // SYNCHRONIZED
230 TEST_F(SynchronizedLockTest, NestedSyncUnSync2) {
231   folly::Synchronized<std::vector<int>, FakeMutex> obj;
232   EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
233   SYNCHRONIZED(objCopy, obj) {
234     EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
235     SYNCHRONIZED(obj) {
236       EXPECT_EQ((CountPair{2, 0}), FakeMutex::getLockUnlockCount());
237       UNSYNCHRONIZED(obj) {
238         EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
239       }
240       EXPECT_EQ((CountPair{3, 1}), FakeMutex::getLockUnlockCount());
241     }
242     EXPECT_EQ((CountPair{3, 2}), FakeMutex::getLockUnlockCount());
243     UNSYNCHRONIZED(obj) {
244       EXPECT_EQ((CountPair{3, 3}), FakeMutex::getLockUnlockCount());
245     }
246     EXPECT_EQ((CountPair{4, 3}), FakeMutex::getLockUnlockCount());
247   }
248   EXPECT_EQ((CountPair{4, 4}), FakeMutex::getLockUnlockCount());
249 }