• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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<cstring>
17 #include<string>
18 
19 #include "gtest/gtest.h"
20 
21 #include "platform/threadpool/include/thread.h"
22 #include "platform/threadpool/include/thread_pool.h"
23 #include "platform/time/include/time.h"
24 #include "utils/log/aie_log.h"
25 
26 using namespace OHOS::AI;
27 using namespace std;
28 using namespace testing::ext;
29 
30 namespace {
31     const int STOP_TIME = 100;
32     const time_t HUNG_TIME = 15;
33     const int SIZE = 70000;
34     const int SIZE_MIN = 0;
35     const int INVALID_THREAD_ID = -1;
36     const string WORKER_DEFAULT_NAME("WorkerDefaultName");
37 }
38 
39 class ThreadPoolTest : public testing::Test {
40 public:
41     // SetUpTestCase:The preset action of the test suite is executed before the first TestCase
SetUpTestCase()42     static void SetUpTestCase() {};
43 
44     // TearDownTestCase:The test suite cleanup action is executed after the last TestCase
TearDownTestCase()45     static void TearDownTestCase() {};
46 
47     // SetUp:Execute before each test case
SetUp()48     void SetUp() {};
49 
50     // TearDown:Execute after each test case
TearDown()51     void TearDown() {};
52 };
53 
54 class CWorker : public IWorker {
55 public:
GetName() const56     const char *GetName() const override
57     {
58         return "CWorker";
59     }
60 
OneAction()61     bool OneAction() override
62     {
63         StepSleepMs(THREAD_SLEEP_MS);
64         HILOGD("[Test]Worker OneAction after sleep");
65         return true;
66     }
67 
Initialize()68     bool Initialize() override
69     {
70         HILOGD("[Test]Worker Initialize success");
71         return true;
72     }
73 };
74 
75 class MultiWorker : public IWorker {
76 public:
MultiWorker(const string & workerName)77     explicit MultiWorker(const string &workerName) : workerName_(workerName)
78     {
79     }
80 
81     ~MultiWorker() override = default;
82 
GetName() const83     const char *GetName() const override
84     {
85         const char *name = workerName_.c_str();
86         return name;
87     }
88 
OneAction()89     bool OneAction() override
90     {
91         StepSleepMs(THREAD_SLEEP_MS);
92         HILOGD("[Test]Worker(%s) OneAction success.", GetName());
93         return true;
94     }
95 
Initialize()96     bool Initialize() override
97     {
98         HILOGD("[Test]Worker(%s) Initialize success.", GetName());
99         return true;
100     }
101 
Uninitialize()102     void Uninitialize() override
103     {
104         HILOGD("[Test]Worker(%s) Uninitialize success.", GetName());
105     }
106 
107 private:
108     string workerName_ = WORKER_DEFAULT_NAME;
109 };
110 
111 /**
112  * @tc.name: TestWorker001
113  * @tc.desc: Test worker Initialize/GetStackSize/Status/GetThreadId functions.
114  * @tc.type: FUNC
115  * @tc.require: AR000F77TL
116  */
117 HWTEST_F(ThreadPoolTest, TestWorker001, TestSize.Level1)
118 {
119     HILOGD("[Test]Test worker begin");
120     CWorker worker;
121     worker.Initialize();
122     ASSERT_FALSE(worker.isHung(HUNG_TIME));
123     ASSERT_EQ(worker.GetStackSize(), THREAD_DEFAULT_STACK_SIZE);
124     ASSERT_EQ(worker.Status(), 0);
125     ASSERT_EQ(static_cast<int>(worker.GetThreadId()), INVALID_THREAD_ID);
126     Thread thread;
127     worker.SetThread(&thread);
128     HILOGD("[Test]Test worker end, GetThreadId is %lu.", worker.GetThreadId());
129 }
130 
131 /**
132  * @tc.name: TestThread001
133  * @tc.desc: Test thread StartThread/IsRunning/IsActive/StopThread functions.
134  * @tc.type: FUNC
135  * @tc.require: AR000F77TL
136  */
137 HWTEST_F(ThreadPoolTest, TestThread001, TestSize.Level1)
138 {
139     HILOGD("[Test]Test thread1 begin");
140     Thread testThread;
141     unsigned long id = testThread.GetThreadId();
142     HILOGD("[Test]Thread id %lu.", id);
143     testThread.SetStackSize(SIZE);
144     CWorker worker;
145     ASSERT_TRUE(testThread.StartThread(&worker));
146     id = testThread.GetThreadId();
147     HILOGD("[Test]Thread id %lu.", id);
148     ASSERT_TRUE(testThread.IsRunning());
149     ASSERT_TRUE(testThread.IsActive());
150     ASSERT_TRUE(testThread.StopThread(STOP_TIME));
151     ASSERT_FALSE(testThread.IsRunning());
152     ASSERT_FALSE(testThread.IsActive());
153     HILOGD("[Test]Test thread1 end.");
154 }
155 
156 /**
157  * @tc.name: ThreadPoolTest001
158  * @tc.desc: Test ThreadPool SetStackSize/getStackSize functions.
159  * @tc.type: FUNC
160  * @tc.require: AR000F77TL
161  */
162 HWTEST_F(ThreadPoolTest, ThreadPoolTest001, TestSize.Level1)
163 {
164     HILOGD("[Test]ThreadPoolTest001 begin");
165     ThreadPool *threadPool = ThreadPool::GetInstance();
166     ASSERT_EQ(threadPool->getStackSize(), 0);
167     threadPool->SetStackSize(SIZE);
168     ASSERT_EQ(threadPool->getStackSize(), SIZE);
169     threadPool->SetStackSize(SIZE_MIN);
170     HILOGD("[Test]ThreadPoolTest001 end");
171 }
172 
173 /**
174  * @tc.name: ThreadPoolTest002
175  * @tc.desc: Test ThreadPool Pop method.
176  * @tc.type: FUNC
177  * @tc.require: AR000F77TL
178  */
179 HWTEST_F(ThreadPoolTest, ThreadPoolTest002, TestSize.Level1)
180 {
181     HILOGD("[Test]ThreadPoolTest002 begin");
182     ThreadPool *threadPool = ThreadPool::GetInstance();
183     ASSERT_NE(threadPool, nullptr);
184     std::shared_ptr<Thread> oneThread = threadPool->Pop();
185     ASSERT_NE(oneThread, nullptr);
186     HILOGD("[Test]ThreadPoolTest002 end");
187 }
188 
189 /**
190  * @tc.name: ThreadPoolTest003
191  * @tc.desc: Test ThreadPool Push method.
192  * @tc.type: FUNC
193  * @tc.require: AR000F77TL
194  */
195 HWTEST_F(ThreadPoolTest, ThreadPoolTest003, TestSize.Level1)
196 {
197     HILOGD("[Test]ThreadPoolTest003 begin");
198     ThreadPool *threadPool = ThreadPool::GetInstance();
199     ASSERT_NE(threadPool, nullptr);
200     std::shared_ptr<Thread> oneThread = threadPool->Pop();
201     threadPool->Push(oneThread);
202     HILOGD("[Test]ThreadPoolTest003 end");
203 }
204 
205 /**
206  * @tc.name: TestThreadWithManyWorker001
207  * @tc.desc: Test pop/push thread, and start/stop thread
208  * @tc.type: FUNC
209  * @tc.require: AR000F77TL
210  */
211 HWTEST_F(ThreadPoolTest, TestThreadWithManyWorker001, TestSize.Level1)
212 {
213     HILOGD("[Test]Test worker begin");
214     string workerName("MultiWorker");
215     int maxThreadNum = 10;
216     MultiWorker *workerList[maxThreadNum];
217     std::shared_ptr<Thread> threadList[maxThreadNum];
218     ThreadPool *threadPool = ThreadPool::GetInstance();
219     ASSERT_NE(threadPool, nullptr);
220     for (int i = 0; i < maxThreadNum; i++) {
221         string tempWorkerName = workerName + to_string(i);
222         AIE_NEW(workerList[i], MultiWorker(tempWorkerName));
223         threadList[i] = threadPool->Pop();
224         ASSERT_TRUE(threadList[i]->StartThread(workerList[i]));
225     }
226     for (int i = 0; i < maxThreadNum; i++) {
227         threadList[i]->StopThread();
228         threadPool->Push(threadList[i]);
229         AIE_DELETE(workerList[i]);
230     }
231     threadPool->ReleaseInstance();
232 }
233