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
19 #include "src/core/lib/gprpp/ref_counted.h"
20
21 #include <gtest/gtest.h>
22
23 #include "src/core/lib/gprpp/memory.h"
24 #include "test/core/util/test_config.h"
25
26 namespace grpc_core {
27 namespace testing {
28 namespace {
29
30 class Foo : public RefCounted<Foo> {
31 public:
Foo()32 Foo() {}
33 };
34
TEST(RefCounted,Basic)35 TEST(RefCounted, Basic) {
36 Foo* foo = New<Foo>();
37 foo->Unref();
38 }
39
TEST(RefCounted,ExtraRef)40 TEST(RefCounted, ExtraRef) {
41 Foo* foo = New<Foo>();
42 RefCountedPtr<Foo> foop = foo->Ref();
43 foop.release();
44 foo->Unref();
45 foo->Unref();
46 }
47
48 // Note: We use DebugOnlyTraceFlag instead of TraceFlag to ensure that
49 // things build properly in both debug and non-debug cases.
50 DebugOnlyTraceFlag foo_tracer(true, "foo");
51
52 class FooWithTracing : public RefCountedWithTracing<FooWithTracing> {
53 public:
FooWithTracing()54 FooWithTracing() : RefCountedWithTracing(&foo_tracer) {}
55 };
56
TEST(RefCountedWithTracing,Basic)57 TEST(RefCountedWithTracing, Basic) {
58 FooWithTracing* foo = New<FooWithTracing>();
59 RefCountedPtr<FooWithTracing> foop = foo->Ref(DEBUG_LOCATION, "extra_ref");
60 foop.release();
61 foo->Unref(DEBUG_LOCATION, "extra_ref");
62 // Can use the no-argument methods, too.
63 foop = foo->Ref();
64 foop.release();
65 foo->Unref();
66 foo->Unref(DEBUG_LOCATION, "original_ref");
67 }
68
69 } // namespace
70 } // namespace testing
71 } // namespace grpc_core
72
main(int argc,char ** argv)73 int main(int argc, char** argv) {
74 grpc_test_init(argc, argv);
75 ::testing::InitGoogleTest(&argc, argv);
76 return RUN_ALL_TESTS();
77 }
78