1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <gtest/gtest.h>
17 #include <ctime>
18 #include <cstdint>
19 #include <cstdlib>
20 #include <unistd.h>
21 #include "le_loop.h"
22 #include "loop_event.h"
23 #include "le_timer.h"
24 
25 using namespace testing::ext;
26 using namespace std;
27 
28 namespace init_ut {
29 class LoopTimerUnitTest : public testing::Test {
30 public:
SetUpTestCase(void)31     static void SetUpTestCase(void) {};
TearDownTestCase(void)32     static void TearDownTestCase(void) {};
SetUp()33     void SetUp() {};
TearDown()34     void TearDown() {};
35 };
36 
37 static LoopHandle g_loop = NULL;
38 int32_t g_maxCount = 0;
Test_ProcessTimer(const TimerHandle taskHandle,void * context)39 static void Test_ProcessTimer(const TimerHandle taskHandle, void *context)
40 {
41     g_maxCount--;
42     if (g_maxCount <= 0) {
43          LE_StopLoop(g_loop);
44     }
45     printf("WaitTimeout count %d\n", g_maxCount);
46 }
47 
TimeoutCancel(const TimerHandle taskHandle,void * context)48 static void TimeoutCancel(const TimerHandle taskHandle, void *context)
49 {
50     printf("TimeoutCancel count %d", g_maxCount);
51     LE_StopTimer(LE_GetDefaultLoop(), taskHandle);
52 }
53 
54 HWTEST_F(LoopTimerUnitTest, Init_Timer_001, TestSize.Level0)
55 {
56     EXPECT_EQ(LE_CreateLoop(&g_loop), 0);
57 
58     TimerHandle timer = NULL;
59     int ret = LE_CreateTimer(NULL, &timer, Test_ProcessTimer, NULL);
60     EXPECT_NE(ret, 0);
61 
62     ret = LE_CreateTimer(g_loop, NULL, Test_ProcessTimer, NULL);
63     EXPECT_NE(ret, 0);
64 
65     ret = LE_CreateTimer(g_loop,  &timer, NULL, NULL);
66     EXPECT_NE(ret, 0);
67 
68     CancelTimer(timer);
69 
70     uint64_t time = GetCurrentTimespec(0);
71     EXPECT_GT(time, 0);
72 
73     time = GetMinTimeoutPeriod(NULL);
74     EXPECT_EQ(time, 0);
75 
76     EventLoop *loop = reinterpret_cast<EventLoop *>(g_loop);
77     DestroyTimerList(loop);
78     printf("Init_Timer_001 %d end", g_maxCount);
79 }
80 
81 HWTEST_F(LoopTimerUnitTest, Init_Timer_002, TestSize.Level0)
82 {
83     EXPECT_EQ(LE_CreateLoop(&g_loop), 0);
84     EventLoop *loop = reinterpret_cast<EventLoop *>(g_loop);
85 
86     TimerHandle timer = NULL;
87     g_maxCount = 1;
88     int ret = LE_CreateTimer(g_loop, &timer, Test_ProcessTimer, NULL);
89     EXPECT_EQ(ret, 0);
90     ret = LE_StartTimer(g_loop, timer, 200, 1);
91     EXPECT_EQ(ret, 0);
92     usleep(200000);
93     CheckTimeoutOfTimer(loop, GetCurrentTimespec(0));
94     EXPECT_EQ(g_maxCount, 0);
95     LE_CloseLoop(g_loop);
96 
97     printf("Init_Timer_002 %d end", g_maxCount);
98 }
99 
100 HWTEST_F(LoopTimerUnitTest, Init_Timer_003, TestSize.Level0)
101 {
102     EXPECT_EQ(LE_CreateLoop(&g_loop), 0);
103 
104     TimerHandle timer = NULL;
105     int ret = LE_CreateTimer(g_loop, &timer, Test_ProcessTimer, NULL);
106     EXPECT_EQ(ret, 0);
107     ret = LE_StartTimer(g_loop, timer, 200, 2);
108     EXPECT_EQ(ret, 0);
109     g_maxCount = 2;
110     LE_RunLoop(g_loop);
111     EXPECT_EQ(g_maxCount, 0);
112     LE_CloseLoop(g_loop);
113 
114     printf("Init_Timer_003 %d end", g_maxCount);
115 }
116 
117 HWTEST_F(LoopTimerUnitTest, Init_Timer_004, TestSize.Level0)
118 {
119     EXPECT_EQ(LE_CreateLoop(&g_loop), 0);
120 
121     g_maxCount = 3;
122     TimerHandle timer = NULL;
123     int ret = LE_CreateTimer(g_loop, &timer, Test_ProcessTimer, NULL);
124     EXPECT_EQ(ret, 0);
125     ret = LE_StartTimer(g_loop, timer, 100, 1);
126     EXPECT_EQ(ret, 0);
127 
128     TimerHandle timer1 = NULL;
129     ret = LE_CreateTimer(g_loop, &timer1, Test_ProcessTimer, NULL);
130     EXPECT_EQ(ret, 0);
131     ret = LE_StartTimer(g_loop, timer1, 150, 1);
132     EXPECT_EQ(ret, 0);
133 
134     TimerHandle timer2 = NULL;
135     ret = LE_CreateTimer(g_loop, &timer2, Test_ProcessTimer, NULL);
136     EXPECT_EQ(ret, 0);
137     ret = LE_StartTimer(g_loop, timer2, 300, 1);
138     EXPECT_EQ(ret, 0);
139 
140     usleep(150);
141     LE_RunLoop(g_loop);
142     EXPECT_EQ(g_maxCount, 0);
143     LE_CloseLoop(g_loop);
144 
145     printf("Init_Timer_004 %d end", g_maxCount);
146 }
147 
148 HWTEST_F(LoopTimerUnitTest, Init_Timer_005, TestSize.Level0)
149 {
150     EXPECT_EQ(LE_CreateLoop(&g_loop), 0);
151 
152     g_maxCount = 3;
153     TimerHandle timer = NULL;
154     int ret = LE_CreateTimer(g_loop, &timer, Test_ProcessTimer, NULL);
155     EXPECT_EQ(ret, 0);
156     ret = LE_StartTimer(g_loop, timer, 100, 2);
157     EXPECT_EQ(ret, 0);
158 
159     TimerHandle timer1 = NULL;
160     ret = LE_CreateTimer(g_loop, &timer1, Test_ProcessTimer, NULL);
161     EXPECT_EQ(ret, 0);
162     ret = LE_StartTimer(g_loop, timer1, 150, 2);
163     EXPECT_EQ(ret, 0);
164 
165     TimerHandle timer2 = NULL;
166     ret = LE_CreateTimer(g_loop, &timer2, Test_ProcessTimer, NULL);
167     EXPECT_EQ(ret, 0);
168     ret = LE_StartTimer(g_loop, timer2, 300, 1);
169     EXPECT_EQ(ret, 0);
170 
171     CancelTimer(timer);
172     LE_RunLoop(g_loop);
173     EXPECT_EQ(g_maxCount, 0);
174     LE_CloseLoop(g_loop);
175 
176     printf("Init_Timer_005 %d end", g_maxCount);
177 }
178 }