diff --git a/test/unittest/service_test/include/time_service_test.h b/test/unittest/service_test/include/time_service_test.h index 1830abad0b58c37039e5c005fdab8c7f13b9122a..5aa70ecde9aee993ffc078e1e4e98823063ae7aa 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 96b54af6335d75c3460f64399eed0d7f993f5c27..efedcf201a82ef937222cee9dea2b89bc5702d6d 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 036f8ff7be7e7db30943f923ccec43bbefaad967..93a1552c940616d32f1d203c8bb77c644bba20b0 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,