1 /*
2 * Copyright (c) 2025 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 "common_components/tests/test_helper.h"
17 #include "common_components/taskpool/runner.h"
18
19 #include <atomic>
20 #include <mutex>
21 #include <thread>
22 #include <vector>
23
24 namespace common {
25 class RunnerTest : public common::test::BaseTestWithScope {
26 protected:
SetUp()27 void SetUp() override {}
TearDown()28 void TearDown() override {}
29
CreateMockPrologueHook()30 static std::function<void(native_handle_type)> CreateMockPrologueHook()
31 {
32 return [](native_handle_type handle) {};
33 }
34
CreateMockEpilogueHook()35 static std::function<void(native_handle_type)> CreateMockEpilogueHook()
36 {
37 return [](native_handle_type handle) {};
38 }
39 };
40
41 class MockTask : public Task {
42 public:
MockTask(int32_t id)43 explicit MockTask(int32_t id)
44 : Task(id), executed_(false) {}
45
Run(uint32_t threadId)46 bool Run(uint32_t threadId) override
47 {
48 executed_ = true;
49 return true;
50 }
51
IsExecuted() const52 bool IsExecuted() const { return executed_; }
53
54 private:
55 std::atomic<bool> executed_;
56 };
57
HWTEST_F_L0(RunnerTest,InitializeRunnerWithThreads)58 HWTEST_F_L0(RunnerTest, InitializeRunnerWithThreads) {
59 constexpr uint32_t threadNum = 4;
60 Runner runner(threadNum, RunnerTest::CreateMockPrologueHook(), RunnerTest::CreateMockEpilogueHook());
61 EXPECT_EQ(runner.GetTotalThreadNum(), threadNum);
62
63 runner.TerminateThread();
64 }
65 }