orphanable_test.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "src/core/lib/gprpp/orphanable.h"
  19. #include <gtest/gtest.h>
  20. #include "src/core/lib/gprpp/memory.h"
  21. #include "test/core/util/test_config.h"
  22. namespace grpc_core {
  23. namespace testing {
  24. namespace {
  25. class Foo : public Orphanable {
  26. public:
  27. Foo() : Foo(0) {}
  28. explicit Foo(int value) : value_(value) {}
  29. void Orphan() override { delete this; }
  30. int value() const { return value_; }
  31. private:
  32. int value_;
  33. };
  34. TEST(Orphanable, Basic) {
  35. Foo* foo = new Foo();
  36. foo->Orphan();
  37. }
  38. TEST(OrphanablePtr, Basic) {
  39. OrphanablePtr<Foo> foo(new Foo());
  40. EXPECT_EQ(0, foo->value());
  41. }
  42. TEST(MakeOrphanable, DefaultConstructor) {
  43. auto foo = MakeOrphanable<Foo>();
  44. EXPECT_EQ(0, foo->value());
  45. }
  46. TEST(MakeOrphanable, WithParameters) {
  47. auto foo = MakeOrphanable<Foo>(5);
  48. EXPECT_EQ(5, foo->value());
  49. }
  50. class Bar : public InternallyRefCounted<Bar> {
  51. public:
  52. Bar() : Bar(0) {}
  53. explicit Bar(int value) : value_(value) {}
  54. void Orphan() override { Unref(); }
  55. int value() const { return value_; }
  56. void StartWork() { self_ref_ = Ref(); }
  57. void FinishWork() { self_ref_.reset(); }
  58. private:
  59. int value_;
  60. RefCountedPtr<Bar> self_ref_;
  61. };
  62. TEST(OrphanablePtr, InternallyRefCounted) {
  63. auto bar = MakeOrphanable<Bar>();
  64. bar->StartWork();
  65. bar->FinishWork();
  66. }
  67. class Baz : public InternallyRefCounted<Baz> {
  68. public:
  69. Baz() : Baz(0) {}
  70. explicit Baz(int value) : InternallyRefCounted<Baz>("Baz"), value_(value) {}
  71. void Orphan() override { Unref(); }
  72. int value() const { return value_; }
  73. void StartWork() { self_ref_ = Ref(DEBUG_LOCATION, "work"); }
  74. void FinishWork() {
  75. // This is a little ugly, but it makes the logged ref and unref match up.
  76. self_ref_.release();
  77. Unref(DEBUG_LOCATION, "work");
  78. }
  79. private:
  80. int value_;
  81. RefCountedPtr<Baz> self_ref_;
  82. };
  83. TEST(OrphanablePtr, InternallyRefCountedWithTracing) {
  84. auto baz = MakeOrphanable<Baz>();
  85. baz->StartWork();
  86. baz->FinishWork();
  87. }
  88. } // namespace
  89. } // namespace testing
  90. } // namespace grpc_core
  91. int main(int argc, char** argv) {
  92. grpc::testing::TestEnvironment env(argc, argv);
  93. ::testing::InitGoogleTest(&argc, argv);
  94. return RUN_ALL_TESTS();
  95. }