-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy paththread_compatible_shared_ptr_test.cc
More file actions
147 lines (124 loc) · 4.41 KB
/
Copy paththread_compatible_shared_ptr_test.cc
File metadata and controls
147 lines (124 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Copyright 2022 Google LLC. All Rights Reserved.
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 "cpp/thread_compatible_shared_ptr.h"
#include <memory>
#include <tuple>
#include <utility>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "cpp/common.h"
#include "cpp/thread_pool.h"
namespace array_record {
namespace {
class FooBase {
public:
virtual ~FooBase() = default;
virtual int value() const = 0;
virtual void add_value(int v) = 0;
virtual void mul_value(int v) = 0;
};
class Foo : public FooBase {
public:
explicit Foo(int v) : value_(v) {}
DECLARE_MOVE_ONLY_CLASS(Foo);
int value() const override { return value_; };
void add_value(int v) override { value_ += v; }
void mul_value(int v) override { value_ *= v; }
private:
int value_;
};
class ThreadCompatibleSharedPtrTest : public testing::Test {
public:
ThreadCompatibleSharedPtrTest() : pool_(ArrayRecordGlobalPool()) {}
protected:
ARThreadPool* pool_;
};
TEST_F(ThreadCompatibleSharedPtrTest, SanityTest) {
auto owned = ThreadCompatibleSharedPtr<FooBase>::Create(Foo(1));
EXPECT_TRUE(owned.is_owning());
EXPECT_EQ(owned.refcnt(), 0);
auto new_owned = std::move(owned);
EXPECT_TRUE(new_owned.is_owning());
// Not owning after move
EXPECT_FALSE(owned.is_owning()); // NOLINT(bugprone-use-after-move)
EXPECT_EQ(new_owned.refcnt(), 0);
// Valid const access
pool_->Schedule([refobj = new_owned]() {
absl::SleepFor(absl::Milliseconds(10));
EXPECT_EQ(refobj->value(), 1);
EXPECT_EQ(refobj.refcnt(), 1);
const auto second_ref = refobj;
EXPECT_EQ(second_ref.refcnt(), 2);
FooBase* foo =
const_cast<FooBase*>(reinterpret_cast<const FooBase*>(refobj.get()));
foo->add_value(1);
});
EXPECT_EQ(new_owned.refcnt(), 1);
// Blocks until pool add value 1 by 1, value is now 2
new_owned->mul_value(3);
EXPECT_EQ(new_owned->value(), 6);
// Destruction blocks until thread is executed
}
TEST_F(ThreadCompatibleSharedPtrTest, SanityTestWithTupleArg) {
auto owned =
ThreadCompatibleSharedPtr<FooBase>::Create<Foo>(std::forward_as_tuple(1));
EXPECT_TRUE(owned.is_owning());
EXPECT_EQ(owned.refcnt(), 0);
auto new_owned = std::move(owned);
EXPECT_TRUE(new_owned.is_owning());
// Not owning after move
EXPECT_FALSE(owned.is_owning()); // NOLINT(bugprone-use-after-move)
EXPECT_EQ(new_owned.refcnt(), 0);
// Valid const access
pool_->Schedule([refobj = new_owned]() {
absl::SleepFor(absl::Milliseconds(10));
EXPECT_EQ(refobj->value(), 1);
EXPECT_EQ(refobj.refcnt(), 1);
FooBase* foo =
const_cast<FooBase*>(reinterpret_cast<const FooBase*>(refobj.get()));
foo->add_value(1);
});
EXPECT_EQ(new_owned.refcnt(), 1);
// Blocks until pool add value 1 by 1, value is now 2
new_owned->mul_value(3);
EXPECT_EQ(new_owned->value(), 6);
// Destruction blocks until thread is executed
}
TEST_F(ThreadCompatibleSharedPtrTest, SanityTestWithUnique) {
auto owned =
ThreadCompatibleSharedPtr<FooBase>::Create(std::make_unique<Foo>(1));
EXPECT_TRUE(owned.is_owning());
EXPECT_EQ(owned.refcnt(), 0);
auto new_owned = std::move(owned);
EXPECT_TRUE(new_owned.is_owning());
// Not owning after move
EXPECT_FALSE(owned.is_owning()); // NOLINT(bugprone-use-after-move)
EXPECT_EQ(new_owned.refcnt(), 0);
// Valid const access
pool_->Schedule([refobj = new_owned]() {
absl::SleepFor(absl::Milliseconds(10));
EXPECT_EQ(refobj->value(), 1);
EXPECT_EQ(refobj.refcnt(), 1);
FooBase* foo =
const_cast<FooBase*>(reinterpret_cast<const FooBase*>(refobj.get()));
foo->add_value(1);
});
EXPECT_EQ(new_owned.refcnt(), 1);
// Blocks until pool add value 1 by 1, value is now 2
new_owned->mul_value(3);
EXPECT_EQ(new_owned->value(), 6);
// Destruction blocks until thread is executed
}
} // namespace
} // namespace array_record