From fa16f0c094443f1f0a7724238dc9b50fb9e599a7 Mon Sep 17 00:00:00 2001 From: xujunyang Date: Fri, 25 Jul 2025 21:37:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=97=B6=E5=8C=BA=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E7=94=A8=E4=BE=8Bcrash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xujunyang --- .../service_test/include/time_service_test.h | 6 ++ .../service_test/src/time_proxy_test.cpp | 57 ++++++++++++++----- .../src/time_service_timer_test.cpp | 2 - 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/test/unittest/service_test/include/time_service_test.h b/test/unittest/service_test/include/time_service_test.h index 1830abad..5aa70ecd 100644 --- a/test/unittest/service_test/include/time_service_test.h +++ b/test/unittest/service_test/include/time_service_test.h @@ -32,6 +32,12 @@ void TimeOutCallback2(void) g_data2 += 1; } +int32_t TimeOutCallbackReturn(const uint64_t num) +{ + g_data1 += 1; + return 0; +} + uint64_t GetProxyKey(int uid, int pid) { uint64_t key = (static_cast(uid) << UID_PROXY_OFFSET) | static_cast(pid); diff --git a/test/unittest/service_test/src/time_proxy_test.cpp b/test/unittest/service_test/src/time_proxy_test.cpp index 96b54af6..efedcf20 100644 --- a/test/unittest/service_test/src/time_proxy_test.cpp +++ b/test/unittest/service_test/src/time_proxy_test.cpp @@ -20,6 +20,7 @@ #include "timer_proxy.h" #include "time_common.h" #include "time_service_test.h" +#include "timer_info_test.h" namespace OHOS { namespace MiscServices { @@ -27,11 +28,13 @@ using namespace testing::ext; using namespace std::chrono; namespace { -constexpr uint64_t NANO_TO_MILESECOND = 1000000; +constexpr int ONE_HUNDRED = 100; +constexpr uint64_t MICRO_TO_MILLISECOND = 1000; const uint64_t TIMER_ID = 88887; const int UID = 999996; const int PID = 999997; const int ELAPSED_REALTIME_WAKEUP = 2; + } TimerManager* timerManagerHandler_ = nullptr; @@ -85,14 +88,30 @@ uint64_t CreateTimer(int uid, int pid) uint64_t StartTimer(uint64_t timerId) { int64_t bootTime = 0; - TimeUtils::GetBootTimeNs(bootTime); - auto nowElapsed = bootTime / NANO_TO_MILESECOND; - uint64_t triggerTime = 10000000 + nowElapsed; + TimeUtils::GetBootTimeMs(bootTime); + uint64_t triggerTime = 10000000 + bootTime; auto ret = timerManagerHandler_->StartTimer(timerId, triggerTime); EXPECT_EQ(ret, TimeError::E_TIME_OK); return triggerTime; } +/** + * @brief Wait for timer trigger + * @param data the global variable that callback function changes + * @param interval the time need to wait + */ +void WaitForAlarm(std::atomic * data, int interval) +{ + int i = 0; + if (interval > 0) { + usleep(interval); + } + while (*data == 0 && i < ONE_HUNDRED) { + ++i; + usleep(MICRO_TO_MILLISECOND); + } +} + /** * @tc.name: UidTimerMap001 * @tc.desc: start a timer, it can be added into UidTimerMap @@ -151,19 +170,29 @@ HWTEST_F(TimeProxyTest, UidTimerMap002, TestSize.Level1) */ HWTEST_F(TimeProxyTest, UidTimerMap003, TestSize.Level1) { + g_data1 = 0; int32_t uid = 2000; int pid = 1000; - uint64_t timerId = CreateTimer(uid, pid); - - StartTimer(timerId); - - std::vector> triggerList; - std::shared_ptr batch = timerManagerHandler_->alarmBatches_.at(0); - std::chrono::steady_clock::time_point tpRpoch(nanoseconds(1000000000)); - batch->start_ = tpRpoch; - auto retTrigger = timerManagerHandler_->TriggerTimersLocked(triggerList, TimeUtils::GetBootTimeNs()); - EXPECT_EQ(retTrigger, true); auto uidTimersMap = TimerProxy::GetInstance().uidTimersMap_; + uidTimersMap.clear(); + TimerPara paras; + paras.timerType = ELAPSED_REALTIME_WAKEUP; + paras.windowLength = -1; + paras.interval = 0; + paras.flag = 0; + auto wantAgent = std::shared_ptr(); + uint64_t timerId = 0; + timerManagerHandler_->CreateTimer(paras, TimeOutCallbackReturn, wantAgent, uid, pid, timerId, NOT_STORE); + + int64_t bootTime = 0; + TimeUtils::GetBootTimeMs(bootTime); + uint64_t triggerTime = ONE_HUNDRED + bootTime; + auto ret = timerManagerHandler_->StartTimer(timerId, triggerTime); + EXPECT_EQ(ret, TimeError::E_TIME_OK); + uidTimersMap = TimerProxy::GetInstance().uidTimersMap_; + EXPECT_EQ(uidTimersMap.size(), (const unsigned int)1); + WaitForAlarm(&g_data1, ONE_HUNDRED * MICRO_TO_MILLISECOND); + uidTimersMap = TimerProxy::GetInstance().uidTimersMap_; EXPECT_EQ(uidTimersMap.size(), (const unsigned int)0); timerManagerHandler_->DestroyTimer(timerId); } diff --git a/test/unittest/service_test/src/time_service_timer_test.cpp b/test/unittest/service_test/src/time_service_timer_test.cpp index 036f8ff7..93a1552c 100644 --- a/test/unittest/service_test/src/time_service_timer_test.cpp +++ b/test/unittest/service_test/src/time_service_timer_test.cpp @@ -1352,8 +1352,6 @@ HWTEST_F(TimeServiceTimerTest, TimerManager004, TestSize.Level0) */ HWTEST_F(TimeServiceTimerTest, TimerManager005, TestSize.Level0) { - TimerManager::GetInstance()->NotifyWantAgentRetry(nullptr); - auto duration = std::chrono::milliseconds::zero(); auto timePoint = std::chrono::steady_clock::now(); auto timerInfo = std::make_shared("", TIMER_ID, 0, duration, timePoint, duration, timePoint, duration, -- Gitee