From bf1ced7db727f68c9e03bb0bcca176475083b0c4 Mon Sep 17 00:00:00 2001 From: honglulu Date: Thu, 3 Jul 2025 17:55:56 +0800 Subject: [PATCH] =?UTF-8?q?function=E4=B8=BA=E7=A9=BA=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E5=B4=A9=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: h60047265 --- .../native/source/samgr_time_handler.cpp | 4 +++ .../src/device_timed_collect_test.cpp | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/services/samgr/native/source/samgr_time_handler.cpp b/services/samgr/native/source/samgr_time_handler.cpp index d0b97552..db5ed262 100644 --- a/services/samgr/native/source/samgr_time_handler.cpp +++ b/services/samgr/native/source/samgr_time_handler.cpp @@ -109,6 +109,10 @@ int SamgrTimeHandler::CreateAndRetry() bool SamgrTimeHandler::PostTask(TaskType func, uint64_t delayTime) { + if (!func) { + HILOGE("SamgrTimeHandler PostTask failed, func is null"); + return false; + } HILOGI("SamgrTimeHandler postTask start: %{public}" PRId64 "s", delayTime); int timerfd = CreateAndRetry(); if (timerfd == -1) { diff --git a/services/samgr/native/test/unittest/src/device_timed_collect_test.cpp b/services/samgr/native/test/unittest/src/device_timed_collect_test.cpp index cd383149..20f84a4a 100644 --- a/services/samgr/native/test/unittest/src/device_timed_collect_test.cpp +++ b/services/samgr/native/test/unittest/src/device_timed_collect_test.cpp @@ -21,6 +21,7 @@ #define private public #include "device_status_collect_manager.h" #include "device_timed_collect.h" +#include "samgr_time_handler.h" #ifdef PREFERENCES_ENABLE #include "preferences_errno.h" #include "preferences_helper.h" @@ -983,4 +984,35 @@ HWTEST_F(DeviceTimedCollectTest, TestFFRTHandlerPostTask003, TestSize.Level3) EXPECT_FALSE(collectHandler->PostTask(func, "test", 1)); DTEST_LOG << " TestFFRTHandlerPostTask003 end" << std::endl; } + +/** + * @tc.name: SamgrTimeHandlerTest001 + * @tc.desc: test SamgrTimeHandler PostTask, with func is null + * @tc.type: FUNC + * @tc.require: I7VZ98 + */ + +HWTEST_F(DeviceTimedCollectTest, SamgrTimeHandlerTest001, TestSize.Level3) +{ + DTEST_LOG << " SamgrTimeHandlerTest001 begin" << std::endl; + std::function timedTask = nullptr; + bool bRet = SamgrTimeHandler::GetInstance()->PostTask(timedTask, 1); + EXPECT_FALSE(bRet); + DTEST_LOG << " SamgrTimeHandlerTest001 end" << std::endl; +} + +/** + * @tc.name: SamgrTimeHandlerTest002 + * @tc.desc: test SamgrTimeHandler PostTask, with func is not null + * @tc.type: FUNC + * @tc.require: I7VZ98 + */ +HWTEST_F(DeviceTimedCollectTest, SamgrTimeHandlerTest002, TestSize.Level3) +{ + DTEST_LOG << " SamgrTimeHandlerTest002 begin" << std::endl; + std::function timedTask = [] () {}; + bool bRet = SamgrTimeHandler::GetInstance()->PostTask(timedTask, 1); + EXPECT_TRUE(bRet); + DTEST_LOG << " SamgrTimeHandlerTest002 end" << std::endl; +} } \ No newline at end of file -- Gitee