1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci 16fb299fa2Sopenharmony_ci#include <cstring> 17fb299fa2Sopenharmony_ci#include <fcntl.h> 18fb299fa2Sopenharmony_ci#include <gtest/gtest.h> 19fb299fa2Sopenharmony_ci#include <iostream> 20fb299fa2Sopenharmony_ci#include <sys/mman.h> 21fb299fa2Sopenharmony_ci#include <sys/stat.h> 22fb299fa2Sopenharmony_ci#include <unistd.h> 23fb299fa2Sopenharmony_ci#include "log.h" 24fb299fa2Sopenharmony_ci#include "pkg_algorithm.h" 25fb299fa2Sopenharmony_ci#include "script_instruction.h" 26fb299fa2Sopenharmony_ci#include "script_manager.h" 27fb299fa2Sopenharmony_ci#include "script_utils.h" 28fb299fa2Sopenharmony_ci#include "thread_pool.h" 29fb299fa2Sopenharmony_ci#include "unittest_comm.h" 30fb299fa2Sopenharmony_ci 31fb299fa2Sopenharmony_ciusing namespace std; 32fb299fa2Sopenharmony_ciusing namespace Hpackage; 33fb299fa2Sopenharmony_ciusing namespace Uscript; 34fb299fa2Sopenharmony_ciusing namespace Updater; 35fb299fa2Sopenharmony_ciusing namespace testing::ext; 36fb299fa2Sopenharmony_ci 37fb299fa2Sopenharmony_cinamespace { 38fb299fa2Sopenharmony_ciconst int32_t MAX_TASK_NUMBER = 5; 39fb299fa2Sopenharmony_ci 40fb299fa2Sopenharmony_ciclass ThreadPoolUnitTest : public ::testing::Test { 41fb299fa2Sopenharmony_cipublic: 42fb299fa2Sopenharmony_ci ThreadPoolUnitTest() : threadPool_(ThreadPool::CreateThreadPool(MAX_TASK_NUMBER)) {} 43fb299fa2Sopenharmony_ci 44fb299fa2Sopenharmony_ci ~ThreadPoolUnitTest() 45fb299fa2Sopenharmony_ci { 46fb299fa2Sopenharmony_ci ThreadPool::Destroy(); 47fb299fa2Sopenharmony_ci threadPool_ = nullptr; 48fb299fa2Sopenharmony_ci } 49fb299fa2Sopenharmony_ci 50fb299fa2Sopenharmony_ci int TestThreadPoolCreate(const int32_t taskNumber) 51fb299fa2Sopenharmony_ci { 52fb299fa2Sopenharmony_ci if (threadPool_ == nullptr) { 53fb299fa2Sopenharmony_ci USCRIPT_LOGE("Fail to create thread pool"); 54fb299fa2Sopenharmony_ci return USCRIPT_INVALID_PARAM; 55fb299fa2Sopenharmony_ci } 56fb299fa2Sopenharmony_ci Task task; 57fb299fa2Sopenharmony_ci int32_t ret = USCRIPT_SUCCESS; 58fb299fa2Sopenharmony_ci size_t taskNumberRound = 20; 59fb299fa2Sopenharmony_ci task.workSize = taskNumber; 60fb299fa2Sopenharmony_ci task.processor = [&](int iter) { 61fb299fa2Sopenharmony_ci for (size_t i = iter; i < taskNumberRound; i += taskNumber) { 62fb299fa2Sopenharmony_ci LOG(INFO) << "Run thread " << i << " " << gettid(); 63fb299fa2Sopenharmony_ci } 64fb299fa2Sopenharmony_ci }; 65fb299fa2Sopenharmony_ci ThreadPool::AddTask(std::move(task)); 66fb299fa2Sopenharmony_ci return ret; 67fb299fa2Sopenharmony_ci } 68fb299fa2Sopenharmony_ci 69fb299fa2Sopenharmony_ciprotected: 70fb299fa2Sopenharmony_ci void SetUp() {} 71fb299fa2Sopenharmony_ci void TearDown() {} 72fb299fa2Sopenharmony_ci void TestBody() {} 73fb299fa2Sopenharmony_ci 74fb299fa2Sopenharmony_ciprivate: 75fb299fa2Sopenharmony_ci ThreadPool* threadPool_; 76fb299fa2Sopenharmony_ci}; 77fb299fa2Sopenharmony_ci 78fb299fa2Sopenharmony_ciHWTEST_F(ThreadPoolUnitTest, TestThreadPoolCreate, TestSize.Level0) 79fb299fa2Sopenharmony_ci{ 80fb299fa2Sopenharmony_ci ThreadPoolUnitTest test; 81fb299fa2Sopenharmony_ci for (size_t i = 0; i < MAX_TASK_NUMBER * 2; i++) { 82fb299fa2Sopenharmony_ci EXPECT_EQ(0, test.TestThreadPoolCreate(MAX_TASK_NUMBER + 2)); 83fb299fa2Sopenharmony_ci } 84fb299fa2Sopenharmony_ci} 85fb299fa2Sopenharmony_ci 86fb299fa2Sopenharmony_ciHWTEST_F(ThreadPoolUnitTest, TestThreadOneCreate, TestSize.Level0) 87fb299fa2Sopenharmony_ci{ 88fb299fa2Sopenharmony_ci ThreadPoolUnitTest test; 89fb299fa2Sopenharmony_ci EXPECT_EQ(0, test.TestThreadPoolCreate(1)); 90fb299fa2Sopenharmony_ci} 91fb299fa2Sopenharmony_ci} 92