• 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/task_queue.h"
18 #include "common_components/taskpool/task.h"
19 
20 #include <thread>
21 #include <chrono>
22 
23 namespace common {
24 
25 class TaskQueueTest : public common::test::BaseTestWithScope {
26 protected:
SetUp()27     void SetUp() override
28     {
29         queue_ = new TaskQueue();
30     }
31 
TearDown()32     void TearDown() override
33     {
34         delete queue_;
35         queue_ = nullptr;
36     }
37 
38     TaskQueue* queue_;
39 };
40 
41 class MockTask : public Task {
42 public:
MockTask(int id)43     explicit MockTask(int id) : Task(id), executed_(false) {}
44 
Run(uint32_t threadId)45     bool Run(uint32_t threadId) override
46     {
47         executed_ = true;
48         return true;
49     }
50 
IsExecuted() const51     bool IsExecuted() const { return executed_; }
52 
53 private:
54     mutable bool executed_;
55 };
56 
57 
HWTEST_F_L0(TaskQueueTest,PopTask_DelayedTaskExpired_CanBeExecuted)58 HWTEST_F_L0(TaskQueueTest, PopTask_DelayedTaskExpired_CanBeExecuted)
59 {
60     auto task = std::make_unique<MockTask>(2);
61     queue_->PostDelayedTask(std::move(task), 500);
62 
63     usleep(600 * 1000);
64 
65     auto poppedTask = queue_->PopTask();
66     ASSERT_NE(poppedTask, nullptr);
67 
68     MockTask* mockTask = static_cast<MockTask*>(poppedTask.get());
69     EXPECT_FALSE(mockTask->IsExecuted());
70 
71     (void)poppedTask->Run(0);
72     EXPECT_TRUE(mockTask->IsExecuted());
73 }
74 
HWTEST_F_L0(TaskQueueTest,PopTask_MultipleDelayedTasks_ExecuteInOrder)75 HWTEST_F_L0(TaskQueueTest, PopTask_MultipleDelayedTasks_ExecuteInOrder)
76 {
77     auto task1 = std::make_unique<MockTask>(1);
78     auto task2 = std::make_unique<MockTask>(2);
79     auto task3 = std::make_unique<MockTask>(3);
80 
81     queue_->PostDelayedTask(std::move(task1), 800);
82     queue_->PostDelayedTask(std::move(task2), 500);
83     queue_->PostDelayedTask(std::move(task3), 1000);
84 
85     usleep(600 * 1000);
86 
87     auto poppedTask = queue_->PopTask();
88     ASSERT_NE(poppedTask, nullptr);
89     EXPECT_EQ(poppedTask->GetId(), 2);
90 
91     poppedTask->Run(0);
92     MockTask* mockTask2 = static_cast<MockTask*>(poppedTask.get());
93     EXPECT_TRUE(mockTask2->IsExecuted());
94 
95     usleep(300 * 1000);
96 
97     poppedTask = queue_->PopTask();
98     ASSERT_NE(poppedTask, nullptr);
99     EXPECT_EQ(poppedTask->GetId(), 1);
100 
101     poppedTask->Run(0);
102     MockTask* mockTask1 = static_cast<MockTask*>(poppedTask.get());
103     EXPECT_TRUE(mockTask1->IsExecuted());
104 
105     usleep(200 * 1000);
106 
107     poppedTask = queue_->PopTask();
108     ASSERT_NE(poppedTask, nullptr);
109     EXPECT_EQ(poppedTask->GetId(), 3);
110 
111     poppedTask->Run(0);
112     MockTask* mockTask3 = static_cast<MockTask*>(poppedTask.get());
113     EXPECT_TRUE(mockTask3->IsExecuted());
114 }
115 }