user_lock_test.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include <hdfspp/locks.h>
  19. #include <gmock/gmock.h>
  20. #include <gtest/gtest.h>
  21. #include <iostream>
  22. #include <memory>
  23. #include <mutex>
  24. #include <thread>
  25. #include <vector>
  26. using namespace hdfs;
  27. // try_lock will always return false, unlock will always throw because it
  28. // can never be locked.
  29. class CantLockMutex : public Mutex {
  30. public:
  31. void lock() override {
  32. throw LockFailure("This mutex cannot be locked");
  33. }
  34. void unlock() override {
  35. throw LockFailure("Unlock");
  36. }
  37. std::string str() override {
  38. return "CantLockMutex";
  39. }
  40. };
  41. TEST(UserLockTest, DefaultMutexBasics) {
  42. Mutex *mtx = LockManager::TEST_get_default_mutex();
  43. // lock and unlock twice to make sure unlock works
  44. bool locked = false;
  45. try {
  46. mtx->lock();
  47. locked = true;
  48. } catch (...) {}
  49. EXPECT_TRUE(locked);
  50. mtx->unlock();
  51. locked = false;
  52. try {
  53. mtx->lock();
  54. locked = true;
  55. } catch (...) {}
  56. EXPECT_TRUE(locked);
  57. mtx->unlock();
  58. EXPECT_EQ(mtx->str(), "DefaultMutex");
  59. }
  60. // Make sure lock manager can only be initialized once unless test reset called
  61. TEST(UserLockTest, LockManager) {
  62. std::unique_ptr<CantLockMutex> mtx(new CantLockMutex());
  63. EXPECT_TRUE(mtx != nullptr);
  64. // Check the default lock
  65. Mutex *defaultGssapiMtx = LockManager::getGssapiMutex();
  66. EXPECT_TRUE(defaultGssapiMtx != nullptr);
  67. // Try a double init. Should not work
  68. bool res = LockManager::InitLocks(mtx.get());
  69. EXPECT_TRUE(res);
  70. // Check pointer value
  71. EXPECT_EQ(LockManager::getGssapiMutex(), mtx.get());
  72. res = LockManager::InitLocks(mtx.get());
  73. EXPECT_FALSE(res);
  74. // Make sure test reset still works
  75. LockManager::TEST_reset_manager();
  76. res = LockManager::InitLocks(mtx.get());
  77. EXPECT_TRUE(res);
  78. LockManager::TEST_reset_manager();
  79. EXPECT_EQ(LockManager::getGssapiMutex(), defaultGssapiMtx);
  80. }
  81. TEST(UserLockTest, CheckCantLockMutex) {
  82. std::unique_ptr<CantLockMutex> mtx(new CantLockMutex());
  83. EXPECT_TRUE(mtx != nullptr);
  84. bool locked = false;
  85. try {
  86. mtx->lock();
  87. } catch (...) {}
  88. EXPECT_FALSE(locked);
  89. bool threw_on_unlock = false;
  90. try {
  91. mtx->unlock();
  92. } catch (const LockFailure& e) {
  93. threw_on_unlock = true;
  94. }
  95. EXPECT_TRUE(threw_on_unlock);
  96. EXPECT_EQ("CantLockMutex", mtx->str());
  97. }
  98. TEST(UserLockTest, LockGuardBasics) {
  99. Mutex *goodMtx = LockManager::TEST_get_default_mutex();
  100. CantLockMutex badMtx;
  101. // lock/unlock a few times to increase chances of UB if lock is misused
  102. for(int i=0;i<10;i++) {
  103. bool caught_exception = false;
  104. try {
  105. LockGuard guard(goodMtx);
  106. // now have a scoped lock
  107. } catch (const LockFailure& e) {
  108. caught_exception = true;
  109. }
  110. EXPECT_FALSE(caught_exception);
  111. }
  112. // still do a few times, but expect it to blow up each time
  113. for(int i=0;i<10;i++) {
  114. bool caught_exception = false;
  115. try {
  116. LockGuard guard(&badMtx);
  117. // now have a scoped lock
  118. } catch (const LockFailure& e) {
  119. caught_exception = true;
  120. }
  121. EXPECT_TRUE(caught_exception);
  122. }
  123. }
  124. struct Incrementer {
  125. int64_t& _val;
  126. int64_t _iters;
  127. Mutex *_mtx;
  128. Incrementer(int64_t &val, int64_t iters, Mutex *m)
  129. : _val(val), _iters(iters), _mtx(m) {}
  130. void operator()(){
  131. for(int64_t i=0; i<_iters; i++) {
  132. LockGuard valguard(_mtx);
  133. _val += 1;
  134. }
  135. }
  136. };
  137. struct Decrementer {
  138. int64_t& _val;
  139. int64_t _iters;
  140. Mutex *_mtx;
  141. Decrementer(int64_t &val, int64_t iters, Mutex *m)
  142. : _val(val), _iters(iters), _mtx(m) {}
  143. void operator()(){
  144. for(int64_t i=0; i<_iters; i++) {
  145. LockGuard valguard(_mtx);
  146. _val -= 1;
  147. }
  148. }
  149. };
  150. TEST(UserLockTest, LockGuardConcurrency) {
  151. Mutex *mtx = LockManager::TEST_get_default_mutex();
  152. // Prove that these actually mutate the value
  153. int64_t test_value = 0;
  154. Incrementer inc(test_value, 1000, mtx);
  155. inc();
  156. EXPECT_EQ(test_value, 1000);
  157. Decrementer dec(test_value, 1000, mtx);
  158. dec();
  159. EXPECT_EQ(test_value, 0);
  160. std::vector<std::thread> workers;
  161. std::vector<Incrementer> incrementers;
  162. std::vector<Decrementer> decrementors;
  163. const int delta = 1024 * 1024;
  164. const int threads = 2 * 6;
  165. EXPECT_EQ(threads % 2, 0);
  166. // a bunch of threads race to increment and decrement the value
  167. // if all goes well the operations balance out and the value is unchanged
  168. for(int i=0; i < threads; i++) {
  169. if(i%2 == 0) {
  170. incrementers.emplace_back(test_value, delta, mtx);
  171. workers.emplace_back(incrementers.back());
  172. } else {
  173. decrementors.emplace_back(test_value, delta, mtx);
  174. workers.emplace_back(decrementors.back());
  175. }
  176. }
  177. // join, everything should balance to 0
  178. for(std::thread& thread : workers) {
  179. thread.join();
  180. }
  181. EXPECT_EQ(test_value, 0);
  182. }
  183. int main(int argc, char *argv[]) {
  184. // The following line must be executed to initialize Google Mock
  185. // (and Google Test) before running the tests.
  186. ::testing::InitGoogleMock(&argc, argv);
  187. int res = RUN_ALL_TESTS();
  188. return res;
  189. }