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 <gtest/gtest.h>
17 #include <string>
18 #include "unittest_log.h"
19 #include <cstdlib>
20 #include <string>
21 #include <cmath>
22 #include <chrono>
23 #include <iostream>
24 #include <sstream>
25 #include "common/status.h"
26 #include "securec.h"
27 #define HST_LOG_TAG "Task"
28 #include "osal/task/task.h"
29 #include "osal/task/condition_variable.h"
30 #include "cpp_ext/memory_ext.h"
31 #include "common/log.h"
32
33 namespace {
34 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_ONLY_PRERELEASE, LOG_DOMAIN_FOUNDATION, "TaskFuncUnitTest" };
35 }
36
37 using namespace std;
38 using namespace testing::ext;
39 using namespace OHOS;
40 using namespace OHOS::Media;
41
42 namespace OHOS {
43 namespace Media {
44 namespace MetaFuncUT {
45 class TaskInnerUnitTest : public testing::Test {
46 public:
47 static void SetUpTestCase(void);
48
49 static void TearDownTestCase(void);
50
51 void SetUp(void);
52
53 void TearDown(void);
54
55 ConditionVariable cv;
56 std::shared_ptr<Task> task1 = nullptr;
57 std::shared_ptr<Task> task2 = nullptr;
58 std::shared_ptr<Task> task3 = nullptr;
59 std::shared_ptr<Task> task4 = nullptr;
60 std::shared_ptr<Task> task5 = nullptr;
61 std::shared_ptr<Task> task6 = nullptr;
62 std::shared_ptr<Task> task7 = nullptr;
63 std::shared_ptr<Task> task8 = nullptr;
64 std::shared_ptr<Task> task9 = nullptr;
65 std::shared_ptr<Task> task10 = nullptr;
66 std::shared_ptr<Task> task01 = nullptr;
67 std::shared_ptr<Task> task02 = nullptr;
68 Mutex mutex_;
69 std::atomic<bool> isStop_{false};
70 std::string modifyMsg_ = "";
71 };
72
SetUpTestCase(void)73 void TaskInnerUnitTest::SetUpTestCase(void) {}
74
TearDownTestCase(void)75 void TaskInnerUnitTest::TearDownTestCase(void) {}
76
SetUp(void)77 void TaskInnerUnitTest::SetUp(void)
78 {
79 std::cout << "[SetUp]: SetUp!!!, test: ";
80 const ::testing::TestInfo *testInfo_ = ::testing::UnitTest::GetInstance()->current_test_info();
81 std::string testName = testInfo_->name();
82 std::cout << testName << std::endl;
83 task1 = std::make_shared<Task>("workTask1");
84 task2 = std::make_shared<Task>("workTask2");
85 task3 = std::make_shared<Task>("workTask3");
86 task4 = std::make_shared<Task>("workTask4");
87 task5 = std::make_shared<Task>("workTask5");
88 task6 = std::make_shared<Task>("workTask6");
89 task7 = std::make_shared<Task>("workTask7");
90 task8 = std::make_shared<Task>("workTask8");
91 task9 = std::make_shared<Task>("workTask9");
92 task10 = std::make_shared<Task>("workTask10");
93 task01 = std::make_shared<Task>("workTask01");
94 task02 = std::make_shared<Task>("workTask02");
95 }
96
TearDown(void)97 void TaskInnerUnitTest::TearDown(void)
98 {
99 task1 = nullptr;
100 task1 = nullptr;
101 task2 = nullptr;
102 task3 = nullptr;
103 task4 = nullptr;
104 task5 = nullptr;
105 task6 = nullptr;
106 task7 = nullptr;
107 task8 = nullptr;
108 task9 = nullptr;
109 task10 = nullptr;
110 task01 = nullptr;
111 task02 = nullptr;
112 std::cout << "[TearDown]: over!!!" << std::endl;
113 }
114
115 /**
116 * @tc.name: TaskNotRunning
117 * @tc.desc: TaskNotRunning
118 * @tc.type: FUNC
119 */
120 HWTEST_F(TaskInnerUnitTest, TaskNotRunning, TestSize.Level1)
121 {
122 bool rtv = task1->IsTaskRunning();
123 EXPECT_EQ(false, rtv);
124 }
125
126 /**
127 * @tc.name: TaskRegisterJobNotRunning
128 * @tc.desc: TaskRegisterJobNotRunning
129 * @tc.type: FUNC
130 */
131 HWTEST_F(TaskInnerUnitTest, TaskRegisterJobNotRunning, TestSize.Level1)
132 {
__anon797004620202() 133 task2->RegisterJob([]() {
134 bool runningState =true;
135 int count = 0;
136 while (runningState) {
137 count++;
138 MEDIA_LOG_I("Task TaskRegisterJobNotRunning running at " PUBLIC_LOG_U32, count);
139 sleep(1);
140 if (count > 10) { //10 second
141 runningState = false;
142 }
143 }
144 return 0;
145 });
146 bool rtv = task2->IsTaskRunning();
147 EXPECT_EQ(false, rtv);
148 }
149
150
151 /**
152 * @tc.name: TaskRegisterJobRun
153 * @tc.desc: TaskRegisterJobRun
154 * @tc.type: FUNC
155 */
156 HWTEST_F(TaskInnerUnitTest, TaskRegisterJobRun, TestSize.Level1)
157 {
__anon797004620302() 158 task3->RegisterJob([]() {
159 bool runningState =true;
160 int count = 0;
161 while (runningState) {
162 count++;
163 MEDIA_LOG_I("Task TaskRegisterJobRun running at " PUBLIC_LOG_U32, count);
164 sleep(1);
165 if (count > 10) { //10 second
166 runningState = false;
167 }
168 }
169 return 0;
170 });
171 task3->Start();
172 sleep(1);
173 bool rtv = task3->IsTaskRunning();
174 EXPECT_EQ(true, rtv);
175 }
176
177 /**
178 * @tc.name: TaskRegisterJobPause
179 * @tc.desc: TaskRegisterJobPause
180 * @tc.type: FUNC
181 */
182 HWTEST_F(TaskInnerUnitTest, TaskRegisterJobPause, TestSize.Level1)
183 {
__anon797004620402() 184 task4->RegisterJob([]() {
185 bool runningState =true;
186 int count = 0;
187 while (runningState) {
188 count++;
189 MEDIA_LOG_I("Task TaskRegisterJobPause running at " PUBLIC_LOG_U32, count);
190 sleep(1);
191 if (count > 10) { //10 second
192 runningState = false;
193 }
194 }
195 return 0;
196 });
197 task4->Start();
198 task4->Pause();
199 sleep(1);
200 bool rtv = task4->IsTaskRunning();
201 EXPECT_EQ(false, rtv);
202 task4->Pause();
203 sleep(1);
204 rtv = task4->IsTaskRunning();
205 EXPECT_EQ(false, rtv);
206 }
207
208 /**
209 * @tc.name: TaskJobPauseResume
210 * @tc.desc: TaskJobPauseResume
211 * @tc.type: FUNC
212 */
213 HWTEST_F(TaskInnerUnitTest, TaskJobPauseResume, TestSize.Level1)
214 {
__anon797004620502() 215 task5->RegisterJob([]() {
216 bool runningState =true;
217 int count = 0;
218 while (runningState) {
219 count++;
220 MEDIA_LOG_I("Task TaskJobPauseResume running at " PUBLIC_LOG_U32, count);
221 sleep(1);
222 if (count > 10) { //10 second
223 runningState = false;
224 }
225 }
226 return 0;
227 });
228 task5->Start();
229 task5->Pause();
230 sleep(1);
231 bool rtv = task5->IsTaskRunning();
232 EXPECT_EQ(false, rtv);
233 task5->Start();
234 sleep(1);
235 rtv = task5->IsTaskRunning();
236 EXPECT_EQ(true, rtv);
237 }
238
239
240 /**
241 * @tc.name: TaskRegisterJobPauseAsync
242 * @tc.desc: TaskRegisterJobPauseAsync
243 * @tc.type: FUNC
244 */
245 HWTEST_F(TaskInnerUnitTest, TaskRegisterJobPauseAsync, TestSize.Level1)
246 {
__anon797004620602() 247 task6->RegisterJob([]() {
248 bool runningState =true;
249 int count = 0;
250 while (runningState) {
251 count++;
252 MEDIA_LOG_I("Task TaskRegisterJobPauseAsync running at " PUBLIC_LOG_U32, count);
253 sleep(1);
254 if (count > 10) { //10 second
255 runningState = false;
256 }
257 }
258 return 0;
259 });
260 task6->Start();
261 task6->PauseAsync();
262 sleep(1);
263 bool rtv = task6->IsTaskRunning();
264 EXPECT_EQ(false, rtv);
265 task6->PauseAsync();
266 sleep(1);
267 rtv = task6->IsTaskRunning();
268 EXPECT_EQ(false, rtv);
269 }
270
271
272 /**
273 * @tc.name: TaskRegisterJobStopAsync
274 * @tc.desc: TaskRegisterJobStopAsync
275 * @tc.type: FUNC
276 */
277 HWTEST_F(TaskInnerUnitTest, TaskRegisterJobStopAsync, TestSize.Level1)
278 {
__anon797004620702() 279 task7->RegisterJob([]() {
280 bool runningState =true;
281 int count = 0;
282 while (runningState) {
283 count++;
284 MEDIA_LOG_I("Task TaskRegisterJobStopAsync running at " PUBLIC_LOG_U32, count);
285 sleep(1);
286 if (count > 10) { //10 second
287 runningState = false;
288 }
289 }
290 return 0;
291 });
292 task7->Start();
293 sleep(1);
294 task7->StopAsync();
295 sleep(1);
296 bool rtv = task7->IsTaskRunning();
297 EXPECT_EQ(false, rtv);
298 }
299
300 /**
301 * @tc.name: TaskRegisterJobStop
302 * @tc.desc: TaskRegisterJobStop
303 * @tc.type: FUNC
304 */
305 HWTEST_F(TaskInnerUnitTest, TaskRegisterJobStop, TestSize.Level1)
306 {
__anon797004620802() 307 task8->RegisterJob([]() {
308 bool runningState =true;
309 int count = 0;
310 while (runningState) {
311 count++;
312 MEDIA_LOG_I("Task TaskRegisterJobStop running at " PUBLIC_LOG_U32, count);
313 sleep(1);
314 if (count > 10) { //10 second
315 runningState = false;
316 }
317 }
318 return 0;
319 });
320 task8->Start();
321 sleep(2);
322 task8->Stop();
323 sleep(1);
324 bool rtv = task8->IsTaskRunning();
325 EXPECT_EQ(false, rtv);
326 }
327
328 /**
329 * @tc.name: TaskRegisterJobStopResume
330 * @tc.desc: TaskRegisterJobStopResume
331 * @tc.type: FUNC
332 */
333 HWTEST_F(TaskInnerUnitTest, TaskRegisterJobStopResume, TestSize.Level1)
334 {
__anon797004620902() 335 task9->RegisterJob([]() {
336 bool runningState =true;
337 int count = 0;
338 while (runningState) {
339 count++;
340 MEDIA_LOG_I("Task TaskRegisterJobStopResume running at " PUBLIC_LOG_U32, count);
341 sleep(1);
342 if (count > 10) { //10 second
343 runningState = false;
344 }
345 }
346 return 0;
347 });
348 task9->Start();
349 sleep(2);
350 task9->Stop();
351 sleep(1);
352 task9->Start();
353 sleep(1);
354 bool rtv = task9->IsTaskRunning();
355 EXPECT_EQ(true, rtv);
356 }
357
358 /**
359 * @tc.name: TaskJobPauseStopResumeStart
360 * @tc.desc: TaskJobPauseStopResumeStart
361 * @tc.type: FUNC
362 */
363 HWTEST_F(TaskInnerUnitTest, TaskJobPauseStopResumeStart, TestSize.Level1)
364 {
__anon797004620a02() 365 task10->RegisterJob([this]() {
366 bool runningState =true;
367 int count = 0;
368 while (runningState) {
369 count++;
370 MEDIA_LOG_I("Task TaskJobPauseStopResumeStart running at " PUBLIC_LOG_U32, count);
371 sleep(1);
372 if (count > 5) { //5 second
373 this->modifyMsg_ = "middle";
374 }
375 if (count > 20) { //20 second
376 runningState = false;
377 }
378 }
379 return 0;
380 });
381 task10->Start();
382 sleep(3);
383 task10->Pause();
384 sleep(2);
385 task10->Start();
386 sleep(2);
387 task10->Stop();
388 sleep(1);
389 EXPECT_EQ("middle", modifyMsg_);
390 task10->Start();
391 sleep(1);
392 bool rtv = task10->IsTaskRunning();
393 EXPECT_EQ(true, rtv);
394 }
395
396 /**
397 * @tc.name: WaitFor_Succ
398 * @tc.desc: WaitFor_Succ
399 * @tc.type: FUNC
400 */
401 HWTEST_F(TaskInnerUnitTest, WaitFor_Succ, TestSize.Level1)
402 {
403 AutoLock lock(mutex_);
__anon797004620b02() 404 task01->RegisterJob([]() {
405 bool runningState =true;
406 int count = 0;
407 while (runningState) {
408 count++;
409 MEDIA_LOG_I("Task WaitFor_Succ running at " PUBLIC_LOG_U32, count);
410 sleep(1);
411 if (count > 10){ //10 second
412 runningState = false;
413 }
414 }
415 return 0;
416 });
417 task01->Start();
418 int timeoutMs = 1000;
419 isStop_.store(true);
__anon797004620c02null420 auto rtv = cv.WaitFor(lock, timeoutMs, [this] { return isStop_.load(); });
421 EXPECT_EQ(true, rtv);
422 }
423
424 /**
425 * @tc.name: WaitFor_Failed
426 * @tc.desc: WaitFor_Failed
427 * @tc.type: FUNC
428 */
429 HWTEST_F(TaskInnerUnitTest, WaitFor_Failed, TestSize.Level1)
430 {
431 AutoLock lock(mutex_);
__anon797004620d02() 432 task02->RegisterJob([]() {
433 bool runningState =true;
434 int count = 0;
435 while (runningState) {
436 count++;
437 MEDIA_LOG_I("Task WaitFor_Failed running at " PUBLIC_LOG_U32, count);
438 sleep(1);
439 if (count > 10){ //10 second
440 runningState = false;
441 }
442 }
443 return 0;
444 });
445 task02->Start();
446 int timeoutMs = 100;
447 isStop_.store(false);
448 auto start = std::chrono::high_resolution_clock::now();
__anon797004620e02null449 auto rtv = cv.WaitFor(lock, timeoutMs, [this] { return isStop_.load(); });
450 auto end = std::chrono::high_resolution_clock::now();
451 int64_t diff = static_cast<int64_t>(static_cast<std::chrono::duration<double>>(end - start).count() * 1000);
452 EXPECT_EQ(false, rtv);
453 EXPECT_TRUE((std::abs(static_cast<int>(diff) - timeoutMs) < 20) || (diff < 5));
454 MEDIA_LOG_I("Wait Time Diff: " PUBLIC_LOG_D64, diff);
455 }
456 } // namespace MetaFuncUT
457 } // namespace Media
458 } // namespace OHOS
459