SynchronizedPtrTest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2017-present 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. #include <folly/SynchronizedPtr.h>
  17. #include <folly/Optional.h>
  18. #include <folly/Replaceable.h>
  19. #include <folly/portability/GTest.h>
  20. #include <folly/synchronization/RWSpinLock.h>
  21. template <typename SPtr>
  22. void basics(SPtr& sptr) {
  23. EXPECT_TRUE((std::is_same<int const&, decltype(*sptr.rlock())>::value));
  24. auto initialValue = *sptr.rlock();
  25. bool rlockedTypeOK{false};
  26. sptr.withRLock([&](auto&& value) {
  27. rlockedTypeOK = std::is_same<int const&, decltype(value)>::value;
  28. });
  29. EXPECT_TRUE(rlockedTypeOK);
  30. EXPECT_TRUE((std::is_same<int&, decltype(*sptr.wlock())>::value));
  31. bool wlockedTypeOK{false};
  32. sptr.withWLock([&](auto&& value) {
  33. wlockedTypeOK = std::is_same<int&, decltype(value)>::value;
  34. ++value;
  35. });
  36. EXPECT_TRUE(wlockedTypeOK);
  37. EXPECT_EQ(initialValue + 1, *sptr.rlock());
  38. }
  39. TEST(SynchronizedPtrTest, Shared) {
  40. folly::SynchronizedPtr<std::shared_ptr<int>> pInt{std::make_shared<int>(0)};
  41. basics(pInt);
  42. }
  43. TEST(SynchronizedPtrTest, UniqueBasic) {
  44. folly::SynchronizedPtr<std::unique_ptr<int>> pInt{std::make_unique<int>(0)};
  45. basics(pInt);
  46. }
  47. TEST(SynchronizedPtrTest, UniqueDeleter) {
  48. bool calledDeleter = false;
  49. auto x = [&](int* ptr) {
  50. delete ptr;
  51. calledDeleter = true;
  52. };
  53. {
  54. folly::SynchronizedPtr<std::unique_ptr<int, decltype(x)>> pInt{
  55. std::unique_ptr<int, decltype(x)>(new int(0), x)};
  56. basics(pInt);
  57. EXPECT_TRUE((std::is_same<
  58. std::unique_ptr<int, decltype(x)>&,
  59. decltype(*pInt.wlockPointer())>::value));
  60. pInt.wlockPointer()->reset(new int(5));
  61. EXPECT_TRUE(calledDeleter);
  62. calledDeleter = false;
  63. }
  64. EXPECT_TRUE(calledDeleter);
  65. }
  66. TEST(SynchronizedPtrTest, Replaceable) {
  67. folly::SynchronizedPtr<folly::Replaceable<int>> pInt{0};
  68. folly::SynchronizedPtr<folly::Replaceable<int const>> pcInt{2};
  69. basics(pInt);
  70. EXPECT_TRUE(
  71. (std::is_same<folly::Replaceable<int>&, decltype(*pInt.wlockPointer())>::
  72. value));
  73. EXPECT_TRUE((std::is_same<
  74. folly::Replaceable<int const>&,
  75. decltype(*pcInt.wlockPointer())>::value));
  76. pcInt.withWLockPointer([](auto&& ptr) {
  77. EXPECT_TRUE(
  78. (std::is_same<folly::Replaceable<int const>&, decltype(ptr)>::value));
  79. ptr.emplace(4);
  80. });
  81. EXPECT_EQ(4, *pcInt.rlock());
  82. }
  83. TEST(SynchronizedPtrTest, Optional) {
  84. folly::SynchronizedPtr<folly::Optional<int>, folly::RWSpinLock> pInt{0};
  85. basics(pInt);
  86. EXPECT_TRUE(
  87. (std::is_same<folly::Optional<int>&, decltype(*pInt.wlockPointer())>::
  88. value));
  89. EXPECT_TRUE(static_cast<bool>(pInt.rlock()));
  90. pInt.withWLockPointer([](auto&& ptr) {
  91. EXPECT_TRUE((std::is_same<folly::Optional<int>&, decltype(ptr)>::value));
  92. ptr.clear();
  93. });
  94. EXPECT_FALSE(static_cast<bool>(pInt.rlock()));
  95. }
  96. TEST(SynchronizedPtrTest, Virtual) {
  97. struct A {
  98. virtual void poke(bool&) const {}
  99. virtual ~A() = default;
  100. };
  101. struct B : A {
  102. void poke(bool& b) const override {
  103. b = true;
  104. }
  105. };
  106. folly::SynchronizedPtr<A*> pA{new B()};
  107. bool itWorks = false;
  108. pA.rlock()->poke(itWorks);
  109. EXPECT_TRUE(itWorks);
  110. itWorks = false;
  111. pA.wlock()->poke(itWorks);
  112. EXPECT_TRUE(itWorks);
  113. pA.withWLockPointer([](auto&& ptr) {
  114. EXPECT_TRUE((std::is_same<A*&, decltype(ptr)>::value));
  115. delete ptr;
  116. ptr = new B();
  117. });
  118. {
  119. auto lockedPtr = pA.wlockPointer();
  120. EXPECT_TRUE((std::is_same<A*&, decltype(*lockedPtr)>::value));
  121. delete *lockedPtr;
  122. *lockedPtr = new B();
  123. }
  124. itWorks = false;
  125. pA.wlock()->poke(itWorks);
  126. EXPECT_TRUE(itWorks);
  127. delete *pA.wlockPointer();
  128. }