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 #define HST_LOG_TAG "Task"
19 #include "osal/task/task.h"
20 #include "common/log.h"
21 #include "osal/task/pipeline_threadpool.h"
22
23 using namespace std;
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::Media;
27
28 namespace OHOS {
29 namespace Media {
30 namespace TaskInnerFuncUT {
31 class TaskInnerFuncUnitTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34
35 static void TearDownTestCase(void);
36
37 void SetUp(void);
38
39 void TearDown(void);
40
41 Mutex mutex_;
42 std::atomic<bool> isStop_{false};
43 std::string modifyMsg_ = "";
44 std::string groupId_ = "";
45 };
46
SetUpTestCase(void)47 void TaskInnerFuncUnitTest::SetUpTestCase(void) {}
48
TearDownTestCase(void)49 void TaskInnerFuncUnitTest::TearDownTestCase(void) {}
50
SetUp(void)51 void TaskInnerFuncUnitTest::SetUp(void)
52 {
53 std::cout << "[SetUp]: SetUp!!!, test: ";
54 const ::testing::TestInfo *testInfo_ = ::testing::UnitTest::GetInstance()->current_test_info();
55 std::string testName = testInfo_->name();
56 std::cout << testName << std::endl;
57 }
58
TearDown(void)59 void TaskInnerFuncUnitTest::TearDown(void)
60 {
61 PipeLineThreadPool::GetInstance().DestroyThread(groupId_);
62 std::cout << "[TearDown]: over!!!" << std::endl;
63 }
64
65 /**
66 * @tc.name: Pause_Pause_Stop_Stop
67 * @tc.desc: Pause_Pause_Stop_Stop
68 * @tc.type: FUNC
69 */
HWTEST_F(TaskInnerFuncUnitTest, Pause_Pause_Stop_Stop, TestSize.Level1)70 HWTEST_F(TaskInnerFuncUnitTest, Pause_Pause_Stop_Stop, TestSize.Level1)
71 {
72 std::shared_ptr<Task> task = std::make_shared<Task>("workTask");
73 AutoLock lock(mutex_);
74 task->RegisterJob([]() {
75 bool runningState =true;
76 int count = 0;
77 while (runningState) {
78 count++;
79 sleep(1);
80 if (count > 10){ //10 second
81 runningState = false;
82 }
83 }
84 return 0;
85 });
86 task->Start();
87 sleep(1);
88 task->Pause();
89 sleep(1);
90 task->Pause();
91 sleep(1);
92 task->PauseAsync();
93 task->Stop();
94 task->Stop();
95 ASSERT_EQ(false, task->IsTaskRunning());
96 }
97
98 /**
99 * @tc.name: UpdateTop_Empty_MsgQueue
100 * @tc.desc: UpdateTop No SubmitJob
101 * @tc.type: FUNC
102 */
HWTEST_F(TaskInnerFuncUnitTest, UpdateTop_Empty_MsgQueue, TestSize.Level1)103 HWTEST_F(TaskInnerFuncUnitTest, UpdateTop_Empty_MsgQueue, TestSize.Level1)
104 {
105 std::shared_ptr<Task> task = std::make_shared<Task>("workTask", groupId_, TaskType::SINGLETON,
106 TaskPriority::NORMAL, false);
107 AutoLock lock(mutex_);
108 task->RegisterJob([]() {
109 bool runningState =true;
110 int count = 0;
111 while (runningState) {
112 count++;
113 sleep(1);
114 if (count > 10){ //10 second
115 runningState = false;
116 }
117 }
118 return 0;
119 });
120 task->Start();
121 sleep(1);
122 ASSERT_EQ(true, task->IsTaskRunning());
123 }
124
125 /**
126 * @tc.name: Stop_SubmitJob_UpdateTop
127 * @tc.desc: UpdateTop jobQueue no empty, state is not start
128 * @tc.type: FUNC
129 */
HWTEST_F(TaskInnerFuncUnitTest, Stop_SubmitJob_UpdateTop, TestSize.Level1)130 HWTEST_F(TaskInnerFuncUnitTest, Stop_SubmitJob_UpdateTop, TestSize.Level1)
131 {
132 std::shared_ptr<Task> task = std::make_shared<Task>("workTask", groupId_, TaskType::SINGLETON,
133 TaskPriority::NORMAL, false);
134 std::function<int64_t()> job = []() {
135 bool runningState =true;
136 int count = 0;
137 while (runningState) {
138 count++;
139 sleep(1);
140 if (count > 10){ //10 second
141 runningState = false;
142 }
143 }
144 return 0;
145 };
146 AutoLock lock(mutex_);
147 task->RegisterJob(job);
148 task->SubmitJob(job, -1, false);
149 task->Start();
150 task->Stop();
151 sleep(1);
152 ASSERT_EQ(false, task->IsTaskRunning());
153 }
154
155 /**
156 * @tc.name: Stop_SubmitJobOnce_UpdateTop
157 * @tc.desc: UpdateTop jobQueue no empty, state is not start
158 * @tc.type: FUNC
159 */
HWTEST_F(TaskInnerFuncUnitTest, Stop_SubmitJobOnce_UpdateTop, TestSize.Level1)160 HWTEST_F(TaskInnerFuncUnitTest, Stop_SubmitJobOnce_UpdateTop, TestSize.Level1)
161 {
162 std::shared_ptr<Task> task = std::make_shared<Task>("workTask", groupId_, TaskType::SINGLETON,
163 TaskPriority::NORMAL, false);
164 std::function<int64_t()> job = []() {
165 bool runningState =true;
166 int count = 0;
167 while (runningState) {
168 count++;
169 sleep(1);
170 if (count > 10){ //10 second
171 runningState = false;
172 }
173 }
174 return 0;
175 };
176 AutoLock lock(mutex_);
177 task->RegisterJob(job);
178 task->SubmitJobOnce(job, -1, false);
179 task->Start();
180 task->Stop();
181 sleep(1);
182 ASSERT_EQ(false, task->IsTaskRunning());
183 }
184 } // namespace TaskInnerFuncUT
185 } // namespace Media
186 } // namespace OHOS