From 247735bbdde55f06854c3d62f54cc8b0fe6a50c3 Mon Sep 17 00:00:00 2001 From: hxf Date: Sun, 27 Apr 2025 20:09:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BB=B6=E8=BF=9F=E4=BB=BB=E5=8A=A1StartSA?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0hisysevent=E6=89=93=E7=82=B9=20Signed-off-by:?= =?UTF-8?q?=20hxf=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/native/include/work_conn_manager.h | 7 +- services/native/src/work_conn_manager.cpp | 4 + services/native/src/work_policy_manager.cpp | 7 +- .../native/src/work_scheduler_service.cpp | 1 + services/test/BUILD.gn | 1 + .../test/src/work_policy_manager_test.cpp | 161 ++++++++++++++++++ 6 files changed, 177 insertions(+), 4 deletions(-) diff --git a/services/native/include/work_conn_manager.h b/services/native/include/work_conn_manager.h index cdc8cbf..b753c92 100644 --- a/services/native/include/work_conn_manager.h +++ b/services/native/include/work_conn_manager.h @@ -44,13 +44,18 @@ public: * @return True if success,else false. */ bool StopWork(std::shared_ptr workStatus, bool isTimeOut); + /** + * @brief Write start work event. + * + * @param workStatus The status of work. + */ + void WriteStartWorkEvent(std::shared_ptr workStatus); private: void RemoveConnInfo(std::string &workId); void AddConnInfo(std::string &workId, sptr &connection); sptr GetConnInfo(std::string &workId); bool DisConnect(sptr connect); - void WriteStartWorkEvent(std::shared_ptr workStatus); private: ffrt::mutex connMapMutex_; diff --git a/services/native/src/work_conn_manager.cpp b/services/native/src/work_conn_manager.cpp index 2c3c0a8..a5589de 100644 --- a/services/native/src/work_conn_manager.cpp +++ b/services/native/src/work_conn_manager.cpp @@ -208,6 +208,10 @@ void WorkConnManager::WriteStartWorkEvent(shared_ptr workStatus) if (workStatus->workInfo_->GetConditionMap()->count(WorkCondition::Type::TIMER) > 0) { conditions.append("TIMER-"); } + + if (workStatus->workInfo_->GetConditionMap()->count(WorkCondition::Type::DEEP_IDLE) > 0) { + conditions.append("DEEP_IDLE-"); + } conditions.pop_back(); string type = "Repeat"; diff --git a/services/native/src/work_policy_manager.cpp b/services/native/src/work_policy_manager.cpp index a516c31..d42dee2 100644 --- a/services/native/src/work_policy_manager.cpp +++ b/services/native/src/work_policy_manager.cpp @@ -424,9 +424,9 @@ void WorkPolicyManager::CheckWorkToRun() if (!policyName.empty()) { topWork->delayReason_= policyName; - WS_HILOGI("trigger delay, reason: %{public}s, bundleName: %{public}s, runningCount:%{public}d," - " allowRunningCount:%{public}d", - policyName.c_str(), topWork->bundleName_.c_str(), runningCount, allowRunningCount); + WS_HILOGI("trigger delay, reason:%{public}s, runningCount:%{public}d allowRunningCount:%{public}d," + "bundleName:%{public}s, workId:%{public}s", policyName.c_str(), runningCount, allowRunningCount, + topWork->bundleName_.c_str(), topWork->workId_.c_str()); } SendRetrigger(DELAY_TIME_LONG); } @@ -464,6 +464,7 @@ void WorkPolicyManager::RealStartSA(std::shared_ptr topWork) } else { topWork->MarkStatus(WorkStatus::Status::WAIT_CONDITION); } + workConnManager_->WriteStartWorkEvent(topWork); return; } WS_HILOGE("startSA %{public}d workId:%{public}s failed", diff --git a/services/native/src/work_scheduler_service.cpp b/services/native/src/work_scheduler_service.cpp index 7f193d3..6c616da 100644 --- a/services/native/src/work_scheduler_service.cpp +++ b/services/native/src/work_scheduler_service.cpp @@ -778,6 +778,7 @@ int32_t WorkSchedulerService::StopWork(const WorkInfo& workInfo) WS_HILOGE("workStatus is nullptr"); return E_WORK_NOT_EXIST_FAILED; } + WS_HILOGI("StopWork %{public}s workId:%{public}d", workInfo_.GetBundleName().c_str(), workInfo_.GetWorkId()); StopWorkInner(workStatus, uid, false, false); return ERR_OK; } diff --git a/services/test/BUILD.gn b/services/test/BUILD.gn index 6fe65a2..cf31d56 100644 --- a/services/test/BUILD.gn +++ b/services/test/BUILD.gn @@ -83,6 +83,7 @@ ohos_unittest("WorkScheduleServiceTest") { "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", "ffrt:libffrt", + "googletest:gmock_main", "hilog:libhilog", "hisysevent:libhisysevent", "ipc:ipc_single", diff --git a/services/test/src/work_policy_manager_test.cpp b/services/test/src/work_policy_manager_test.cpp index ba5fe6f..fbf20cc 100644 --- a/services/test/src/work_policy_manager_test.cpp +++ b/services/test/src/work_policy_manager_test.cpp @@ -15,6 +15,7 @@ #include #include +#include "gmock/gmock.h" #include "work_scheduler_service.h" #include "work_policy_manager.h" @@ -25,6 +26,7 @@ using namespace testing::ext; +using ::testing::Return; namespace OHOS { namespace WorkScheduler { @@ -37,6 +39,15 @@ public: static std::shared_ptr workPolicyManager_; }; +class MockPolicyFilter : public IPolicyFilter { +public: + explicit MockPolicyFilter(std::shared_ptr workPolicyManager){}; + ~MockPolicyFilter(){}; + + MOCK_METHOD(int32_t, GetPolicyMaxRunning, (), (override)); + MOCK_METHOD(std::string, GetPolicyName, (), (override)); +}; + std::shared_ptr WorkPolicyManagerTest::workPolicyManager_ = nullptr; void WorkPolicyManagerTest::SetUpTestCase() @@ -722,6 +733,19 @@ HWTEST_F(WorkPolicyManagerTest, OnConditionReady_002, TestSize.Level1) EXPECT_FALSE(readyWork == nullptr); } +/** + * @tc.name: OnConditionReady_003 + * @tc.desc: Test WorkPolicyManagerTest OnConditionReady. + * @tc.type: FUNC + * @tc.require: I9J0A7 + */ +HWTEST_F(WorkPolicyManagerTest, OnConditionReady_003, TestSize.Level1) +{ + std::shared_ptr>> readyWork = nullptr; + workPolicyManager_->OnConditionReady(readyWork); + EXPECT_TRUE(readyWork == nullptr); +} + /** * @tc.name: AddToReadyQueue_001 * @tc.desc: Test WorkPolicyManagerTest AddToReadyQueue. @@ -774,6 +798,143 @@ HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_002, TestSize.Level1) EXPECT_TRUE(ret >= workPolicyManager_->dumpSetMaxRunningCount_); } +/** + * @tc.name: GetMaxRunningCount_003 + * @tc.desc: Test WorkPolicyManagerTest GetMaxRunningCount. + * @tc.type: FUNC + * @tc.require: I9J0A7 + */ +HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_003, TestSize.Level1) +{ + workPolicyManager_->policyFilters_.clear(); + std::string policyName; + workPolicyManager_->SetMaxRunningCountByDump(5); + int32_t result = workPolicyManager_->GetMaxRunningCount(policyName); + + EXPECT_EQ(5, result); + EXPECT_TRUE(policyName.empty()); +} + +/** + * @tc.name: GetMaxRunningCount_004 + * @tc.desc: Test WorkPolicyManagerTest GetMaxRunningCount. + * @tc.type: FUNC + * @tc.require: I9J0A7 + */ +HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_004, TestSize.Level1) +{ + workPolicyManager_->policyFilters_.clear(); + std::string policyName; + workPolicyManager_->SetMaxRunningCountByDump(0); + + auto filter1 = std::make_shared(workPolicyManager_); + EXPECT_CALL(*filter1, GetPolicyMaxRunning()).WillRepeatedly(Return(2)); + EXPECT_CALL(*filter1, GetPolicyName()).WillRepeatedly(Return("策略A")); + + auto filter2 = std::make_shared(workPolicyManager_); + EXPECT_CALL(*filter2, GetPolicyMaxRunning()).WillRepeatedly(Return(4)); + EXPECT_CALL(*filter2, GetPolicyName()).WillRepeatedly(Return("策略B")); + + workPolicyManager_->AddPolicyFilter(filter1); + workPolicyManager_->AddPolicyFilter(filter2); + + int32_t result = workPolicyManager_->GetMaxRunningCount(policyName); + + EXPECT_EQ(2, result); + EXPECT_EQ("策略A", policyName); +} + +/** + * @tc.name: GetMaxRunningCount_005 + * @tc.desc: Test WorkPolicyManagerTest GetMaxRunningCount. + * @tc.type: FUNC + * @tc.require: I9J0A7 + */ +HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_005, TestSize.Level1) +{ + workPolicyManager_->policyFilters_.clear(); + std::string policyName; + workPolicyManager_->SetMaxRunningCountByDump(0); + + auto filter1 = std::make_shared(workPolicyManager_); + EXPECT_CALL(*filter1, GetPolicyMaxRunning()).WillRepeatedly(Return(5)); + EXPECT_CALL(*filter1, GetPolicyName()).WillRepeatedly(Return("策略X")); + + auto filter2 = std::make_shared(workPolicyManager_); + EXPECT_CALL(*filter2, GetPolicyMaxRunning()).WillRepeatedly(Return(3)); + EXPECT_CALL(*filter2, GetPolicyName()).WillRepeatedly(Return("策略Y")); + + auto filter3 = std::make_shared(workPolicyManager_); + EXPECT_CALL(*filter3, GetPolicyMaxRunning()).WillRepeatedly(Return(7)); + EXPECT_CALL(*filter3, GetPolicyName()).WillRepeatedly(Return("策略Z")); + + workPolicyManager_->AddPolicyFilter(filter1); + workPolicyManager_->AddPolicyFilter(filter2); + workPolicyManager_->AddPolicyFilter(filter3); + + int32_t result = workPolicyManager_->GetMaxRunningCount(policyName); + + EXPECT_EQ(3, result); + EXPECT_TRUE(policyName.empty()); +} + +/** + * @tc.name: GetMaxRunningCount_006 + * @tc.desc: Test WorkPolicyManagerTest GetMaxRunningCount. + * @tc.type: FUNC + * @tc.require: I9J0A7 + */ +HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_006, TestSize.Level1) +{ + workPolicyManager_->policyFilters_.clear(); + std::string policyName; + workPolicyManager_->SetMaxRunningCountByDump(0); + + int32_t result = workPolicyManager_->GetMaxRunningCount(policyName); + + EXPECT_EQ(3, result); + EXPECT_TRUE(policyName.empty()); +} + +/** + * @tc.name: GetMaxRunningCount_007 + * @tc.desc: Test WorkPolicyManagerTest GetMaxRunningCount. + * @tc.type: FUNC + * @tc.require: I9J0A7 + */ +HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_007, TestSize.Level1) +{ + workPolicyManager_->policyFilters_.clear(); + std::string policyName; + workPolicyManager_->SetMaxRunningCountByDump(0); + + auto filter1 = std::make_shared(workPolicyManager_); + EXPECT_CALL(*filter1, GetPolicyMaxRunning()).WillRepeatedly(Return(10)); + EXPECT_CALL(*filter1, GetPolicyName()).WillRepeatedly(Return("策略1")); + + auto filter2 = std::make_shared(workPolicyManager_); + EXPECT_CALL(*filter2, GetPolicyMaxRunning()).WillRepeatedly(Return(5)); + EXPECT_CALL(*filter2, GetPolicyName()).WillRepeatedly(Return("策略2")); + + auto filter3 = std::make_shared(workPolicyManager_); + EXPECT_CALL(*filter3, GetPolicyMaxRunning()).WillRepeatedly(Return(8)); + EXPECT_CALL(*filter3, GetPolicyName()).WillRepeatedly(Return("策略3")); + + auto filter4 = std::make_shared(workPolicyManager_); + EXPECT_CALL(*filter4, GetPolicyMaxRunning()).WillRepeatedly(Return(3)); + EXPECT_CALL(*filter4, GetPolicyName()).WillRepeatedly(Return("策略4")); + + workPolicyManager_->AddPolicyFilter(filter1); + workPolicyManager_->AddPolicyFilter(filter2); + workPolicyManager_->AddPolicyFilter(filter3); + workPolicyManager_->AddPolicyFilter(filter4); + + int32_t result = workPolicyManager_->GetMaxRunningCount(policyName); + + EXPECT_EQ(3, result); + EXPECT_TRUE(policyName.empty()); +} + /** * @tc.name: GetRunningCount_001 * @tc.desc: Test WorkPolicyManagerTest GetRunningCount. -- Gitee