4aaeadabd49755aaee38c3e47010fc02916e6922
[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, Unlock) {
67   testUnlock<TypeParam>();
68 }
69
70 TYPED_TEST(SynchronizedTest, Deprecated) {
71   testDeprecated<TypeParam>();
72 }
73
74 TYPED_TEST(SynchronizedTest, Concurrency) {
75   testConcurrency<TypeParam>();
76 }
77
78 TYPED_TEST(SynchronizedTest, AcquireLocked) {
79   testAcquireLocked<TypeParam>();
80 }
81
82 TYPED_TEST(SynchronizedTest, AcquireLockedWithConst) {
83   testAcquireLockedWithConst<TypeParam>();
84 }
85
86 TYPED_TEST(SynchronizedTest, DualLocking) {
87   testDualLocking<TypeParam>();
88 }
89
90 TYPED_TEST(SynchronizedTest, DualLockingWithConst) {
91   testDualLockingWithConst<TypeParam>();
92 }
93
94 TYPED_TEST(SynchronizedTest, ConstCopy) {
95   testConstCopy<TypeParam>();
96 }
97
98 template <class Mutex>
99 class SynchronizedTimedTest : public testing::Test {};
100
101 using SynchronizedTimedTestTypes = testing::Types<
102 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
103     std::timed_mutex,
104     std::recursive_timed_mutex,
105     boost::timed_mutex,
106     boost::recursive_timed_mutex,
107     boost::shared_mutex,
108 #endif
109 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
110     folly::RWTicketSpinLock32,
111     folly::RWTicketSpinLock64,
112 #endif
113     folly::SharedMutexReadPriority,
114     folly::SharedMutexWritePriority>;
115 TYPED_TEST_CASE(SynchronizedTimedTest, SynchronizedTimedTestTypes);
116
117 TYPED_TEST(SynchronizedTimedTest, Timed) {
118   testTimed<TypeParam>();
119 }
120
121 TYPED_TEST(SynchronizedTimedTest, TimedSynchronized) {
122   testTimedSynchronized<TypeParam>();
123 }
124
125 template <class Mutex>
126 class SynchronizedTimedWithConstTest : public testing::Test {};
127
128 using SynchronizedTimedWithConstTestTypes = testing::Types<
129 #if FOLLY_LOCK_TRAITS_HAVE_TIMED_MUTEXES
130     boost::shared_mutex,
131 #endif
132 #ifdef RW_SPINLOCK_USE_X86_INTRINSIC_
133     folly::RWTicketSpinLock32,
134     folly::RWTicketSpinLock64,
135 #endif
136     folly::SharedMutexReadPriority,
137     folly::SharedMutexWritePriority>;
138 TYPED_TEST_CASE(
139     SynchronizedTimedWithConstTest, SynchronizedTimedWithConstTestTypes);
140
141 TYPED_TEST(SynchronizedTimedWithConstTest, TimedShared) {
142   testTimedShared<TypeParam>();
143 }
144
145 TYPED_TEST(SynchronizedTimedWithConstTest, TimedSynchronizeWithConst) {
146   testTimedSynchronizedWithConst<TypeParam>();
147 }
148
149 TYPED_TEST(SynchronizedTest, InPlaceConstruction) {
150   testInPlaceConstruction<TypeParam>();
151 }
152
153 using CountPair = std::pair<int, int>;
154 // This class is specialized only to be uesed in SynchronizedLockTest
155 class FakeMutex {
156  public:
157   void lock() {
158     ++lockCount_;
159   }
160
161   void unlock() {
162     ++unlockCount_;
163   }
164
165   static CountPair getLockUnlockCount() {
166     return CountPair{lockCount_, unlockCount_};
167   }
168
169   static void resetLockUnlockCount() {
170     lockCount_ = 0;
171     unlockCount_ = 0;
172   }
173  private:
174   // Keep these two static for test access
175   // Keep them thread_local in case of tests are run in parallel within one
176   // process
177   static FOLLY_TLS int lockCount_;
178   static FOLLY_TLS int unlockCount_;
179 };
180 FOLLY_TLS int FakeMutex::lockCount_{0};
181 FOLLY_TLS int FakeMutex::unlockCount_{0};
182
183 // SynchronizedLockTest is used to verify the correct lock unlock behavior
184 // happens per design
185 class SynchronizedLockTest : public testing::Test {
186  public:
187   void SetUp() override {
188     FakeMutex::resetLockUnlockCount();
189   }
190 };
191
192 /**
193  * Test mutex to help to automate assertions, taken from LockTraitsTest.cpp
194  */
195 class FakeAllPowerfulAssertingMutexInternal {
196  public:
197   enum class CurrentLockState { UNLOCKED, SHARED, UPGRADE, UNIQUE };
198
199   void lock() {
200     EXPECT_EQ(this->lock_state, CurrentLockState::UNLOCKED);
201     this->lock_state = CurrentLockState::UNIQUE;
202   }
203   void unlock() {
204     EXPECT_EQ(this->lock_state, CurrentLockState::UNIQUE);
205     this->lock_state = CurrentLockState::UNLOCKED;
206   }
207   void lock_shared() {
208     EXPECT_EQ(this->lock_state, CurrentLockState::UNLOCKED);
209     this->lock_state = CurrentLockState::SHARED;
210   }
211   void unlock_shared() {
212     EXPECT_EQ(this->lock_state, CurrentLockState::SHARED);
213     this->lock_state = CurrentLockState::UNLOCKED;
214   }
215   void lock_upgrade() {
216     EXPECT_EQ(this->lock_state, CurrentLockState::UNLOCKED);
217     this->lock_state = CurrentLockState::UPGRADE;
218   }
219   void unlock_upgrade() {
220     EXPECT_EQ(this->lock_state, CurrentLockState::UPGRADE);
221     this->lock_state = CurrentLockState::UNLOCKED;
222   }
223
224   void unlock_upgrade_and_lock() {
225     EXPECT_EQ(this->lock_state, CurrentLockState::UPGRADE);
226     this->lock_state = CurrentLockState::UNIQUE;
227   }
228   void unlock_and_lock_upgrade() {
229     EXPECT_EQ(this->lock_state, CurrentLockState::UNIQUE);
230     this->lock_state = CurrentLockState::UPGRADE;
231   }
232   void unlock_and_lock_shared() {
233     EXPECT_EQ(this->lock_state, CurrentLockState::UNIQUE);
234     this->lock_state = CurrentLockState::SHARED;
235   }
236   void unlock_upgrade_and_lock_shared() {
237     EXPECT_EQ(this->lock_state, CurrentLockState::UPGRADE);
238     this->lock_state = CurrentLockState::SHARED;
239   }
240
241   template <class Rep, class Period>
242   bool try_lock_for(const std::chrono::duration<Rep, Period>&) {
243     EXPECT_EQ(this->lock_state, CurrentLockState::UNLOCKED);
244     this->lock_state = CurrentLockState::UNIQUE;
245     return true;
246   }
247
248   template <class Rep, class Period>
249   bool try_lock_upgrade_for(const std::chrono::duration<Rep, Period>&) {
250     EXPECT_EQ(this->lock_state, CurrentLockState::UNLOCKED);
251     this->lock_state = CurrentLockState::UPGRADE;
252     return true;
253   }
254
255   template <class Rep, class Period>
256   bool try_unlock_upgrade_and_lock_for(
257       const std::chrono::duration<Rep, Period>&) {
258     EXPECT_EQ(this->lock_state, CurrentLockState::UPGRADE);
259     this->lock_state = CurrentLockState::UNIQUE;
260     return true;
261   }
262
263   /*
264    * Initialize the FakeMutex with an unlocked state
265    */
266   CurrentLockState lock_state{CurrentLockState::UNLOCKED};
267 };
268
269 /**
270  * The following works around the internal mutex for synchronized being
271  * private
272  *
273  * This is horridly thread unsafe.
274  */
275 static FakeAllPowerfulAssertingMutexInternal globalAllPowerfulAssertingMutex;
276
277 class FakeAllPowerfulAssertingMutex {
278  public:
279   void lock() {
280     globalAllPowerfulAssertingMutex.lock();
281   }
282   void unlock() {
283     globalAllPowerfulAssertingMutex.unlock();
284   }
285   void lock_shared() {
286     globalAllPowerfulAssertingMutex.lock_shared();
287   }
288   void unlock_shared() {
289     globalAllPowerfulAssertingMutex.unlock_shared();
290   }
291   void lock_upgrade() {
292     globalAllPowerfulAssertingMutex.lock_upgrade();
293   }
294   void unlock_upgrade() {
295     globalAllPowerfulAssertingMutex.unlock_upgrade();
296   }
297
298   void unlock_upgrade_and_lock() {
299     globalAllPowerfulAssertingMutex.unlock_upgrade_and_lock();
300   }
301   void unlock_and_lock_upgrade() {
302     globalAllPowerfulAssertingMutex.unlock_and_lock_upgrade();
303   }
304   void unlock_and_lock_shared() {
305     globalAllPowerfulAssertingMutex.unlock_and_lock_shared();
306   }
307   void unlock_upgrade_and_lock_shared() {
308     globalAllPowerfulAssertingMutex.unlock_upgrade_and_lock_shared();
309   }
310
311   template <class Rep, class Period>
312   bool try_lock_for(const std::chrono::duration<Rep, Period>& arg) {
313     return globalAllPowerfulAssertingMutex.try_lock_for(arg);
314   }
315
316   template <class Rep, class Period>
317   bool try_lock_upgrade_for(const std::chrono::duration<Rep, Period>& arg) {
318     return globalAllPowerfulAssertingMutex.try_lock_upgrade_for(arg);
319   }
320
321   template <class Rep, class Period>
322   bool try_unlock_upgrade_and_lock_for(
323       const std::chrono::duration<Rep, Period>& arg) {
324     return globalAllPowerfulAssertingMutex.try_unlock_upgrade_and_lock_for(arg);
325   }
326
327   // reset state on destruction
328   ~FakeAllPowerfulAssertingMutex() {
329     globalAllPowerfulAssertingMutex = FakeAllPowerfulAssertingMutexInternal{};
330   }
331 };
332
333 // Single level of SYNCHRONIZED and UNSYNCHRONIZED, although nested test are
334 // super set of it, it is possible single level test passes while nested tests
335 // fail
336 TEST_F(SynchronizedLockTest, SyncUnSync) {
337   folly::Synchronized<std::vector<int>, FakeMutex> obj;
338   EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
339   SYNCHRONIZED(obj) {
340     EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
341     UNSYNCHRONIZED(obj) {
342       EXPECT_EQ((CountPair{1, 1}), FakeMutex::getLockUnlockCount());
343     }
344     EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
345   }
346   EXPECT_EQ((CountPair{2, 2}), FakeMutex::getLockUnlockCount());
347 }
348
349 // Nested SYNCHRONIZED UNSYNCHRONIZED test, 2 levels of synchronization
350 TEST_F(SynchronizedLockTest, NestedSyncUnSync) {
351   folly::Synchronized<std::vector<int>, FakeMutex> obj;
352   EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
353   SYNCHRONIZED(objCopy, obj) {
354     EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
355     SYNCHRONIZED(obj) {
356       EXPECT_EQ((CountPair{2, 0}), FakeMutex::getLockUnlockCount());
357       // Note: UNSYNCHRONIZED has always been kind of broken here.
358       // The input parameter is ignored (other than to overwrite what the input
359       // variable name refers to), and it unlocks the most object acquired in
360       // the most recent SYNCHRONIZED scope.
361       UNSYNCHRONIZED(obj) {
362         EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
363       }
364       EXPECT_EQ((CountPair{3, 1}), FakeMutex::getLockUnlockCount());
365       UNSYNCHRONIZED(obj) {
366         EXPECT_EQ((CountPair{3, 2}), FakeMutex::getLockUnlockCount());
367       }
368       EXPECT_EQ((CountPair{4, 2}), FakeMutex::getLockUnlockCount());
369     }
370     EXPECT_EQ((CountPair{4, 3}), FakeMutex::getLockUnlockCount());
371   }
372   EXPECT_EQ((CountPair{4, 4}), FakeMutex::getLockUnlockCount());
373 }
374
375 // Different nesting behavior, UNSYNCHRONIZED called on different depth of
376 // SYNCHRONIZED
377 TEST_F(SynchronizedLockTest, NestedSyncUnSync2) {
378   folly::Synchronized<std::vector<int>, FakeMutex> obj;
379   EXPECT_EQ((CountPair{0, 0}), FakeMutex::getLockUnlockCount());
380   SYNCHRONIZED(objCopy, obj) {
381     EXPECT_EQ((CountPair{1, 0}), FakeMutex::getLockUnlockCount());
382     SYNCHRONIZED(obj) {
383       EXPECT_EQ((CountPair{2, 0}), FakeMutex::getLockUnlockCount());
384       UNSYNCHRONIZED(obj) {
385         EXPECT_EQ((CountPair{2, 1}), FakeMutex::getLockUnlockCount());
386       }
387       EXPECT_EQ((CountPair{3, 1}), FakeMutex::getLockUnlockCount());
388     }
389     EXPECT_EQ((CountPair{3, 2}), FakeMutex::getLockUnlockCount());
390     UNSYNCHRONIZED(obj) {
391       EXPECT_EQ((CountPair{3, 3}), FakeMutex::getLockUnlockCount());
392     }
393     EXPECT_EQ((CountPair{4, 3}), FakeMutex::getLockUnlockCount());
394   }
395   EXPECT_EQ((CountPair{4, 4}), FakeMutex::getLockUnlockCount());
396 }
397
398 TEST_F(SynchronizedLockTest, UpgradableLocking) {
399   folly::Synchronized<int, FakeAllPowerfulAssertingMutex> sync;
400
401   // sanity assert
402   static_assert(
403       std::is_same<std::decay<decltype(*sync.ulock())>::type, int>::value,
404       "The ulock function was not well configured, blame aary@instagram.com");
405
406   {
407     auto ulock = sync.ulock();
408     EXPECT_EQ(
409         globalAllPowerfulAssertingMutex.lock_state,
410         FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UPGRADE);
411   }
412
413   // should be unlocked here
414   EXPECT_EQ(
415       globalAllPowerfulAssertingMutex.lock_state,
416       FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UNLOCKED);
417
418   // test going from upgrade to exclusive
419   {
420     auto ulock = sync.ulock();
421     auto wlock = ulock.moveFromUpgradeToWrite();
422     EXPECT_EQ(static_cast<bool>(ulock), false);
423     EXPECT_EQ(
424         globalAllPowerfulAssertingMutex.lock_state,
425         FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UNIQUE);
426   }
427
428   // should be unlocked here
429   EXPECT_EQ(
430       globalAllPowerfulAssertingMutex.lock_state,
431       FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UNLOCKED);
432
433   // test going from upgrade to shared
434   {
435     auto ulock = sync.ulock();
436     auto slock = ulock.moveFromUpgradeToRead();
437     EXPECT_EQ(static_cast<bool>(ulock), false);
438     EXPECT_EQ(
439         globalAllPowerfulAssertingMutex.lock_state,
440         FakeAllPowerfulAssertingMutexInternal::CurrentLockState::SHARED);
441   }
442
443   // should be unlocked here
444   EXPECT_EQ(
445       globalAllPowerfulAssertingMutex.lock_state,
446       FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UNLOCKED);
447
448   // test going from exclusive to upgrade
449   {
450     auto wlock = sync.wlock();
451     auto ulock = wlock.moveFromWriteToUpgrade();
452     EXPECT_EQ(static_cast<bool>(wlock), false);
453     EXPECT_EQ(
454         globalAllPowerfulAssertingMutex.lock_state,
455         FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UPGRADE);
456   }
457
458   // should be unlocked here
459   EXPECT_EQ(
460       globalAllPowerfulAssertingMutex.lock_state,
461       FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UNLOCKED);
462
463   // test going from exclusive to shared
464   {
465     auto wlock = sync.wlock();
466     auto slock = wlock.moveFromWriteToRead();
467     EXPECT_EQ(static_cast<bool>(wlock), false);
468     EXPECT_EQ(
469         globalAllPowerfulAssertingMutex.lock_state,
470         FakeAllPowerfulAssertingMutexInternal::CurrentLockState::SHARED);
471   }
472
473   // should be unlocked here
474   EXPECT_EQ(
475       globalAllPowerfulAssertingMutex.lock_state,
476       FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UNLOCKED);
477 }
478
479 TEST_F(SynchronizedLockTest, UpgradableLockingWithULock) {
480   folly::Synchronized<int, FakeAllPowerfulAssertingMutex> sync;
481
482   // sanity assert
483   static_assert(
484       std::is_same<std::decay<decltype(*sync.ulock())>::type, int>::value,
485       "The ulock function was not well configured, blame aary@instagram.com");
486
487   // test from upgrade to write
488   sync.withULockPtr([](auto ulock) {
489     EXPECT_EQ(static_cast<bool>(ulock), true);
490     EXPECT_EQ(
491         globalAllPowerfulAssertingMutex.lock_state,
492         FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UPGRADE);
493
494     auto wlock = ulock.moveFromUpgradeToWrite();
495     EXPECT_EQ(static_cast<bool>(ulock), false);
496     EXPECT_EQ(
497         globalAllPowerfulAssertingMutex.lock_state,
498         FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UNIQUE);
499   });
500
501   // should be unlocked here
502   EXPECT_EQ(
503       globalAllPowerfulAssertingMutex.lock_state,
504       FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UNLOCKED);
505
506   // test from write to upgrade
507   sync.withWLockPtr([](auto wlock) {
508     EXPECT_EQ(static_cast<bool>(wlock), true);
509     EXPECT_EQ(
510         globalAllPowerfulAssertingMutex.lock_state,
511         FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UNIQUE);
512
513     auto ulock = wlock.moveFromWriteToUpgrade();
514     EXPECT_EQ(static_cast<bool>(wlock), false);
515     EXPECT_EQ(
516         globalAllPowerfulAssertingMutex.lock_state,
517         FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UPGRADE);
518   });
519
520   // should be unlocked here
521   EXPECT_EQ(
522       globalAllPowerfulAssertingMutex.lock_state,
523       FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UNLOCKED);
524
525   // test from upgrade to shared
526   sync.withULockPtr([](auto ulock) {
527     EXPECT_EQ(static_cast<bool>(ulock), true);
528     EXPECT_EQ(
529         globalAllPowerfulAssertingMutex.lock_state,
530         FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UPGRADE);
531
532     auto slock = ulock.moveFromUpgradeToRead();
533     EXPECT_EQ(static_cast<bool>(ulock), false);
534     EXPECT_EQ(
535         globalAllPowerfulAssertingMutex.lock_state,
536         FakeAllPowerfulAssertingMutexInternal::CurrentLockState::SHARED);
537   });
538
539   // should be unlocked here
540   EXPECT_EQ(
541       globalAllPowerfulAssertingMutex.lock_state,
542       FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UNLOCKED);
543
544   // test from write to shared
545   sync.withWLockPtr([](auto wlock) {
546     EXPECT_EQ(static_cast<bool>(wlock), true);
547     EXPECT_EQ(
548         globalAllPowerfulAssertingMutex.lock_state,
549         FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UNIQUE);
550
551     auto slock = wlock.moveFromWriteToRead();
552     EXPECT_EQ(static_cast<bool>(wlock), false);
553     EXPECT_EQ(
554         globalAllPowerfulAssertingMutex.lock_state,
555         FakeAllPowerfulAssertingMutexInternal::CurrentLockState::SHARED);
556   });
557
558   // should be unlocked here
559   EXPECT_EQ(
560       globalAllPowerfulAssertingMutex.lock_state,
561       FakeAllPowerfulAssertingMutexInternal::CurrentLockState::UNLOCKED);
562 }