• 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 <fcntl.h>
18 #include <gtest/gtest.h>
19 #include <iostream>
20 #include <sys/mman.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include "log.h"
24 #include "pkg_algorithm.h"
25 #include "script_instruction.h"
26 #include "script_manager.h"
27 #include "script_utils.h"
28 #include "thread_pool.h"
29 #include "unittest_comm.h"
30 
31 using namespace std;
32 using namespace Hpackage;
33 using namespace Uscript;
34 using namespace Updater;
35 using namespace testing::ext;
36 
37 namespace {
38 const int32_t MAX_TASK_NUMBER = 5;
39 
40 class ThreadPoolUnitTest : public ::testing::Test {
41 public:
ThreadPoolUnitTest()42     ThreadPoolUnitTest() : threadPool_(ThreadPool::CreateThreadPool(MAX_TASK_NUMBER)) {}
43 
~ThreadPoolUnitTest()44     ~ThreadPoolUnitTest()
45     {
46         ThreadPool::Destroy();
47         threadPool_ = nullptr;
48     }
49 
TestThreadPoolCreate(const int32_t taskNumber)50     int TestThreadPoolCreate(const int32_t taskNumber)
51     {
52         USCRIPT_CHECK(threadPool_ != nullptr, return USCRIPT_INVALID_PARAM, "Fail to create thread pool");
53         Task task;
54         int32_t ret = USCRIPT_SUCCESS;
55         size_t taskNumberRound = 20;
56         task.workSize = taskNumber;
57         task.processor = [&](int iter) {
58             for (size_t i = iter; i < taskNumberRound; i += taskNumber) {
59                 LOG(INFO) << "Run thread " << i << " " << gettid();
60             }
61         };
62         ThreadPool::AddTask(std::move(task));
63         return ret;
64     }
65 
66 protected:
SetUp()67     void SetUp() {}
TearDown()68     void TearDown() {}
TestBody()69     void TestBody() {}
70 
71 private:
72     ThreadPool* threadPool_;
73 };
74 
75 HWTEST_F(ThreadPoolUnitTest, TestThreadPoolCreate, TestSize.Level0)
76 {
77     ThreadPoolUnitTest test;
78     for (size_t i = 0; i < MAX_TASK_NUMBER * 2; i++) {
79         EXPECT_EQ(0, test.TestThreadPoolCreate(MAX_TASK_NUMBER + 2));
80     }
81 }
82 
83 HWTEST_F(ThreadPoolUnitTest, TestThreadOneCreate, TestSize.Level0)
84 {
85     ThreadPoolUnitTest test;
86     EXPECT_EQ(0, test.TestThreadPoolCreate(1));
87 }
88 }
89