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
48 HWTEST_F(LoopTimerUnitTest, Init_Timer_001, TestSize.Level0)
49 {
50 EXPECT_EQ(LE_CreateLoop(&g_loop), 0);
51
52 TimerHandle timer = NULL;
53 int ret = LE_CreateTimer(NULL, &timer, Test_ProcessTimer, NULL);
54 EXPECT_NE(ret, 0);
55
56 ret = LE_CreateTimer(g_loop, NULL, Test_ProcessTimer, NULL);
57 EXPECT_NE(ret, 0);
58
59 ret = LE_CreateTimer(g_loop, &timer, NULL, NULL);
60 EXPECT_NE(ret, 0);
61
62 CancelTimer(timer);
63
64 uint64_t time = GetCurrentTimespec(0);
65 EXPECT_GT(time, 0);
66
67 time = GetMinTimeoutPeriod(NULL);
68 EXPECT_EQ(time, 0);
69
70 EventLoop *loop = reinterpret_cast<EventLoop *>(g_loop);
71 DestroyTimerList(loop);
72 printf("Init_Timer_001 %d end", g_maxCount);
73 }
74
75 HWTEST_F(LoopTimerUnitTest, Init_Timer_002, TestSize.Level0)
76 {
77 EXPECT_EQ(LE_CreateLoop(&g_loop), 0);
78 EventLoop *loop = reinterpret_cast<EventLoop *>(g_loop);
79
80 TimerHandle timer = NULL;
81 g_maxCount = 1;
82 int ret = LE_CreateTimer(g_loop, &timer, Test_ProcessTimer, NULL);
83 EXPECT_EQ(ret, 0);
84 ret = LE_StartTimer(g_loop, timer, 200, 1);
85 EXPECT_EQ(ret, 0);
86 usleep(200000);
87 CheckTimeoutOfTimer(loop, GetCurrentTimespec(0));
88 EXPECT_EQ(g_maxCount, 0);
89 LE_CloseLoop(g_loop);
90
91 printf("Init_Timer_002 %d end", g_maxCount);
92 }
93
94 HWTEST_F(LoopTimerUnitTest, Init_Timer_003, TestSize.Level0)
95 {
96 EXPECT_EQ(LE_CreateLoop(&g_loop), 0);
97
98 TimerHandle timer = NULL;
99 int ret = LE_CreateTimer(g_loop, &timer, Test_ProcessTimer, NULL);
100 EXPECT_EQ(ret, 0);
101 ret = LE_StartTimer(g_loop, timer, 200, 2);
102 EXPECT_EQ(ret, 0);
103 g_maxCount = 2;
104 LE_RunLoop(g_loop);
105 EXPECT_EQ(g_maxCount, 0);
106 LE_CloseLoop(g_loop);
107
108 printf("Init_Timer_003 %d end", g_maxCount);
109 }
110
111 HWTEST_F(LoopTimerUnitTest, Init_Timer_004, TestSize.Level0)
112 {
113 EXPECT_EQ(LE_CreateLoop(&g_loop), 0);
114
115 g_maxCount = 3;
116 TimerHandle timer = NULL;
117 int ret = LE_CreateTimer(g_loop, &timer, Test_ProcessTimer, NULL);
118 EXPECT_EQ(ret, 0);
119 ret = LE_StartTimer(g_loop, timer, 100, 1);
120 EXPECT_EQ(ret, 0);
121
122 TimerHandle timer1 = NULL;
123 ret = LE_CreateTimer(g_loop, &timer1, Test_ProcessTimer, NULL);
124 EXPECT_EQ(ret, 0);
125 ret = LE_StartTimer(g_loop, timer1, 150, 1);
126 EXPECT_EQ(ret, 0);
127
128 TimerHandle timer2 = NULL;
129 ret = LE_CreateTimer(g_loop, &timer2, Test_ProcessTimer, NULL);
130 EXPECT_EQ(ret, 0);
131 ret = LE_StartTimer(g_loop, timer2, 300, 1);
132 EXPECT_EQ(ret, 0);
133
134 usleep(150);
135 LE_RunLoop(g_loop);
136 EXPECT_EQ(g_maxCount, 0);
137 LE_CloseLoop(g_loop);
138
139 printf("Init_Timer_004 %d end", g_maxCount);
140 }
141
142 HWTEST_F(LoopTimerUnitTest, Init_Timer_005, TestSize.Level0)
143 {
144 EXPECT_EQ(LE_CreateLoop(&g_loop), 0);
145
146 g_maxCount = 3;
147 TimerHandle timer = NULL;
148 int ret = LE_CreateTimer(g_loop, &timer, Test_ProcessTimer, NULL);
149 EXPECT_EQ(ret, 0);
150 ret = LE_StartTimer(g_loop, timer, 100, 2);
151 EXPECT_EQ(ret, 0);
152
153 TimerHandle timer1 = NULL;
154 ret = LE_CreateTimer(g_loop, &timer1, Test_ProcessTimer, NULL);
155 EXPECT_EQ(ret, 0);
156 ret = LE_StartTimer(g_loop, timer1, 150, 2);
157 EXPECT_EQ(ret, 0);
158
159 TimerHandle timer2 = NULL;
160 ret = LE_CreateTimer(g_loop, &timer2, Test_ProcessTimer, NULL);
161 EXPECT_EQ(ret, 0);
162 ret = LE_StartTimer(g_loop, timer2, 300, 1);
163 EXPECT_EQ(ret, 0);
164
165 CancelTimer(timer);
166 LE_RunLoop(g_loop);
167 EXPECT_EQ(g_maxCount, 0);
168 LE_CloseLoop(g_loop);
169
170 printf("Init_Timer_005 %d end", g_maxCount);
171 }
172 }