13f4cbf05Sopenharmony_ci/*
23f4cbf05Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License.
53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at
63f4cbf05Sopenharmony_ci *
73f4cbf05Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83f4cbf05Sopenharmony_ci *
93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and
133f4cbf05Sopenharmony_ci * limitations under the License.
143f4cbf05Sopenharmony_ci */
153f4cbf05Sopenharmony_ci
163f4cbf05Sopenharmony_ci#include <gtest/gtest.h>
173f4cbf05Sopenharmony_ci#include "timer.h"
183f4cbf05Sopenharmony_ci#include "common_timer_errors.h"
193f4cbf05Sopenharmony_ci#include <iostream>
203f4cbf05Sopenharmony_ci#include <thread>
213f4cbf05Sopenharmony_ci#include <chrono>
223f4cbf05Sopenharmony_ci#include <stdatomic.h>
233f4cbf05Sopenharmony_ci#include <sys/time.h>
243f4cbf05Sopenharmony_ciusing namespace testing::ext;
253f4cbf05Sopenharmony_ciusing namespace std;
263f4cbf05Sopenharmony_ci
273f4cbf05Sopenharmony_cinamespace OHOS {
283f4cbf05Sopenharmony_cinamespace {
293f4cbf05Sopenharmony_ciint64_t CurMs()
303f4cbf05Sopenharmony_ci{
313f4cbf05Sopenharmony_ci    struct timeval tpend;
323f4cbf05Sopenharmony_ci    gettimeofday(&tpend, nullptr);
333f4cbf05Sopenharmony_ci    return (tpend.tv_sec * 1000000 + tpend.tv_usec) / 1000;
343f4cbf05Sopenharmony_ci}
353f4cbf05Sopenharmony_ci
363f4cbf05Sopenharmony_ciclass UtilsTimerTest : public testing::Test {
373f4cbf05Sopenharmony_cipublic :
383f4cbf05Sopenharmony_ci    static void SetUpTestCase(void);
393f4cbf05Sopenharmony_ci    static void TearDownTestCase(void);
403f4cbf05Sopenharmony_ci    void SetUp();
413f4cbf05Sopenharmony_ci    void TearDown();
423f4cbf05Sopenharmony_ci};
433f4cbf05Sopenharmony_ci
443f4cbf05Sopenharmony_civoid UtilsTimerTest::SetUpTestCase(void)
453f4cbf05Sopenharmony_ci{
463f4cbf05Sopenharmony_ci}
473f4cbf05Sopenharmony_ci
483f4cbf05Sopenharmony_civoid UtilsTimerTest::TearDownTestCase(void)
493f4cbf05Sopenharmony_ci{
503f4cbf05Sopenharmony_ci}
513f4cbf05Sopenharmony_ci
523f4cbf05Sopenharmony_civoid UtilsTimerTest::SetUp(void)
533f4cbf05Sopenharmony_ci{
543f4cbf05Sopenharmony_ci}
553f4cbf05Sopenharmony_ci
563f4cbf05Sopenharmony_civoid UtilsTimerTest::TearDown(void)
573f4cbf05Sopenharmony_ci{
583f4cbf05Sopenharmony_ci}
593f4cbf05Sopenharmony_ci
603f4cbf05Sopenharmony_cistd::atomic<int> g_data1(0);
613f4cbf05Sopenharmony_civoid TimeOutCallback1()
623f4cbf05Sopenharmony_ci{
633f4cbf05Sopenharmony_ci    g_data1 = g_data1 + 1;
643f4cbf05Sopenharmony_ci}
653f4cbf05Sopenharmony_ci
663f4cbf05Sopenharmony_cistd::atomic<int> g_data2(0);
673f4cbf05Sopenharmony_civoid TimeOutCallback2()
683f4cbf05Sopenharmony_ci{
693f4cbf05Sopenharmony_ci    g_data2 = g_data2 + 1;
703f4cbf05Sopenharmony_ci}
713f4cbf05Sopenharmony_ci
723f4cbf05Sopenharmony_ci/*
733f4cbf05Sopenharmony_ci * @tc.name: testTimer001
743f4cbf05Sopenharmony_ci * @tc.desc: timer unit test
753f4cbf05Sopenharmony_ci *
763f4cbf05Sopenharmony_ci * temporarily offline for kernel difference
773f4cbf05Sopenharmony_ciHWTEST_F(UtilsTimerTest, testTimer001, TestSize.Level0)
783f4cbf05Sopenharmony_ci{
793f4cbf05Sopenharmony_ci    g_data1 = 0;
803f4cbf05Sopenharmony_ci    Utils::Timer timer("test_timer");
813f4cbf05Sopenharmony_ci    uint32_t ret = timer.Setup();
823f4cbf05Sopenharmony_ci    EXPECT_EQ(Utils::TIMER_ERR_OK, ret);
833f4cbf05Sopenharmony_ci    uint32_t timerId = timer.Register(TimeOutCallback1, 1);
843f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(8));
853f4cbf05Sopenharmony_ci    timer.Unregister(timerId);
863f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(10));
873f4cbf05Sopenharmony_ci    timer.Shutdown();
883f4cbf05Sopenharmony_ci    EXPECT_GE(g_data1, 2);
893f4cbf05Sopenharmony_ci    EXPECT_GE(10, g_data1);
903f4cbf05Sopenharmony_ci*/
913f4cbf05Sopenharmony_ci
923f4cbf05Sopenharmony_ci/*
933f4cbf05Sopenharmony_ci * @tc.name: testTimer002
943f4cbf05Sopenharmony_ci * @tc.desc: timer unit test
953f4cbf05Sopenharmony_ci */
963f4cbf05Sopenharmony_ciHWTEST_F(UtilsTimerTest, testTimer002, TestSize.Level0)
973f4cbf05Sopenharmony_ci{
983f4cbf05Sopenharmony_ci    g_data1 = 0;
993f4cbf05Sopenharmony_ci    Utils::Timer timer("test_timer");
1003f4cbf05Sopenharmony_ci    uint32_t ret = timer.Setup();
1013f4cbf05Sopenharmony_ci    EXPECT_EQ(Utils::TIMER_ERR_OK, ret);
1023f4cbf05Sopenharmony_ci    timer.Register(TimeOutCallback1, 1, true);
1033f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(15));
1043f4cbf05Sopenharmony_ci    timer.Shutdown();
1053f4cbf05Sopenharmony_ci    EXPECT_EQ(1, g_data1);
1063f4cbf05Sopenharmony_ci}
1073f4cbf05Sopenharmony_ci
1083f4cbf05Sopenharmony_ci/*
1093f4cbf05Sopenharmony_ci * @tc.name: testTimer003
1103f4cbf05Sopenharmony_ci * @tc.desc: timer unit test
1113f4cbf05Sopenharmony_ci */
1123f4cbf05Sopenharmony_ciHWTEST_F(UtilsTimerTest, testTimer003, TestSize.Level0)
1133f4cbf05Sopenharmony_ci{
1143f4cbf05Sopenharmony_ci    g_data1 = 0;
1153f4cbf05Sopenharmony_ci    g_data2 = 0;
1163f4cbf05Sopenharmony_ci    Utils::Timer timer("test_timer");
1173f4cbf05Sopenharmony_ci    uint32_t ret = timer.Setup();
1183f4cbf05Sopenharmony_ci    EXPECT_EQ(Utils::TIMER_ERR_OK, ret);
1193f4cbf05Sopenharmony_ci    timer.Register(TimeOutCallback1, 1);
1203f4cbf05Sopenharmony_ci    timer.Register(TimeOutCallback2, 50);
1213f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(500));
1223f4cbf05Sopenharmony_ci    timer.Shutdown();
1233f4cbf05Sopenharmony_ci    EXPECT_GE(g_data1, 8);
1243f4cbf05Sopenharmony_ci    EXPECT_GE(g_data2, 2);
1253f4cbf05Sopenharmony_ci}
1263f4cbf05Sopenharmony_ci
1273f4cbf05Sopenharmony_cistatic void TestTimerEvent(Utils::Timer& timer)
1283f4cbf05Sopenharmony_ci{
1293f4cbf05Sopenharmony_ci    uint32_t interval = 1;
1303f4cbf05Sopenharmony_ci    timer.Register(TimeOutCallback1, interval);
1313f4cbf05Sopenharmony_ci    uint32_t interval2 = 2;
1323f4cbf05Sopenharmony_ci    timer.Register(TimeOutCallback1, interval2);
1333f4cbf05Sopenharmony_ci    int sleepTime = 30;
1343f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
1353f4cbf05Sopenharmony_ci    timer.Shutdown();
1363f4cbf05Sopenharmony_ci}
1373f4cbf05Sopenharmony_ci
1383f4cbf05Sopenharmony_ci/*
1393f4cbf05Sopenharmony_ci * @tc.name: testTimer004
1403f4cbf05Sopenharmony_ci * @tc.desc: timer unit test
1413f4cbf05Sopenharmony_ci */
1423f4cbf05Sopenharmony_ciHWTEST_F(UtilsTimerTest, testTimer004, TestSize.Level0)
1433f4cbf05Sopenharmony_ci{
1443f4cbf05Sopenharmony_ci    g_data1 = 0;
1453f4cbf05Sopenharmony_ci    Utils::Timer timer("test_timer");
1463f4cbf05Sopenharmony_ci    uint32_t ret = timer.Setup();
1473f4cbf05Sopenharmony_ci    EXPECT_EQ(Utils::TIMER_ERR_OK, ret);
1483f4cbf05Sopenharmony_ci    TestTimerEvent(timer);
1493f4cbf05Sopenharmony_ci    EXPECT_GE(g_data1, 5);
1503f4cbf05Sopenharmony_ci}
1513f4cbf05Sopenharmony_ci
1523f4cbf05Sopenharmony_ciclass A {
1533f4cbf05Sopenharmony_cipublic:
1543f4cbf05Sopenharmony_ci    explicit A(int data) : data_(data), timer_("ATimer") {}
1553f4cbf05Sopenharmony_ci    ~A() = default;
1563f4cbf05Sopenharmony_ci    bool Init();
1573f4cbf05Sopenharmony_ci    bool StartTimer(int milliseconds, bool once);
1583f4cbf05Sopenharmony_ci    void StopTimer();
1593f4cbf05Sopenharmony_ci    int GetData() const
1603f4cbf05Sopenharmony_ci    {
1613f4cbf05Sopenharmony_ci        return data_;
1623f4cbf05Sopenharmony_ci    }
1633f4cbf05Sopenharmony_ciprivate:
1643f4cbf05Sopenharmony_ci    void TimeOutProc()
1653f4cbf05Sopenharmony_ci    {
1663f4cbf05Sopenharmony_ci        data_ -= 1;
1673f4cbf05Sopenharmony_ci    };
1683f4cbf05Sopenharmony_ci    int data_;
1693f4cbf05Sopenharmony_ci    Utils::Timer timer_;
1703f4cbf05Sopenharmony_ci};
1713f4cbf05Sopenharmony_ci
1723f4cbf05Sopenharmony_cibool A::Init()
1733f4cbf05Sopenharmony_ci{
1743f4cbf05Sopenharmony_ci    return timer_.Setup() == Utils::TIMER_ERR_OK;
1753f4cbf05Sopenharmony_ci}
1763f4cbf05Sopenharmony_ci
1773f4cbf05Sopenharmony_cibool A::StartTimer(int milliseconds, bool once)
1783f4cbf05Sopenharmony_ci{
1793f4cbf05Sopenharmony_ci    uint32_t timerId = timer_.Register(std::bind(&A::TimeOutProc, this), milliseconds, once);
1803f4cbf05Sopenharmony_ci    return timerId != Utils::TIMER_ERR_DEAL_FAILED;
1813f4cbf05Sopenharmony_ci}
1823f4cbf05Sopenharmony_ci
1833f4cbf05Sopenharmony_civoid A::StopTimer()
1843f4cbf05Sopenharmony_ci{
1853f4cbf05Sopenharmony_ci    timer_.Shutdown();
1863f4cbf05Sopenharmony_ci}
1873f4cbf05Sopenharmony_ci
1883f4cbf05Sopenharmony_ci/*
1893f4cbf05Sopenharmony_ci * @tc.name: testTimer005
1903f4cbf05Sopenharmony_ci * @tc.desc: timer unit test
1913f4cbf05Sopenharmony_ci *
1923f4cbf05Sopenharmony_ci * temporarily offline for kernel difference
1933f4cbf05Sopenharmony_ciHWTEST_F(UtilsTimerTest, testTimer005, TestSize.Level0)
1943f4cbf05Sopenharmony_ci{
1953f4cbf05Sopenharmony_ci    A a(10);
1963f4cbf05Sopenharmony_ci    EXPECT_TRUE(a.Init());
1973f4cbf05Sopenharmony_ci    EXPECT_TRUE(a.StartTimer(1, false));
1983f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(10));
1993f4cbf05Sopenharmony_ci    a.StopTimer();
2003f4cbf05Sopenharmony_ci    EXPECT_GE(8, a.GetData());
2013f4cbf05Sopenharmony_ci}
2023f4cbf05Sopenharmony_ci*/
2033f4cbf05Sopenharmony_ci
2043f4cbf05Sopenharmony_ci/*
2053f4cbf05Sopenharmony_ci * @tc.name: testTimer006
2063f4cbf05Sopenharmony_ci * @tc.desc: timer unit test
2073f4cbf05Sopenharmony_ci */
2083f4cbf05Sopenharmony_ciHWTEST_F(UtilsTimerTest, testTimer006, TestSize.Level0)
2093f4cbf05Sopenharmony_ci{
2103f4cbf05Sopenharmony_ci    A a(10);
2113f4cbf05Sopenharmony_ci    EXPECT_TRUE(a.Init());
2123f4cbf05Sopenharmony_ci    EXPECT_TRUE(a.StartTimer(1, true));
2133f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(20));
2143f4cbf05Sopenharmony_ci    a.StopTimer();
2153f4cbf05Sopenharmony_ci    EXPECT_EQ(9, a.GetData());
2163f4cbf05Sopenharmony_ci}
2173f4cbf05Sopenharmony_ci
2183f4cbf05Sopenharmony_cistatic void TimerEventFun(Utils::Timer& timer)
2193f4cbf05Sopenharmony_ci{
2203f4cbf05Sopenharmony_ci    uint32_t timerId = 0;
2213f4cbf05Sopenharmony_ci    uint32_t loops = 10;
2223f4cbf05Sopenharmony_ci    uint32_t interval = 7;
2233f4cbf05Sopenharmony_ci    int sleepTime = 10;
2243f4cbf05Sopenharmony_ci    for (uint32_t i = 0; i < loops; i++) {
2253f4cbf05Sopenharmony_ci        timerId = timer.Register(TimeOutCallback1, interval, true);
2263f4cbf05Sopenharmony_ci        std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
2273f4cbf05Sopenharmony_ci    }
2283f4cbf05Sopenharmony_ci    timer.Unregister(timerId);
2293f4cbf05Sopenharmony_ci    timer.Unregister(timerId);
2303f4cbf05Sopenharmony_ci}
2313f4cbf05Sopenharmony_ci
2323f4cbf05Sopenharmony_ci/*
2333f4cbf05Sopenharmony_ci * @tc.name: testTimer007
2343f4cbf05Sopenharmony_ci * @tc.desc: abnormal case
2353f4cbf05Sopenharmony_ci */
2363f4cbf05Sopenharmony_ciHWTEST_F(UtilsTimerTest, testTimer007, TestSize.Level0)
2373f4cbf05Sopenharmony_ci{
2383f4cbf05Sopenharmony_ci    g_data1 = 0;
2393f4cbf05Sopenharmony_ci    Utils::Timer timer("test_timer");
2403f4cbf05Sopenharmony_ci    uint32_t ret = timer.Setup();
2413f4cbf05Sopenharmony_ci    EXPECT_EQ(Utils::TIMER_ERR_OK, ret);
2423f4cbf05Sopenharmony_ci    TimerEventFun(timer);
2433f4cbf05Sopenharmony_ci    timer.Shutdown();
2443f4cbf05Sopenharmony_ci    timer.Shutdown(false);
2453f4cbf05Sopenharmony_ci    EXPECT_GE(g_data1, 5);
2463f4cbf05Sopenharmony_ci}
2473f4cbf05Sopenharmony_ci
2483f4cbf05Sopenharmony_cistatic void SleepLoopFunc()
2493f4cbf05Sopenharmony_ci{
2503f4cbf05Sopenharmony_ci    int loops = 11;
2513f4cbf05Sopenharmony_ci    int sleepTime = 10;
2523f4cbf05Sopenharmony_ci    int64_t desiredVal = 10;
2533f4cbf05Sopenharmony_ci    for (int i = 0; i < loops; i++) {
2543f4cbf05Sopenharmony_ci        int64_t pre = CurMs();
2553f4cbf05Sopenharmony_ci        std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
2563f4cbf05Sopenharmony_ci        int64_t cur = CurMs();
2573f4cbf05Sopenharmony_ci        EXPECT_GE(cur - pre, desiredVal);
2583f4cbf05Sopenharmony_ci    }
2593f4cbf05Sopenharmony_ci}
2603f4cbf05Sopenharmony_ci
2613f4cbf05Sopenharmony_ci/*
2623f4cbf05Sopenharmony_ci * @tc.name: testTimer008
2633f4cbf05Sopenharmony_ci * @tc.desc: timer sleep test for ivi
2643f4cbf05Sopenharmony_ci */
2653f4cbf05Sopenharmony_ciHWTEST_F(UtilsTimerTest, testTimer008, TestSize.Level0)
2663f4cbf05Sopenharmony_ci{
2673f4cbf05Sopenharmony_ci    g_data1 = 0;
2683f4cbf05Sopenharmony_ci    Utils::Timer timer("test_timer");
2693f4cbf05Sopenharmony_ci    uint32_t ret = timer.Setup();
2703f4cbf05Sopenharmony_ci    EXPECT_EQ(Utils::TIMER_ERR_OK, ret);
2713f4cbf05Sopenharmony_ci    timer.Register(TimeOutCallback1, 10);
2723f4cbf05Sopenharmony_ci
2733f4cbf05Sopenharmony_ci    SleepLoopFunc();
2743f4cbf05Sopenharmony_ci    timer.Shutdown();
2753f4cbf05Sopenharmony_ci    EXPECT_GE(g_data1, 10);
2763f4cbf05Sopenharmony_ci}
2773f4cbf05Sopenharmony_ci
2783f4cbf05Sopenharmony_ci/*
2793f4cbf05Sopenharmony_ci * @tc.name: testTimer009
2803f4cbf05Sopenharmony_ci * @tc.desc: recursive test
2813f4cbf05Sopenharmony_ci */
2823f4cbf05Sopenharmony_civoid DoFunc(Utils::Timer &timer, int &count)
2833f4cbf05Sopenharmony_ci{
2843f4cbf05Sopenharmony_ci    (void)timer.Register(
2853f4cbf05Sopenharmony_ci        [&timer, &count]() {
2863f4cbf05Sopenharmony_ci            count += 1;
2873f4cbf05Sopenharmony_ci            if (count > 9) {
2883f4cbf05Sopenharmony_ci                return;
2893f4cbf05Sopenharmony_ci            }
2903f4cbf05Sopenharmony_ci            DoFunc(timer, count);
2913f4cbf05Sopenharmony_ci        },
2923f4cbf05Sopenharmony_ci        10, true);
2933f4cbf05Sopenharmony_ci    g_data1++;
2943f4cbf05Sopenharmony_ci}
2953f4cbf05Sopenharmony_ci
2963f4cbf05Sopenharmony_civoid DoFunc2(Utils::Timer &timer, int &count)
2973f4cbf05Sopenharmony_ci{
2983f4cbf05Sopenharmony_ci    (void)timer.Register(
2993f4cbf05Sopenharmony_ci        [&timer, &count]() {
3003f4cbf05Sopenharmony_ci            count += 1;
3013f4cbf05Sopenharmony_ci            if (count > 9) {
3023f4cbf05Sopenharmony_ci                return;
3033f4cbf05Sopenharmony_ci            }
3043f4cbf05Sopenharmony_ci            DoFunc2(timer, count);
3053f4cbf05Sopenharmony_ci        },
3063f4cbf05Sopenharmony_ci        10, true);
3073f4cbf05Sopenharmony_ci    g_data1++;
3083f4cbf05Sopenharmony_ci}
3093f4cbf05Sopenharmony_ci
3103f4cbf05Sopenharmony_ciHWTEST_F(UtilsTimerTest, testTimer009, TestSize.Level0)
3113f4cbf05Sopenharmony_ci{
3123f4cbf05Sopenharmony_ci    g_data1 = 0;
3133f4cbf05Sopenharmony_ci    Utils::Timer timer("test_timer");
3143f4cbf05Sopenharmony_ci    uint32_t ret = timer.Setup();
3153f4cbf05Sopenharmony_ci    EXPECT_EQ(Utils::TIMER_ERR_OK, ret);
3163f4cbf05Sopenharmony_ci
3173f4cbf05Sopenharmony_ci    int cnt = 0, cnt1 = 0;
3183f4cbf05Sopenharmony_ci    DoFunc(timer, cnt);
3193f4cbf05Sopenharmony_ci    DoFunc2(timer, cnt1);
3203f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(50));
3213f4cbf05Sopenharmony_ci    EXPECT_GE(g_data1, 5);  /* 8 for max */
3223f4cbf05Sopenharmony_ci    EXPECT_GE(14, g_data1); /* 10 for min */
3233f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(50));
3243f4cbf05Sopenharmony_ci    timer.Shutdown();
3253f4cbf05Sopenharmony_ci    EXPECT_GE(g_data1, 10); /* 18 for max */
3263f4cbf05Sopenharmony_ci}
3273f4cbf05Sopenharmony_ci
3283f4cbf05Sopenharmony_cistatic void TimerRegisterMechanism(Utils::Timer& timer, bool once)
3293f4cbf05Sopenharmony_ci{
3303f4cbf05Sopenharmony_ci    uint32_t interval = 10;
3313f4cbf05Sopenharmony_ci    timer.Register(TimeOutCallback1, interval, once);
3323f4cbf05Sopenharmony_ci    timer.Register(TimeOutCallback1, interval, !once);
3333f4cbf05Sopenharmony_ci    timer.Register(TimeOutCallback1, interval, once);
3343f4cbf05Sopenharmony_ci    timer.Register(TimeOutCallback1, interval, !once);
3353f4cbf05Sopenharmony_ci}
3363f4cbf05Sopenharmony_ci
3373f4cbf05Sopenharmony_ci/*
3383f4cbf05Sopenharmony_ci * @tc.name: testTimer010
3393f4cbf05Sopenharmony_ci * @tc.desc: once timer register
3403f4cbf05Sopenharmony_ci */
3413f4cbf05Sopenharmony_ciHWTEST_F(UtilsTimerTest, testTimer010, TestSize.Level0)
3423f4cbf05Sopenharmony_ci{
3433f4cbf05Sopenharmony_ci    g_data1 = 0;
3443f4cbf05Sopenharmony_ci    Utils::Timer timer("test_timer");
3453f4cbf05Sopenharmony_ci    uint32_t ret = timer.Setup();
3463f4cbf05Sopenharmony_ci    EXPECT_EQ(Utils::TIMER_ERR_OK, ret);
3473f4cbf05Sopenharmony_ci    bool once = true;
3483f4cbf05Sopenharmony_ci    TimerRegisterMechanism(timer, once);
3493f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(52));
3503f4cbf05Sopenharmony_ci    timer.Shutdown();
3513f4cbf05Sopenharmony_ci    EXPECT_GE(g_data1, 8); /* 12 for max */
3523f4cbf05Sopenharmony_ci}
3533f4cbf05Sopenharmony_ci
3543f4cbf05Sopenharmony_ci/*
3553f4cbf05Sopenharmony_ci * @tc.name: testTimer011
3563f4cbf05Sopenharmony_ci * @tc.desc: once timer register
3573f4cbf05Sopenharmony_ci */
3583f4cbf05Sopenharmony_ciHWTEST_F(UtilsTimerTest, testTimer011, TestSize.Level0)
3593f4cbf05Sopenharmony_ci{
3603f4cbf05Sopenharmony_ci    g_data1 = 0;
3613f4cbf05Sopenharmony_ci    Utils::Timer timer("test_timer");
3623f4cbf05Sopenharmony_ci    uint32_t ret = timer.Setup();
3633f4cbf05Sopenharmony_ci    EXPECT_EQ(Utils::TIMER_ERR_OK, ret);
3643f4cbf05Sopenharmony_ci    bool once = false;
3653f4cbf05Sopenharmony_ci    TimerRegisterMechanism(timer, once);
3663f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(52));
3673f4cbf05Sopenharmony_ci    timer.Shutdown();
3683f4cbf05Sopenharmony_ci    EXPECT_GE(g_data1, 8); /* 12 for max */
3693f4cbf05Sopenharmony_ci}
3703f4cbf05Sopenharmony_ci
3713f4cbf05Sopenharmony_ci/*
3723f4cbf05Sopenharmony_ci * @tc.name: testTimer012
3733f4cbf05Sopenharmony_ci * @tc.desc: Test double setup.
3743f4cbf05Sopenharmony_ci */
3753f4cbf05Sopenharmony_ciHWTEST_F(UtilsTimerTest, testTimer012, TestSize.Level0)
3763f4cbf05Sopenharmony_ci{
3773f4cbf05Sopenharmony_ci    g_data1 = 0;
3783f4cbf05Sopenharmony_ci    Utils::Timer timer("test_timer");
3793f4cbf05Sopenharmony_ci    uint32_t ret = timer.Setup();
3803f4cbf05Sopenharmony_ci    EXPECT_EQ(Utils::TIMER_ERR_OK, ret);
3813f4cbf05Sopenharmony_ci    ret = timer.Setup();
3823f4cbf05Sopenharmony_ci    EXPECT_EQ(Utils::TIMER_ERR_INVALID_VALUE, ret);
3833f4cbf05Sopenharmony_ci
3843f4cbf05Sopenharmony_ci    timer.Shutdown();
3853f4cbf05Sopenharmony_ci}
3863f4cbf05Sopenharmony_ci
3873f4cbf05Sopenharmony_ci/*
3883f4cbf05Sopenharmony_ci * @tc.name: testTimer013
3893f4cbf05Sopenharmony_ci * @tc.desc: Test uncommon operations.
3903f4cbf05Sopenharmony_ci */
3913f4cbf05Sopenharmony_ciHWTEST_F(UtilsTimerTest, testTimer013, TestSize.Level0)
3923f4cbf05Sopenharmony_ci{
3933f4cbf05Sopenharmony_ci    g_data1 = 0;
3943f4cbf05Sopenharmony_ci    Utils::Timer timer("test_timer", -1);
3953f4cbf05Sopenharmony_ci    uint32_t ret = timer.Setup();
3963f4cbf05Sopenharmony_ci    EXPECT_EQ(Utils::TIMER_ERR_OK, ret);
3973f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(1));
3983f4cbf05Sopenharmony_ci    timer.Shutdown();
3993f4cbf05Sopenharmony_ci
4003f4cbf05Sopenharmony_ci    Utils::Timer timer1("test_timer_1");
4013f4cbf05Sopenharmony_ci    ret = timer1.Setup();
4023f4cbf05Sopenharmony_ci    EXPECT_EQ(Utils::TIMER_ERR_OK, ret);
4033f4cbf05Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(1));
4043f4cbf05Sopenharmony_ci    timer1.Shutdown(false);
4053f4cbf05Sopenharmony_ci}
4063f4cbf05Sopenharmony_ci}  // namespace
4073f4cbf05Sopenharmony_ci}  // namespace OHOS