diff options
author | Mark D. Roth <roth@google.com> | 2018-01-17 08:58:33 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-17 08:58:33 -0800 |
commit | 8afe8d35e13d52fa61c72f9570bc900253ffebb9 (patch) | |
tree | 74f2aa18daebea5bdbf5b118ca8eabe5aa9ca891 /test | |
parent | acca4a2a6d79a9465978fa9bcee4db3fc24548e3 (diff) | |
parent | 2b19f4922db21a77d43808a1f1b2e9475348b1d7 (diff) |
Merge pull request #13984 from markdroth/ref_counting
Fix existing ref counting classes and add new ones.
Diffstat (limited to 'test')
-rw-r--r-- | test/core/support/BUILD | 13 | ||||
-rw-r--r-- | test/core/support/orphanable_test.cc | 114 | ||||
-rw-r--r-- | test/core/support/ref_counted_ptr_test.cc | 13 | ||||
-rw-r--r-- | test/core/support/ref_counted_test.cc | 4 |
4 files changed, 143 insertions, 1 deletions
diff --git a/test/core/support/BUILD b/test/core/support/BUILD index 4372b49b54..c8fa046da1 100644 --- a/test/core/support/BUILD +++ b/test/core/support/BUILD @@ -215,6 +215,19 @@ grpc_cc_test( ) grpc_cc_test( + name = "orphanable_test", + srcs = ["orphanable_test.cc"], + language = "C++", + deps = [ + "//:orphanable", + "//test/core/util:gpr_test_util", + ], + external_deps = [ + "gtest", + ], +) + +grpc_cc_test( name = "ref_counted_test", srcs = ["ref_counted_test.cc"], language = "C++", diff --git a/test/core/support/orphanable_test.cc b/test/core/support/orphanable_test.cc new file mode 100644 index 0000000000..e07017ab1e --- /dev/null +++ b/test/core/support/orphanable_test.cc @@ -0,0 +1,114 @@ +/* + * + * Copyright 2017 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/support/orphanable.h" + +#include <gtest/gtest.h> + +#include "src/core/lib/support/memory.h" +#include "test/core/util/test_config.h" + +namespace grpc_core { +namespace testing { +namespace { + +class Foo : public Orphanable { + public: + Foo() : Foo(0) {} + explicit Foo(int value) : value_(value) {} + void Orphan() override { Delete(this); } + int value() const { return value_; } + + private: + int value_; +}; + +TEST(Orphanable, Basic) { + Foo* foo = New<Foo>(); + foo->Orphan(); +} + +TEST(OrphanablePtr, Basic) { + OrphanablePtr<Foo> foo(New<Foo>()); + EXPECT_EQ(0, foo->value()); +} + +TEST(MakeOrphanable, DefaultConstructor) { + auto foo = MakeOrphanable<Foo>(); + EXPECT_EQ(0, foo->value()); +} + +TEST(MakeOrphanable, WithParameters) { + auto foo = MakeOrphanable<Foo>(5); + EXPECT_EQ(5, foo->value()); +} + +class Bar : public InternallyRefCounted { + public: + Bar() : Bar(0) {} + explicit Bar(int value) : value_(value) {} + void Orphan() override { Unref(); } + int value() const { return value_; } + + void StartWork() { Ref(); } + void FinishWork() { Unref(); } + + private: + int value_; +}; + +TEST(OrphanablePtr, InternallyRefCounted) { + auto bar = MakeOrphanable<Bar>(); + bar->StartWork(); + bar->FinishWork(); +} + +// Note: We use DebugOnlyTraceFlag instead of TraceFlag to ensure that +// things build properly in both debug and non-debug cases. +DebugOnlyTraceFlag baz_tracer(true, "baz"); + +class Baz : public InternallyRefCountedWithTracing { + public: + Baz() : Baz(0) {} + explicit Baz(int value) + : InternallyRefCountedWithTracing(&baz_tracer), value_(value) {} + void Orphan() override { Unref(); } + int value() const { return value_; } + + void StartWork() { Ref(DEBUG_LOCATION, "work"); } + void FinishWork() { Unref(DEBUG_LOCATION, "work"); } + + private: + int value_; +}; + +TEST(OrphanablePtr, InternallyRefCountedWithTracing) { + auto baz = MakeOrphanable<Baz>(); + baz->StartWork(); + baz->FinishWork(); +} + +} // namespace +} // namespace testing +} // namespace grpc_core + +int main(int argc, char** argv) { + grpc_test_init(argc, argv); + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/core/support/ref_counted_ptr_test.cc b/test/core/support/ref_counted_ptr_test.cc index 1830edc4e5..ce4975d347 100644 --- a/test/core/support/ref_counted_ptr_test.cc +++ b/test/core/support/ref_counted_ptr_test.cc @@ -138,6 +138,19 @@ TEST(RefCountedPtr, DerefernceOperators) { foo_ref.value(); } +TEST(RefCountedPtr, EqualityOperators) { + RefCountedPtr<Foo> foo(New<Foo>()); + RefCountedPtr<Foo> bar = foo; + RefCountedPtr<Foo> empty; + // Test equality between RefCountedPtrs. + EXPECT_EQ(foo, bar); + EXPECT_NE(foo, empty); + // Test equality with bare pointers. + EXPECT_EQ(foo, foo.get()); + EXPECT_EQ(empty, nullptr); + EXPECT_NE(foo, nullptr); +} + TEST(MakeRefCounted, NoArgs) { RefCountedPtr<Foo> foo = MakeRefCounted<Foo>(); EXPECT_EQ(0, foo->value()); diff --git a/test/core/support/ref_counted_test.cc b/test/core/support/ref_counted_test.cc index be9b6ff7c2..0629e3ff5f 100644 --- a/test/core/support/ref_counted_test.cc +++ b/test/core/support/ref_counted_test.cc @@ -44,7 +44,9 @@ TEST(RefCounted, ExtraRef) { foo->Unref(); } -TraceFlag foo_tracer(true, "foo"); +// Note: We use DebugOnlyTraceFlag instead of TraceFlag to ensure that +// things build properly in both debug and non-debug cases. +DebugOnlyTraceFlag foo_tracer(true, "foo"); class FooWithTracing : public RefCountedWithTracing { public: |