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 31using namespace std; 32using namespace Hpackage; 33using namespace Uscript; 34using namespace Updater; 35using namespace testing::ext; 36 37namespace { 38const int32_t MAX_TASK_NUMBER = 5; 39 40class ThreadPoolUnitTest : public ::testing::Test { 41public: 42 ThreadPoolUnitTest() : threadPool_(ThreadPool::CreateThreadPool(MAX_TASK_NUMBER)) {} 43 44 ~ThreadPoolUnitTest() 45 { 46 ThreadPool::Destroy(); 47 threadPool_ = nullptr; 48 } 49 50 int TestThreadPoolCreate(const int32_t taskNumber) 51 { 52 if (threadPool_ == nullptr) { 53 USCRIPT_LOGE("Fail to create thread pool"); 54 return USCRIPT_INVALID_PARAM; 55 } 56 Task task; 57 int32_t ret = USCRIPT_SUCCESS; 58 size_t taskNumberRound = 20; 59 task.workSize = taskNumber; 60 task.processor = [&](int iter) { 61 for (size_t i = iter; i < taskNumberRound; i += taskNumber) { 62 LOG(INFO) << "Run thread " << i << " " << gettid(); 63 } 64 }; 65 ThreadPool::AddTask(std::move(task)); 66 return ret; 67 } 68 69protected: 70 void SetUp() {} 71 void TearDown() {} 72 void TestBody() {} 73 74private: 75 ThreadPool* threadPool_; 76}; 77 78HWTEST_F(ThreadPoolUnitTest, TestThreadPoolCreate, TestSize.Level0) 79{ 80 ThreadPoolUnitTest test; 81 for (size_t i = 0; i < MAX_TASK_NUMBER * 2; i++) { 82 EXPECT_EQ(0, test.TestThreadPoolCreate(MAX_TASK_NUMBER + 2)); 83 } 84} 85 86HWTEST_F(ThreadPoolUnitTest, TestThreadOneCreate, TestSize.Level0) 87{ 88 ThreadPoolUnitTest test; 89 EXPECT_EQ(0, test.TestThreadPoolCreate(1)); 90} 91} 92