1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <functional>
17 #include <mutex>
18
19 #include "log_print.h"
20 #include "pool.h"
21 #include "gtest/gtest.h"
22
23 using namespace testing::ext;
24 using namespace OHOS;
25 namespace OHOS::Test {
26 static constexpr uint32_t CAPABILITY_TEST = 3; // capability
27 static constexpr uint32_t MIN_TEST = 1; // min
28 class PoolTest : public testing::Test {
29 public:
30 struct Node {
31 int value;
operator ==OHOS::Test::PoolTest::Node32 bool operator==(Node &other)
33 {
34 return value == other.value;
35 }
NodeOHOS::Test::PoolTest::Node36 explicit Node(const std::string &threadName = "pool_test") {};
37 };
38 static void SetUpTestCase(void);
39 static void TearDownTestCase(void);
40 void SetUp();
41 void TearDown();
42 protected:
43 static Pool<PoolTest::Node> pool_;
44 };
45 Pool<PoolTest::Node> PoolTest::pool_ = Pool<PoolTest::Node>(CAPABILITY_TEST, MIN_TEST, "pool_test");
46
SetUpTestCase(void)47 void PoolTest::SetUpTestCase(void) { }
48
TearDownTestCase(void)49 void PoolTest::TearDownTestCase(void) { }
50
SetUp(void)51 void PoolTest::SetUp(void) { }
52
TearDown(void)53 void PoolTest::TearDown(void)
54 {
55 auto close = [](std::shared_ptr<PoolTest::Node> data) {
56 pool_.Idle(data);
57 pool_.Release(data);
58 };
59 pool_.Clean(close);
60 }
61
62 /**
63 * @tc.name: Get_001
64 * @tc.desc: test the std::shared_ptr<T> Get(bool isForce = false) function.
65 * @tc.type: FUNC
66 * @tc.require:
67 * @tc.author: suoqilong
68 */
69 HWTEST_F(PoolTest, Get_001, TestSize.Level1)
70 {
71 int index = 0;
72 auto ret = pool_.Get();
73 EXPECT_NE(ret, nullptr);
74 ret->value = index++;
75
76 ret = pool_.Get();
77 EXPECT_NE(ret, nullptr);
78 ret->value = index++;
79
80 ret = pool_.Get();
81 EXPECT_NE(ret, nullptr);
82 ret->value = index++;
83
84 ret = pool_.Get();
85 EXPECT_EQ(ret, nullptr);
86 }
87
88 /**
89 * @tc.name: Get_002
90 * @tc.desc: test the std::shared_ptr<T> Get(bool isForce = false) function.
91 * @tc.type: FUNC
92 * @tc.require:
93 * @tc.author: suoqilong
94 */
95 HWTEST_F(PoolTest, Get_002, TestSize.Level1)
96 {
97 int index = 0;
98 auto ret = pool_.Get();
99 EXPECT_NE(ret, nullptr);
100 ret->value = index++;
101
102 ret = pool_.Get();
103 EXPECT_NE(ret, nullptr);
104 ret->value = index++;
105
106 ret = pool_.Get();
107 EXPECT_NE(ret, nullptr);
108 ret->value = index++;
109
110 ret = pool_.Get(true);
111 EXPECT_NE(ret, nullptr);
112 ret->value = index++;
113
114 ret = pool_.Get();
115 EXPECT_EQ(ret, nullptr);
116 }
117
118 /**
119 * @tc.name: Release_001
120 * @tc.desc: test the int32_t Release(std::shared_ptr<T> data, bool force = false) function.
121 * @tc.type: FUNC
122 * @tc.require:
123 * @tc.author: suoqilong
124 */
125 HWTEST_F(PoolTest, Release_001, TestSize.Level1)
126 {
127 int index = 0;
128 auto ret = pool_.Get();
129 EXPECT_NE(ret, nullptr);
130 ret->value = index++;
131
132 ret = pool_.Get();
133 EXPECT_NE(ret, nullptr);
134
135 pool_.Idle(ret);
136 auto retRelease = pool_.Release(ret);
137 EXPECT_EQ(retRelease, true);
138 }
139
140 /**
141 * @tc.name: Release_002
142 * @tc.desc: test the int32_t Release(std::shared_ptr<T> data, bool force = false) function.
143 * @tc.type: FUNC
144 * @tc.require:
145 * @tc.author: suoqilong
146 */
147 HWTEST_F(PoolTest, Release_002, TestSize.Level1)
148 {
149 auto ret = pool_.Get();
150 EXPECT_NE(ret, nullptr);
151
152 pool_.Idle(ret);
153 auto retRelease = pool_.Release(ret);
154 EXPECT_EQ(retRelease, false);
155 }
156
157 /**
158 * @tc.name: Release_003
159 * @tc.desc: test the int32_t Release(std::shared_ptr<T> data, bool force = false) function.
160 * @tc.type: FUNC
161 * @tc.require:
162 * @tc.author: suoqilong
163 */
164 HWTEST_F(PoolTest, Release_003, TestSize.Level1)
165 {
166 int index = 0;
167 auto ret = pool_.Get();
168 EXPECT_NE(ret, nullptr);
169 ret->value = index++;
170
171 ret = pool_.Get();
172 EXPECT_NE(ret, nullptr);
173
174 pool_.Idle(ret);
175 auto retRelease = pool_.Release(ret);
176 EXPECT_EQ(retRelease, true);
177 }
178
179 /**
180 * @tc.name: Release_004
181 * @tc.desc: test the int32_t Release(std::shared_ptr<T> data, bool force = false) function.
182 * @tc.type: FUNC
183 * @tc.require:
184 * @tc.author: suoqilong
185 */
186 HWTEST_F(PoolTest, Release_004, TestSize.Level1)
187 {
188 int index = 0;
189 auto ret = pool_.Get();
190 EXPECT_NE(ret, nullptr);
191 ret->value = index++;
192
193 ret = pool_.Get();
194 EXPECT_NE(ret, nullptr);
195 ret->value = index++;
196
197 ret = pool_.Get();
198 EXPECT_NE(ret, nullptr);
199 ret->value = index++;
200
201 ret = pool_.Get(true);
202 EXPECT_NE(ret, nullptr);
203 ret->value = index++;
204
205 ret = pool_.Get();
206 EXPECT_EQ(ret, nullptr);
207
208 pool_.Idle(ret);
209 auto retRelease = pool_.Release(ret);
210 EXPECT_EQ(retRelease, false);
211 }
212
213 /**
214 * @tc.name: Release_005
215 * @tc.desc: test the int32_t Release(std::shared_ptr<T> data, bool force = false) function.
216 * @tc.type: FUNC
217 * @tc.require:
218 * @tc.author: suoqilong
219 */
220 HWTEST_F(PoolTest, Release_005, TestSize.Level1)
221 {
222 int index = 0;
223 auto ret = pool_.Get();
224 EXPECT_NE(ret, nullptr);
225 ret->value = index++;
226
227 ret = pool_.Get();
228 EXPECT_NE(ret, nullptr);
229
230 auto data = std::make_shared<PoolTest::Node>();
231 pool_.Idle(ret);
232 auto retRelease = pool_.Release(data);
233 EXPECT_EQ(retRelease, false);
234 }
235
236 /**
237 * @tc.name: Release_006
238 * @tc.desc: test the int32_t Release(std::shared_ptr<T> data, bool force = false) function.
239 * @tc.type: FUNC
240 * @tc.require:
241 * @tc.author: suoqilong
242 */
243 HWTEST_F(PoolTest, Release_006, TestSize.Level1)
244 {
245 auto ret = pool_.Get();
246 EXPECT_NE(ret, nullptr);
247
248 pool_.Idle(ret);
249 auto retRelease = pool_.Release(ret, true);
250 EXPECT_EQ(retRelease, true);
251 }
252
253 /**
254 * @tc.name: Release_007
255 * @tc.desc: test the int32_t Release(std::shared_ptr<T> data, bool force = false) function.
256 * @tc.type: FUNC
257 * @tc.require:
258 * @tc.author: suoqilong
259 */
260 HWTEST_F(PoolTest, Release_007, TestSize.Level1)
261 {
262 auto ret = nullptr;
263 auto retRelease = pool_.Release(ret, true);
264 EXPECT_EQ(retRelease, false);
265 }
266
267 /**
268 * @tc.name: Idle_001
269 * @tc.desc: test the void Idle(std::shared_ptr<T> data) function.
270 * @tc.type: FUNC
271 * @tc.require:
272 * @tc.author: suoqilong
273 */
274 HWTEST_F(PoolTest, Idle_001, TestSize.Level1)
275 {
276 int index = 0;
277 auto ret = pool_.Get();
278 EXPECT_NE(ret, nullptr);
279 ret->value = index++;
280
281 ret = pool_.Get();
282 EXPECT_NE(ret, nullptr);
283 ret->value = index++;
284
285 ret = pool_.Get();
286 EXPECT_NE(ret, nullptr);
287
288 pool_.Idle(ret);
289 auto retRelease = pool_.Release(ret);
290 EXPECT_EQ(retRelease, true);
291 }
292
293 /**
294 * @tc.name: Clean_001
295 * @tc.desc: test the int32_t Clean(std::function<void(std::shared_ptr<T>)> close) noexcept function.
296 * @tc.type: FUNC
297 * @tc.require:
298 * @tc.author: suoqilong
299 */
300 HWTEST_F(PoolTest, Clean_001, TestSize.Level1)
301 {
302 int index = 0;
303 auto ret = pool_.Get();
304 EXPECT_NE(ret, nullptr);
305 ret->value = index++;
306
307 ret = pool_.Get();
308 EXPECT_NE(ret, nullptr);
309 ret->value = index++;
310
311 ret = pool_.Get();
312 EXPECT_NE(ret, nullptr);
313
__anon5f8ac7000202(std::shared_ptr<PoolTest::Node> data) 314 auto close = [](std::shared_ptr<PoolTest::Node> data) {
315 pool_.Idle(data);
316 pool_.Release(data);
317 };
318 auto retClean = pool_.Clean(close);
319 EXPECT_EQ(retClean, true);
320 }
321 } // namespace OHOS::Test
322