dual_ref_counted_test.cc
Go to the documentation of this file.
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 
18 
19 #include <set>
20 
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 
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 }
testing
Definition: aws_request_signer_test.cc:25
generate.env
env
Definition: generate.py:37
grpc_core
Definition: call_metric_recorder.h:31
main
int main(int argc, char **argv)
Definition: dual_ref_counted_test.cc:104
foo
Definition: bloaty/third_party/googletest/googletest/test/googletest-output-test_.cc:546
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
Foo
Definition: abseil-cpp/absl/debugging/symbolize_test.cc:65
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
shutting_down_
bool shutting_down_
Definition: dual_ref_counted_test.cc:38
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
test_config.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
dual_ref_counted.h
grpc_core::testing::TEST
TEST(ServiceConfigParserTest, DoubleRegistration)
Definition: service_config_test.cc:448


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:17