orphanable_test.cc
Go to the documentation of this file.
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 
20 
21 #include <gtest/gtest.h>
22 
25 
26 namespace grpc_core {
27 namespace testing {
28 namespace {
29 
30 class Foo : public Orphanable {
31  public:
32  Foo() : Foo(0) {}
33  explicit Foo(int value) : value_(value) {}
34  void Orphan() override { delete this; }
35  int value() const { return value_; }
36 
37  private:
38  int value_;
39 };
40 
41 TEST(Orphanable, Basic) {
42  Foo* foo = new Foo();
43  foo->Orphan();
44 }
45 
46 TEST(OrphanablePtr, Basic) {
47  OrphanablePtr<Foo> foo(new Foo());
48  EXPECT_EQ(0, foo->value());
49 }
50 
51 TEST(MakeOrphanable, DefaultConstructor) {
52  auto foo = MakeOrphanable<Foo>();
53  EXPECT_EQ(0, foo->value());
54 }
55 
56 TEST(MakeOrphanable, WithParameters) {
57  auto foo = MakeOrphanable<Foo>(5);
58  EXPECT_EQ(5, foo->value());
59 }
60 
61 class Bar : public InternallyRefCounted<Bar> {
62  public:
63  Bar() : Bar(0) {}
64  explicit Bar(int value) : value_(value) {}
65  void Orphan() override { Unref(); }
66  int value() const { return value_; }
67 
68  void StartWork() { self_ref_ = Ref(); }
69  void FinishWork() { self_ref_.reset(); }
70 
71  private:
72  int value_;
73  RefCountedPtr<Bar> self_ref_;
74 };
75 
76 TEST(OrphanablePtr, InternallyRefCounted) {
77  auto bar = MakeOrphanable<Bar>();
78  bar->StartWork();
79  bar->FinishWork();
80 }
81 
82 class Baz : public InternallyRefCounted<Baz> {
83  public:
84  Baz() : Baz(0) {}
85  explicit Baz(int value) : InternallyRefCounted<Baz>("Baz"), value_(value) {}
86  void Orphan() override { Unref(); }
87  int value() const { return value_; }
88 
89  void StartWork() { self_ref_ = Ref(DEBUG_LOCATION, "work"); }
90  void FinishWork() {
91  // This is a little ugly, but it makes the logged ref and unref match up.
92  self_ref_.release();
93  Unref(DEBUG_LOCATION, "work");
94  }
95 
96  private:
97  int value_;
98  RefCountedPtr<Baz> self_ref_;
99 };
100 
101 TEST(OrphanablePtr, InternallyRefCountedWithTracing) {
102  auto baz = MakeOrphanable<Baz>();
103  baz->StartWork();
104  baz->FinishWork();
105 }
106 
107 } // namespace
108 } // namespace testing
109 } // namespace grpc_core
110 
111 int main(int argc, char** argv) {
112  grpc::testing::TestEnvironment env(&argc, argv);
113  ::testing::InitGoogleTest(&argc, argv);
114  return RUN_ALL_TESTS();
115 }
testing
Definition: aws_request_signer_test.cc:25
orphanable.h
generate.env
env
Definition: generate.py:37
grpc_core
Definition: call_metric_recorder.h:31
bar
Definition: bloaty/third_party/googletest/googletest/test/googletest-output-test_.cc:562
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
memory.h
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
Foo
Definition: abseil-cpp/absl/debugging/symbolize_test.cc:65
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
value_
int value_
Definition: orphanable_test.cc:38
test_config.h
value
const char * value
Definition: hpack_parser_table.cc:165
foo
int foo
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/statusor_test.cc:66
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_core::OrphanablePtr
std::unique_ptr< T, Deleter > OrphanablePtr
Definition: orphanable.h:64
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_core::MakeOrphanable
OrphanablePtr< T > MakeOrphanable(Args &&... args)
Definition: orphanable.h:67
grpc_core::testing::TEST
TEST(ServiceConfigParserTest, DoubleRegistration)
Definition: service_config_test.cc:448
self_ref_
RefCountedPtr< Bar > self_ref_
Definition: orphanable_test.cc:73
main
int main(int argc, char **argv)
Definition: orphanable_test.cc:111
testing::Ref
internal::RefMatcher< T & > Ref(T &x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8628


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:36