| 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 <memory> |
| 22 |
#include <new> |
| 23 |
#include <set> |
| 24 |
#include <type_traits> |
| 25 |
|
| 26 |
#include "gmock/gmock.h" |
| 27 |
#include "gtest/gtest.h" |
| 28 |
|
| 29 |
#include "test/core/util/test_config.h" |
| 30 |
|
| 31 |
namespace grpc_core { |
| 32 |
namespace testing { |
| 33 |
namespace { |
| 34 |
|
| 35 |
class Foo : public RefCounted<Foo> { |
| 36 |
public: |
| 37 |
Foo() { |
| 38 |
static_assert(std::has_virtual_destructor<Foo>::value, |
| 39 |
"PolymorphicRefCount doesn't have a virtual dtor"); |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
TEST(RefCounted, Basic) { |
| 44 |
Foo* foo = new Foo(); |
| 45 |
foo->Unref(); |
| 46 |
} |
| 47 |
|
| 48 |
TEST(RefCounted, ExtraRef) { |
| 49 |
Foo* foo = new Foo(); |
| 50 |
RefCountedPtr<Foo> foop = foo->Ref(); |
| 51 |
foop.release(); |
| 52 |
foo->Unref(); |
| 53 |
foo->Unref(); |
| 54 |
} |
| 55 |
|
| 56 |
class Value : public RefCounted<Value, PolymorphicRefCount, kUnrefNoDelete> { |
| 57 |
public: |
| 58 |
Value(int value, std::set<std::unique_ptr<Value>>* registry) : value_(value) { |
| 59 |
registry->emplace(this); |
| 60 |
} |
| 61 |
|
| 62 |
int value() const { return value_; } |
| 63 |
|
| 64 |
private: |
| 65 |
int value_; |
| 66 |
}; |
| 67 |
|
| 68 |
void GarbageCollectRegistry(std::set<std::unique_ptr<Value>>* registry) { |
| 69 |
for (auto it = registry->begin(); it != registry->end();) { |
| 70 |
RefCountedPtr<Value> v = (*it)->RefIfNonZero(); |
| 71 |
// Check if the object has any refs remaining. |
| 72 |
if (v != nullptr) { |
| 73 |
// It has refs remaining, so we do not delete it. |
| 74 |
++it; |
| 75 |
} else { |
| 76 |
// No refs remaining, so remove it from the registry. |
| 77 |
it = registry->erase(it); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
TEST(RefCounted, NoDeleteUponUnref) { |
| 83 |
std::set<std::unique_ptr<Value>> registry; |
| 84 |
// Add two objects to the registry. |
| 85 |
auto v1 = MakeRefCounted<Value>(1, ®istry); |
| 86 |
auto v2 = MakeRefCounted<Value>(2, ®istry); |
| 87 |
EXPECT_THAT(registry, |
| 88 |
::testing::UnorderedElementsAre( |
| 89 |
::testing::Pointee(::testing::Property(&Value::value, 1)), |
| 90 |
::testing::Pointee(::testing::Property(&Value::value, 2)))); |
| 91 |
// Running garbage collection should not delete anything, since both |
| 92 |
// entries still have refs. |
| 93 |
GarbageCollectRegistry(®istry); |
| 94 |
EXPECT_THAT(registry, |
| 95 |
::testing::UnorderedElementsAre( |
| 96 |
::testing::Pointee(::testing::Property(&Value::value, 1)), |
| 97 |
::testing::Pointee(::testing::Property(&Value::value, 2)))); |
| 98 |
// Unref v2 and run GC to remove it. |
| 99 |
v2.reset(); |
| 100 |
GarbageCollectRegistry(®istry); |
| 101 |
EXPECT_THAT(registry, ::testing::UnorderedElementsAre(::testing::Pointee( |
| 102 |
::testing::Property(&Value::value, 1)))); |
| 103 |
// Now unref v1 and run GC again. |
| 104 |
v1.reset(); |
| 105 |
GarbageCollectRegistry(®istry); |
| 106 |
EXPECT_THAT(registry, ::testing::UnorderedElementsAre()); |
| 107 |
} |
| 108 |
|
| 109 |
class ValueInExternalAllocation |
| 110 |
: public RefCounted<ValueInExternalAllocation, PolymorphicRefCount, |
| 111 |
kUnrefCallDtor> { |
| 112 |
public: |
| 113 |
explicit ValueInExternalAllocation(int value) : value_(value) {} |
| 114 |
|
| 115 |
int value() const { return value_; } |
| 116 |
|
| 117 |
private: |
| 118 |
int value_; |
| 119 |
}; |
| 120 |
|
| 121 |
TEST(RefCounted, CallDtorUponUnref) { |
| 122 |
std::aligned_storage<sizeof(ValueInExternalAllocation), |
| 123 |
alignof(ValueInExternalAllocation)>::type storage; |
| 124 |
RefCountedPtr<ValueInExternalAllocation> value( |
| 125 |
new (&storage) ValueInExternalAllocation(5)); |
| 126 |
EXPECT_EQ(value->value(), 5); |
| 127 |
} |
| 128 |
|
| 129 |
class FooNonPolymorphic |
| 130 |
: public RefCounted<FooNonPolymorphic, NonPolymorphicRefCount> { |
| 131 |
public: |
| 132 |
FooNonPolymorphic() { |
| 133 |
static_assert(!std::has_virtual_destructor<FooNonPolymorphic>::value, |
| 134 |
"NonPolymorphicRefCount has a virtual dtor"); |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
TEST(RefCountedNonPolymorphic, Basic) { |
| 139 |
FooNonPolymorphic* foo = new FooNonPolymorphic(); |
| 140 |
foo->Unref(); |
| 141 |
} |
| 142 |
|
| 143 |
TEST(RefCountedNonPolymorphic, ExtraRef) { |
| 144 |
FooNonPolymorphic* foo = new FooNonPolymorphic(); |
| 145 |
RefCountedPtr<FooNonPolymorphic> foop = foo->Ref(); |
| 146 |
foop.release(); |
| 147 |
foo->Unref(); |
| 148 |
foo->Unref(); |
| 149 |
} |
| 150 |
|
| 151 |
class FooWithTracing : public RefCounted<FooWithTracing> { |
| 152 |
public: |
| 153 |
FooWithTracing() : RefCounted("Foo") {} |
| 154 |
}; |
| 155 |
|
| 156 |
TEST(RefCountedWithTracing, Basic) { |
| 157 |
FooWithTracing* foo = new FooWithTracing(); |
| 158 |
RefCountedPtr<FooWithTracing> foop = foo->Ref(DEBUG_LOCATION, "extra_ref"); |
| 159 |
foop.release(); |
| 160 |
foo->Unref(DEBUG_LOCATION, "extra_ref"); |
| 161 |
// Can use the no-argument methods, too. |
| 162 |
foop = foo->Ref(); |
| 163 |
foop.release(); |
| 164 |
foo->Unref(); |
| 165 |
foo->Unref(DEBUG_LOCATION, "original_ref"); |
| 166 |
} |
| 167 |
|
| 168 |
class FooNonPolymorphicWithTracing |
| 169 |
: public RefCounted<FooNonPolymorphicWithTracing, NonPolymorphicRefCount> { |
| 170 |
public: |
| 171 |
FooNonPolymorphicWithTracing() : RefCounted("FooNonPolymorphicWithTracing") {} |
| 172 |
}; |
| 173 |
|
| 174 |
TEST(RefCountedNonPolymorphicWithTracing, Basic) { |
| 175 |
FooNonPolymorphicWithTracing* foo = new FooNonPolymorphicWithTracing(); |
| 176 |
RefCountedPtr<FooNonPolymorphicWithTracing> foop = |
| 177 |
foo->Ref(DEBUG_LOCATION, "extra_ref"); |
| 178 |
foop.release(); |
| 179 |
foo->Unref(DEBUG_LOCATION, "extra_ref"); |
| 180 |
// Can use the no-argument methods, too. |
| 181 |
foop = foo->Ref(); |
| 182 |
foop.release(); |
| 183 |
foo->Unref(); |
| 184 |
foo->Unref(DEBUG_LOCATION, "original_ref"); |
| 185 |
} |
| 186 |
|
| 187 |
} // namespace |
| 188 |
} // namespace testing |
| 189 |
} // namespace grpc_core |
| 190 |
|
| 191 |
int main(int argc, char** argv) { |
| 192 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 193 |
::testing::InitGoogleTest(&argc, argv); |
| 194 |
return RUN_ALL_TESTS(); |
| 195 |
} |