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