dual_ref_counted_test.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // Copyright 2020 gRPC authors.
  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 "src/core/lib/gprpp/dual_ref_counted.h"
  17. #include <set>
  18. #include <gmock/gmock.h>
  19. #include <gtest/gtest.h>
  20. #include "test/core/util/test_config.h"
  21. namespace grpc_core {
  22. namespace testing {
  23. namespace {
  24. class Foo : public DualRefCounted<Foo> {
  25. public:
  26. Foo() = default;
  27. ~Foo() override { GPR_ASSERT(shutting_down_); }
  28. void Orphan() override { shutting_down_ = true; }
  29. private:
  30. bool shutting_down_ = false;
  31. };
  32. TEST(DualRefCounted, Basic) {
  33. Foo* foo = new Foo();
  34. foo->Unref();
  35. }
  36. TEST(DualRefCounted, ExtraRef) {
  37. Foo* foo = new Foo();
  38. foo->Ref().release();
  39. foo->Unref();
  40. foo->Unref();
  41. }
  42. TEST(DualRefCounted, ExtraWeakRef) {
  43. Foo* foo = new Foo();
  44. foo->WeakRef().release();
  45. foo->Unref();
  46. foo->WeakUnref();
  47. }
  48. TEST(DualRefCounted, RefIfNonZero) {
  49. Foo* foo = new Foo();
  50. foo->WeakRef().release();
  51. {
  52. RefCountedPtr<Foo> foop = foo->RefIfNonZero();
  53. EXPECT_NE(foop.get(), nullptr);
  54. }
  55. foo->Unref();
  56. {
  57. RefCountedPtr<Foo> foop = foo->RefIfNonZero();
  58. EXPECT_EQ(foop.get(), nullptr);
  59. }
  60. foo->WeakUnref();
  61. }
  62. class FooWithTracing : public DualRefCounted<FooWithTracing> {
  63. public:
  64. FooWithTracing() : DualRefCounted("FooWithTracing") {}
  65. ~FooWithTracing() override { GPR_ASSERT(shutting_down_); }
  66. void Orphan() override { shutting_down_ = true; }
  67. private:
  68. bool shutting_down_ = false;
  69. };
  70. TEST(DualRefCountedWithTracing, Basic) {
  71. FooWithTracing* foo = new FooWithTracing();
  72. foo->Ref(DEBUG_LOCATION, "extra_ref").release();
  73. foo->Unref(DEBUG_LOCATION, "extra_ref");
  74. foo->WeakRef(DEBUG_LOCATION, "extra_ref").release();
  75. foo->WeakUnref(DEBUG_LOCATION, "extra_ref");
  76. // Can use the no-argument methods, too.
  77. foo->Ref().release();
  78. foo->Unref();
  79. foo->WeakRef().release();
  80. foo->WeakUnref();
  81. foo->Unref(DEBUG_LOCATION, "original_ref");
  82. }
  83. } // namespace
  84. } // namespace testing
  85. } // namespace grpc_core
  86. int main(int argc, char** argv) {
  87. grpc::testing::TestEnvironment env(argc, argv);
  88. ::testing::InitGoogleTest(&argc, argv);
  89. return RUN_ALL_TESTS();
  90. }