1 /*
2  * Copyright (c) 2023 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 #include <gtest/gtest.h>
16 #ifndef WITH_NO_MOCKER
17 #include <mockcpp/mockcpp.hpp>
18 #endif
19 #include <thread>
20 #include <climits>
21 #include <cstring>
22 #define private public
23 #define protected public
24 #include "eu/worker_manager.h"
25 #include "eu/scpuworker_manager.h"
26 #undef private
27 #undef protected
28 #include "../common.h"
29 
30 using namespace testing;
31 #ifdef HWTEST_TESTING_EXT_ENABLE
32 using namespace testing::ext;
33 #endif
34 using namespace ffrt;
35 
36 class WorkerManagerTest : public testing::Test {
37 protected:
SetUpTestCase()38     static void SetUpTestCase()
39     {
40     }
41 
TearDownTestCase()42     static void TearDownTestCase()
43     {
44     }
45 
SetUp()46     virtual void SetUp()
47     {
48     }
49 
TearDown()50     virtual void TearDown()
51     {
52     }
53 };
54 
55 HWTEST_F(WorkerManagerTest, JoinRtgTest, TestSize.Level1)
56 {
57     CPUWorkerManager* cm = new SCPUWorkerManager();
58     QoS* qos = new QoS();
59     cm->IncWorker(*qos);
60     cm->JoinRtg(*qos);
61 
62     delete qos;
63     delete cm;
64 }
65 
66 HWTEST_F(WorkerManagerTest, JoinTGTest, TestSize.Level1)
67 {
68     CPUWorkerManager* cm = new SCPUWorkerManager();
69     QoS* qos = new QoS(ffrt::qos_deadline_request);
70     ThreadGroup* tg = cm->JoinTG(*qos);
71     EXPECT_NE(tg, nullptr);
72 
73     QoS* qos1 = new QoS(ffrt::qos_user_interactive);
74     ThreadGroup* tg1 = cm->JoinTG(*qos1);
75     EXPECT_EQ(tg1, nullptr);
76 }
77 
78 HWTEST_F(WorkerManagerTest, LeaveTGTest, TestSize.Level1)
79 {
80     CPUWorkerManager* cm = new SCPUWorkerManager();
81     QoS* qos = new QoS(ffrt::qos_deadline_request);
82     cm->IncWorker(*qos);
83 #ifndef WITH_NO_MOCKER
84     MOCKER_CPP(&RTGCtrl::GetThreadGroup).stubs().will(returnValue(1));
85     MOCKER_CPP(&RTGCtrl::PutThreadGroup).stubs().will(returnValue(true));
86     MOCKER_CPP(&RTGCtrl::JoinThread).stubs().will(returnValue(true));
87     MOCKER_CPP(&RTGCtrl::RemoveThread).stubs().will(returnValue(true));
88 #endif
89     cm->JoinTG(*qos);
90     cm->LeaveTG(*qos);
91 
92     delete qos;
93     delete cm;
94 #ifndef WITH_NO_MOCKER
95     GlobalMockObject::verify();
96 #endif
97 }
98 
99 HWTEST_F(WorkerManagerTest, CPUManagerStrategyApiTest, TestSize.Level1)
100 {
101     WorkerManager* manager = new SCPUWorkerManager();
102 
103     CPUMonitor* monitor = CPUManagerStrategy::CreateCPUMonitor(QoS(2), manager);
104     EXPECT_NE(monitor, nullptr);
105     delete monitor;
106 
107     WorkerThread* worker = CPUManagerStrategy::CreateCPUWorker(QoS(2), manager);
108     EXPECT_NE(worker, nullptr);
109 
110     delete manager;
111     worker->Join();
112     delete worker;
113 }
114 
115 HWTEST_F(WorkerManagerTest, CPUWorkerStandardLoopTest, TestSize.Level1)
116 {
117     CPUWorkerManager* manager = new SCPUWorkerManager();
118 
119     CpuWorkerOps ops {
120         CPUWorker::WorkerLooperDefault,
121         std::bind(&CPUWorkerManager::PickUpTaskFromGlobalQueue, manager, std::placeholders::_1),
122         std::bind(&CPUWorkerManager::NotifyTaskPicked, manager, std::placeholders::_1),
123         std::bind(&CPUWorkerManager::WorkerIdleAction, manager, std::placeholders::_1),
124         std::bind(&CPUWorkerManager::WorkerRetired, manager, std::placeholders::_1),
125         std::bind(&CPUWorkerManager::WorkerPrepare, manager, std::placeholders::_1),
126     };
127     WorkerThread* worker = new CPUWorker(QoS(2), std::move(ops));
128     EXPECT_NE(worker, nullptr);
129     sleep(1);
130     manager->NotifyTaskAdded(QoS(2));
131 
132     delete manager;
133     worker->Join();
134     delete worker;
135 }
136