de45a4045b97d237dec722a4654f146fb99c9262
[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, WithLock) {
63   testWithLock<TypeParam>();
64 }
65
66 TYPED_TEST(SynchronizedTest, Deprecated) {
67   testDeprecated<TypeParam>();
68 }
69
70 TYPED_TEST(SynchronizedTest, Concurrency) {
71   testConcurrency<TypeParam>();
72 }
73
74 TYPED_TEST(SynchronizedTest, AcquireLocked) {
75   testAcquireLocked<TypeParam>();
76 }
77
78 TYPED_TEST(SynchronizedTest, AcquireLockedWithConst) {
79   testAcquireLockedWithConst<TypeParam>();
80 }
81
82 TYPED_TEST(SynchronizedTest, DualLocking) {
83   testDualLocking<TypeParam>();
84 }
85
86 TYPED_TEST(SynchronizedTest, DualLockingWithConst) {
87   testDualLockingWithConst<TypeParam>();
88 }
89
90 TYPED_TEST(SynchronizedTest, ConstCopy) {
91   testConstCopy<TypeParam>();
92 }
93
94 template <class Mutex>
95 class SynchronizedTimedTest : public testing::Test {};
96
97 using SynchronizedTimedTestTypes = testing::Types<
98 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
99     std::timed_mutex,
100     std::recursive_timed_mutex,
101     boost::timed_mutex,
102     boost::recursive_timed_mutex,
103     boost::shared_mutex,
104 #endif
105 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
106     folly::RWTicketSpinLock32,
107     folly::RWTicketSpinLock64,
108 #endif
109     folly::SharedMutexReadPriority,
110     folly::SharedMutexWritePriority>;
111 TYPED_TEST_CASE(SynchronizedTimedTest, SynchronizedTimedTestTypes);
112
113 TYPED_TEST(SynchronizedTimedTest, Timed) {
114   testTimed<TypeParam>();
115 }
116
117 TYPED_TEST(SynchronizedTimedTest, TimedSynchronized) {
118   testTimedSynchronized<TypeParam>();
119 }
120
121 template <class Mutex>
122 class SynchronizedTimedWithConstTest : public testing::Test {};
123
124 using SynchronizedTimedWithConstTestTypes = testing::Types<
125 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
126     boost::shared_mutex,
127 #endif
128 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
129     folly::RWTicketSpinLock32,
130     folly::RWTicketSpinLock64,
131 #endif
132     folly::SharedMutexReadPriority,
133     folly::SharedMutexWritePriority>;
134 TYPED_TEST_CASE(
135     SynchronizedTimedWithConstTest, SynchronizedTimedWithConstTestTypes);
136
137 TYPED_TEST(SynchronizedTimedWithConstTest, TimedShared) {
138   testTimedShared<TypeParam>();
139 }
140
141 TYPED_TEST(SynchronizedTimedWithConstTest, TimedSynchronizeWithConst) {
142   testTimedSynchronizedWithConst<TypeParam>();
143 }
144
145 TYPED_TEST(SynchronizedTest, InPlaceConstruction) {
146   testInPlaceConstruction<TypeParam>();
147 }
148
149 using CountPair = std::pair<int, int>;
150 // This class is specialized only to be uesed in SynchronizedLockTest
151 class FakeMutex {
152  public:
153   bool lock() {
154     ++lockCount_;
155     return true;
156   }
157
158   bool unlock() {
159     ++unlockCount_;
160     return true;
161   }
162
163   static CountPair getLockUnlockCount() {
164     return CountPair{lockCount_, unlockCount_};
165   }
166
167   static void resetLockUnlockCount() {
168     lockCount_ = 0;
169     unlockCount_ = 0;
170   }
171  private:
172   // Keep these two static for test access
173   // Keep them thread_local in case of tests are run in parallel within one
174   // process
175   static FOLLY_TLS int lockCount_;
176   static FOLLY_TLS int unlockCount_;
177 };
178 FOLLY_TLS int FakeMutex::lockCount_{0};
179 FOLLY_TLS int FakeMutex::unlockCount_{0};
180
181 // SynchronizedLockTest is used to verify the correct lock unlock behavior
182 // happens per design
183 class SynchronizedLockTest : public testing::Test {
184  public:
185   void SetUp() override {
186     FakeMutex::resetLockUnlockCount();
187   }
188 };
189
190 // Single level of SYNCHRONIZED and UNSYNCHRONIZED, although nested test are
191 // super set of it, it is possible single level test passes while nested tests
192 // fail
193 TEST_F(SynchronizedLockTest, SyncUnSync) {
194   folly::Synchronized<std::vector<int>, FakeMutex> obj;
195   EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
196   SYNCHRONIZED(obj) {
197     EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
198     UNSYNCHRONIZED(obj) {
199       EXPECT_EQ((CountPair{1, 1}), FakeMutex::getLockUnlockCount());
200     }
201     EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
202   }
203   EXPECT_EQ((CountPair{2, 2}), FakeMutex::getLockUnlockCount());
204 }
205
206 // Nested SYNCHRONIZED UNSYNCHRONIZED test, 2 levels of synchronization
207 TEST_F(SynchronizedLockTest, NestedSyncUnSync) {
208   folly::Synchronized<std::vector<int>, FakeMutex> obj;
209   EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
210   SYNCHRONIZED(objCopy, obj) {
211     EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
212     SYNCHRONIZED(obj) {
213       EXPECT_EQ((CountPair{2, 0}), FakeMutex::getLockUnlockCount());
214       // Note: UNSYNCHRONIZED has always been kind of broken here.
215       // The input parameter is ignored (other than to overwrite what the input
216       // variable name refers to), and it unlocks the most object acquired in
217       // the most recent SYNCHRONIZED scope.
218       UNSYNCHRONIZED(obj) {
219         EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
220       }
221       EXPECT_EQ((CountPair{3, 1}), FakeMutex::getLockUnlockCount());
222       UNSYNCHRONIZED(obj) {
223         EXPECT_EQ((CountPair{3, 2}), FakeMutex::getLockUnlockCount());
224       }
225       EXPECT_EQ((CountPair{4, 2}), FakeMutex::getLockUnlockCount());
226     }
227     EXPECT_EQ((CountPair{4, 3}), FakeMutex::getLockUnlockCount());
228   }
229   EXPECT_EQ((CountPair{4, 4}), FakeMutex::getLockUnlockCount());
230 }
231
232 // Different nesting behavior, UNSYNCHRONIZED called on different depth of
233 // SYNCHRONIZED
234 TEST_F(SynchronizedLockTest, NestedSyncUnSync2) {
235   folly::Synchronized<std::vector<int>, FakeMutex> obj;
236   EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
237   SYNCHRONIZED(objCopy, obj) {
238     EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
239     SYNCHRONIZED(obj) {
240       EXPECT_EQ((CountPair{2, 0}), FakeMutex::getLockUnlockCount());
241       UNSYNCHRONIZED(obj) {
242         EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
243       }
244       EXPECT_EQ((CountPair{3, 1}), FakeMutex::getLockUnlockCount());
245     }
246     EXPECT_EQ((CountPair{3, 2}), FakeMutex::getLockUnlockCount());
247     UNSYNCHRONIZED(obj) {
248       EXPECT_EQ((CountPair{3, 3}), FakeMutex::getLockUnlockCount());
249     }
250     EXPECT_EQ((CountPair{4, 3}), FakeMutex::getLockUnlockCount());
251   }
252   EXPECT_EQ((CountPair{4, 4}), FakeMutex::getLockUnlockCount());
253 }