• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/taskpool.h"
18 #include "common_components/taskpool/task.h"
19 
20 #include <atomic>
21 #include <chrono>
22 #include <thread>
23 
24 namespace common {
25 
26 class MockTask : public Task {
27 public:
MockTask(int32_t id)28     explicit MockTask(int32_t id)
29         : Task(id), executed_(false), terminated_(false) {}
30 
Run(uint32_t threadId)31     bool Run(uint32_t threadId) override
32     {
33         executed_ = true;
34         return true;
35     }
36 
IsExecuted() const37     bool IsExecuted() const { return executed_; }
38 
IsTerminate() const39     bool IsTerminate() const
40     {
41         return terminated_;
42     }
43 
Terminate()44     void Terminate()
45     {
46         terminated_ = true;
47     }
48 
49 private:
50     std::atomic<bool> executed_;
51     std::atomic<bool> terminated_;
52 };
53 
54 class TaskpoolTest : public common::test::BaseTestWithScope {
55 protected:
SetUp()56     void SetUp() override {}
TearDown()57     void TearDown() override {}
58 
59     class ScopedTaskpool {
60     public:
ScopedTaskpool(int threadNum=DEFAULT_TASKPOOL_THREAD_NUM)61         explicit ScopedTaskpool(int threadNum = DEFAULT_TASKPOOL_THREAD_NUM)
62             : isInitialized_(false)
63         {
64             isInitialized_ = true;
65             pool_.Initialize(threadNum);
66         }
67 
Get()68         Taskpool* Get()
69         {
70             return &pool_;
71         }
72 
73     private:
74         Taskpool pool_;
75         bool isInitialized_;
76     };
77 };
78 
HWTEST_F_L0(TaskpoolTest,InitializeAndDestroy)79 HWTEST_F_L0(TaskpoolTest, InitializeAndDestroy) {
80     TaskpoolTest::ScopedTaskpool pool(2);
81     EXPECT_NE(pool.Get(), nullptr);
82     EXPECT_GT(pool.Get()->GetTotalThreadNum(), 0U);
83 }
84 
HWTEST_F_L0(TaskpoolTest,TerminateTask)85 HWTEST_F_L0(TaskpoolTest, TerminateTask) {
86     TaskpoolTest::ScopedTaskpool pool(2);
87     Taskpool* taskpool = pool.Get();
88     ASSERT_NE(taskpool, nullptr);
89 
90     auto task1 = std::make_unique<MockTask>(1);
91     auto task2 = std::make_unique<MockTask>(2);
92     taskpool->PostTask(std::move(task1));
93     taskpool->PostTask(std::move(task2));
94 
95     std::this_thread::sleep_for(std::chrono::milliseconds(100));
96 
97     taskpool->TerminateTask(1, TaskType::ALL);
98 
99     bool isTerminated = false;
100     taskpool->ForEachTask([&isTerminated](Task* task) {
101         if (task->GetId() == 1 && task->IsTerminate()) {
102             isTerminated = true;
103         }
104     });
105 
106     EXPECT_FALSE(isTerminated);
107 }
108 }