cleanup_test.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright 2021 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/cleanup/cleanup.h"
  15. #include <functional>
  16. #include <type_traits>
  17. #include <utility>
  18. #include "gtest/gtest.h"
  19. #include "absl/base/config.h"
  20. #include "absl/utility/utility.h"
  21. namespace {
  22. using Tag = absl::cleanup_internal::Tag;
  23. template <typename Type1, typename Type2>
  24. constexpr bool IsSame() {
  25. return (std::is_same<Type1, Type2>::value);
  26. }
  27. struct IdentityFactory {
  28. template <typename Callback>
  29. static Callback AsCallback(Callback callback) {
  30. return Callback(std::move(callback));
  31. }
  32. };
  33. // `FunctorClass` is a type used for testing `absl::Cleanup`. It is intended to
  34. // represent users that make their own move-only callback types outside of
  35. // `std::function` and lambda literals.
  36. class FunctorClass {
  37. using Callback = std::function<void()>;
  38. public:
  39. explicit FunctorClass(Callback callback) : callback_(std::move(callback)) {}
  40. FunctorClass(FunctorClass&& other)
  41. : callback_(absl::exchange(other.callback_, Callback())) {}
  42. FunctorClass(const FunctorClass&) = delete;
  43. FunctorClass& operator=(const FunctorClass&) = delete;
  44. FunctorClass& operator=(FunctorClass&&) = delete;
  45. void operator()() const& = delete;
  46. void operator()() && {
  47. ASSERT_TRUE(callback_);
  48. callback_();
  49. callback_ = nullptr;
  50. }
  51. private:
  52. Callback callback_;
  53. };
  54. struct FunctorClassFactory {
  55. template <typename Callback>
  56. static FunctorClass AsCallback(Callback callback) {
  57. return FunctorClass(std::move(callback));
  58. }
  59. };
  60. struct StdFunctionFactory {
  61. template <typename Callback>
  62. static std::function<void()> AsCallback(Callback callback) {
  63. return std::function<void()>(std::move(callback));
  64. }
  65. };
  66. using CleanupTestParams =
  67. ::testing::Types<IdentityFactory, FunctorClassFactory, StdFunctionFactory>;
  68. template <typename>
  69. struct CleanupTest : public ::testing::Test {};
  70. TYPED_TEST_SUITE(CleanupTest, CleanupTestParams);
  71. bool fn_ptr_called = false;
  72. void FnPtrFunction() { fn_ptr_called = true; }
  73. TYPED_TEST(CleanupTest, FactoryProducesCorrectType) {
  74. {
  75. auto callback = TypeParam::AsCallback([] {});
  76. auto cleanup = absl::MakeCleanup(std::move(callback));
  77. static_assert(
  78. IsSame<absl::Cleanup<Tag, decltype(callback)>, decltype(cleanup)>(),
  79. "");
  80. }
  81. {
  82. auto cleanup = absl::MakeCleanup(&FnPtrFunction);
  83. static_assert(IsSame<absl::Cleanup<Tag, void (*)()>, decltype(cleanup)>(),
  84. "");
  85. }
  86. {
  87. auto cleanup = absl::MakeCleanup(FnPtrFunction);
  88. static_assert(IsSame<absl::Cleanup<Tag, void (*)()>, decltype(cleanup)>(),
  89. "");
  90. }
  91. }
  92. #if defined(ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION)
  93. TYPED_TEST(CleanupTest, CTADProducesCorrectType) {
  94. {
  95. auto callback = TypeParam::AsCallback([] {});
  96. absl::Cleanup cleanup = std::move(callback);
  97. static_assert(
  98. IsSame<absl::Cleanup<Tag, decltype(callback)>, decltype(cleanup)>(),
  99. "");
  100. }
  101. {
  102. absl::Cleanup cleanup = &FnPtrFunction;
  103. static_assert(IsSame<absl::Cleanup<Tag, void (*)()>, decltype(cleanup)>(),
  104. "");
  105. }
  106. {
  107. absl::Cleanup cleanup = FnPtrFunction;
  108. static_assert(IsSame<absl::Cleanup<Tag, void (*)()>, decltype(cleanup)>(),
  109. "");
  110. }
  111. }
  112. TYPED_TEST(CleanupTest, FactoryAndCTADProduceSameType) {
  113. {
  114. auto callback = IdentityFactory::AsCallback([] {});
  115. auto factory_cleanup = absl::MakeCleanup(callback);
  116. absl::Cleanup deduction_cleanup = callback;
  117. static_assert(
  118. IsSame<decltype(factory_cleanup), decltype(deduction_cleanup)>(), "");
  119. }
  120. {
  121. auto factory_cleanup =
  122. absl::MakeCleanup(FunctorClassFactory::AsCallback([] {}));
  123. absl::Cleanup deduction_cleanup = FunctorClassFactory::AsCallback([] {});
  124. static_assert(
  125. IsSame<decltype(factory_cleanup), decltype(deduction_cleanup)>(), "");
  126. }
  127. {
  128. auto factory_cleanup =
  129. absl::MakeCleanup(StdFunctionFactory::AsCallback([] {}));
  130. absl::Cleanup deduction_cleanup = StdFunctionFactory::AsCallback([] {});
  131. static_assert(
  132. IsSame<decltype(factory_cleanup), decltype(deduction_cleanup)>(), "");
  133. }
  134. {
  135. auto factory_cleanup = absl::MakeCleanup(&FnPtrFunction);
  136. absl::Cleanup deduction_cleanup = &FnPtrFunction;
  137. static_assert(
  138. IsSame<decltype(factory_cleanup), decltype(deduction_cleanup)>(), "");
  139. }
  140. {
  141. auto factory_cleanup = absl::MakeCleanup(FnPtrFunction);
  142. absl::Cleanup deduction_cleanup = FnPtrFunction;
  143. static_assert(
  144. IsSame<decltype(factory_cleanup), decltype(deduction_cleanup)>(), "");
  145. }
  146. }
  147. #endif // defined(ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION)
  148. TYPED_TEST(CleanupTest, BasicUsage) {
  149. bool called = false;
  150. {
  151. auto cleanup =
  152. absl::MakeCleanup(TypeParam::AsCallback([&called] { called = true; }));
  153. EXPECT_FALSE(called); // Constructor shouldn't invoke the callback
  154. }
  155. EXPECT_TRUE(called); // Destructor should invoke the callback
  156. }
  157. TYPED_TEST(CleanupTest, BasicUsageWithFunctionPointer) {
  158. fn_ptr_called = false;
  159. {
  160. auto cleanup = absl::MakeCleanup(TypeParam::AsCallback(&FnPtrFunction));
  161. EXPECT_FALSE(fn_ptr_called); // Constructor shouldn't invoke the callback
  162. }
  163. EXPECT_TRUE(fn_ptr_called); // Destructor should invoke the callback
  164. }
  165. TYPED_TEST(CleanupTest, Cancel) {
  166. bool called = false;
  167. {
  168. auto cleanup =
  169. absl::MakeCleanup(TypeParam::AsCallback([&called] { called = true; }));
  170. EXPECT_FALSE(called); // Constructor shouldn't invoke the callback
  171. std::move(cleanup).Cancel();
  172. EXPECT_FALSE(called); // Cancel shouldn't invoke the callback
  173. }
  174. EXPECT_FALSE(called); // Destructor shouldn't invoke the callback
  175. }
  176. TYPED_TEST(CleanupTest, Invoke) {
  177. bool called = false;
  178. {
  179. auto cleanup =
  180. absl::MakeCleanup(TypeParam::AsCallback([&called] { called = true; }));
  181. EXPECT_FALSE(called); // Constructor shouldn't invoke the callback
  182. std::move(cleanup).Invoke();
  183. EXPECT_TRUE(called); // Invoke should invoke the callback
  184. called = false; // Reset tracker before destructor runs
  185. }
  186. EXPECT_FALSE(called); // Destructor shouldn't invoke the callback
  187. }
  188. TYPED_TEST(CleanupTest, Move) {
  189. bool called = false;
  190. {
  191. auto moved_from_cleanup =
  192. absl::MakeCleanup(TypeParam::AsCallback([&called] { called = true; }));
  193. EXPECT_FALSE(called); // Constructor shouldn't invoke the callback
  194. {
  195. auto moved_to_cleanup = std::move(moved_from_cleanup);
  196. EXPECT_FALSE(called); // Move shouldn't invoke the callback
  197. }
  198. EXPECT_TRUE(called); // Destructor should invoke the callback
  199. called = false; // Reset tracker before destructor runs
  200. }
  201. EXPECT_FALSE(called); // Destructor shouldn't invoke the callback
  202. }
  203. int DestructionCount = 0;
  204. struct DestructionCounter {
  205. void operator()() {}
  206. ~DestructionCounter() { ++DestructionCount; }
  207. };
  208. TYPED_TEST(CleanupTest, DestructorDestroys) {
  209. {
  210. auto cleanup =
  211. absl::MakeCleanup(TypeParam::AsCallback(DestructionCounter()));
  212. DestructionCount = 0;
  213. }
  214. EXPECT_EQ(DestructionCount, 1); // Engaged cleanup destroys
  215. }
  216. TYPED_TEST(CleanupTest, CancelDestroys) {
  217. {
  218. auto cleanup =
  219. absl::MakeCleanup(TypeParam::AsCallback(DestructionCounter()));
  220. DestructionCount = 0;
  221. std::move(cleanup).Cancel();
  222. EXPECT_EQ(DestructionCount, 1); // Cancel destroys
  223. }
  224. EXPECT_EQ(DestructionCount, 1); // Canceled cleanup does not double destroy
  225. }
  226. TYPED_TEST(CleanupTest, InvokeDestroys) {
  227. {
  228. auto cleanup =
  229. absl::MakeCleanup(TypeParam::AsCallback(DestructionCounter()));
  230. DestructionCount = 0;
  231. std::move(cleanup).Invoke();
  232. EXPECT_EQ(DestructionCount, 1); // Invoke destroys
  233. }
  234. EXPECT_EQ(DestructionCount, 1); // Invoked cleanup does not double destroy
  235. }
  236. } // namespace