1 /* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
22 */
23 
24 #include "locks/shared_spin_lock.h"
25 
26 #include <gmock/gmock.h>
27 #include <gtest/gtest.h>
28 
29 namespace lock
30 {
31 namespace unittests
32 {
33 class Shared_spin_lock_test : public ::testing::Test
34 {
35  protected:
Shared_spin_lock_test()36   Shared_spin_lock_test() {}
SetUp()37   virtual void SetUp() {}
TearDown()38   virtual void TearDown() {}
39 };
40 
TEST_F(Shared_spin_lock_test,Lock_unlock_test)41 TEST_F(Shared_spin_lock_test, Lock_unlock_test)
42 {
43   lock::Shared_spin_lock lock1;
44   lock::Shared_spin_lock lock2;
45 
46   EXPECT_EQ(lock1.acquire_exclusive().is_exclusive_acquisition(),
47             true);  // Successfully acquired in exclusive mode
48   EXPECT_EQ(lock2.acquire_shared().is_shared_acquisition(),
49             true);  // Successfully acquired in shared mode
50 
51   EXPECT_EQ(lock1.try_shared().is_shared_acquisition(),
52             false);  // Trying to acquire shared access fails
53   EXPECT_EQ(lock2.try_exclusive().is_exclusive_acquisition(),
54             false);  // Trying to acquire exclusive access fails
55   EXPECT_EQ(lock2.try_shared().is_shared_acquisition(),
56             true);  // Trying to acquire shared access succeeds
57 
58   EXPECT_EQ(lock1.release_exclusive().is_exclusive_acquisition(),
59             false);  // Release and test that acquisition isn't kept
60   EXPECT_EQ(lock2.release_shared().release_shared().is_shared_acquisition(),
61             false);  // Release and test that acquisition isn't kept
62 }
63 
TEST_F(Shared_spin_lock_test,Sentry_class_test)64 TEST_F(Shared_spin_lock_test, Sentry_class_test)
65 {
66   lock::Shared_spin_lock lock1;
67 
68   {
69     lock::Shared_spin_lock::Guard sentry(
70         lock1, lock::Shared_spin_lock::SL_EXCLUSIVE, true /* try and exit*/);
71     EXPECT_EQ(sentry->is_exclusive_acquisition(),
72               true);  // Successfully acquired in exclusive mode
73   }
74   EXPECT_EQ(
75       lock1.is_exclusive_acquisition(),
76       false);  // Successfully released the locked upon exiting the code block
77   {
78     lock::Shared_spin_lock::Guard sentry2(
79         lock1, lock::Shared_spin_lock::SL_NO_ACQUISITION);
80     sentry2.acquire(lock::Shared_spin_lock::SL_SHARED, true /* try and exit*/
81     );
82     EXPECT_EQ((*sentry2).is_shared_acquisition(),
83               true);  // Successfully acquired in shared mode
84   }
85   EXPECT_EQ(
86       lock1.is_shared_acquisition(),
87       false);  // Successfully released the locked upon exiting the code block
88 }  // namespace unittests
89 
90 }  // namespace unittests
91 }  // namespace lock
92