1 /*
2 * Copyright (C) 2023-2023 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 #define private public
17 #include <gtest/gtest.h>
18 #include <unistd.h>
19 #include "timer_manager.h"
20 #include "timer_proxy.h"
21
22 namespace OHOS {
23 namespace MiscServices {
24 using namespace testing::ext;
25 using namespace std::chrono;
26
27 namespace {
28 constexpr uint64_t NANO_TO_MILESECOND = 100000;
29 constexpr int BLOCK_TEST_TIME = 100000;
30 }
31 std::shared_ptr<TimerManager> timerManagerHandler_ = nullptr;
32
33 class TimeProxyTest : public testing::Test {
34 public:
35 static void SetUpTestCase(void);
36
37 static void TearDownTestCase(void);
38
39 void SetUp();
40
41 void TearDown();
42 };
43
SetUpTestCase(void)44 void TimeProxyTest::SetUpTestCase(void)
45 {}
46
TearDownTestCase(void)47 void TimeProxyTest::TearDownTestCase(void)
48 {}
49
SetUp(void)50 void TimeProxyTest::SetUp(void)
51 {
52 TIME_HILOGI(TIME_MODULE_SERVICE, "start SetUp.");
53 timerManagerHandler_ = TimerManager::Create();
54 EXPECT_NE(timerManagerHandler_, nullptr);
55 TIME_HILOGI(TIME_MODULE_SERVICE, "end SetUp.");
56 usleep(BLOCK_TEST_TIME);
57 }
58
TearDown(void)59 void TimeProxyTest::TearDown(void)
60 {
61 TIME_HILOGI(TIME_MODULE_SERVICE, "start TearDown.");
62 timerManagerHandler_->alarmThread_->detach();
63 timerManagerHandler_ = nullptr;
64 EXPECT_EQ(timerManagerHandler_, nullptr);
65 TIME_HILOGI(TIME_MODULE_SERVICE, "end TearDown.");
66 }
67
68 /**
69 * @tc.name: UidTimerMap001
70 * @tc.desc: 启动timer时uid timer map数据更新测试
71 * @tc.type: FUNC
72 */
73 HWTEST_F(TimeProxyTest, UidTimerMap001, TestSize.Level1)
74 {
75 /* 创建一个timer,可以创建成功 */
76 TimerPara paras;
77 paras.timerType = 2;
78 paras.windowLength = -1;
79 paras.interval = 0;
80 paras.flag = 0;
81 auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
82 int32_t uid = 2000;
83 uint64_t timerId = 1000;
__anonc2863dd40202(const uint64_t) 84 int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {}, wantAgent, uid, timerId);
85 EXPECT_EQ(ret, TimeError::E_TIME_OK);
86 usleep(BLOCK_TEST_TIME);
87
88 /* 启动一个timer, 可以启动成功,可以记录到uidTimerMap_中 */
89 auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
90 uint64_t triggerTime = 10000000 + nowElapsed;
91 ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
92 EXPECT_EQ(ret, TimeError::E_TIME_OK);
93 usleep(BLOCK_TEST_TIME);
94 EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)1);
95 auto itUidTimerMap = TimerProxy::GetInstance().uidTimersMap_.find(uid);
96 EXPECT_NE(itUidTimerMap, TimerProxy::GetInstance().uidTimersMap_.end());
97 EXPECT_EQ(itUidTimerMap->second.size(), (const unsigned int)1);
98 auto itTimerId = itUidTimerMap->second.find(timerId);
99 EXPECT_NE(itTimerId, itUidTimerMap->second.end());
100 EXPECT_NE(itTimerId->second, nullptr);
101
102 /* 清理uidTimerMap_,可以清理成功 */
103 TimerProxy::GetInstance().uidTimersMap_.clear();
104 EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)0);
105 timerManagerHandler_->DestroyTimer(timerId);
106 usleep(BLOCK_TEST_TIME);
107 }
108
109 /**
110 * @tc.name: UidTimerMap002
111 * @tc.desc: 停止timer时uid timer map数据更新测试
112 * @tc.type: FUNC
113 */
114 HWTEST_F(TimeProxyTest, UidTimerMap002, TestSize.Level1)
115 {
116 /* 创建一个timer,可以创建成功 */
117 TimerPara paras;
118 paras.timerType = 2;
119 paras.windowLength = -1;
120 paras.interval = 0;
121 paras.flag = 0;
122 auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
123 int32_t uid = 2000;
124 uint64_t timerId = 1000;
__anonc2863dd40302(const uint64_t) 125 int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {}, wantAgent, uid, timerId);
126 EXPECT_EQ(ret, TimeError::E_TIME_OK);
127 usleep(BLOCK_TEST_TIME);
128
129 /* 启动一个timer, 可以启动成功,可以记录到uidTimerMap_中 */
130 auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
131 uint64_t triggerTime = 10000000 + nowElapsed;
132 ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
133 EXPECT_EQ(ret, TimeError::E_TIME_OK);
134 usleep(BLOCK_TEST_TIME);
135 EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)1);
136
137 /* 停止一个timer,可以停止成功,可以从uidTimerMap_中删除 */
138 ret = timerManagerHandler_->StopTimerInner(timerId, true);
139 EXPECT_EQ(ret, TimeError::E_TIME_OK);
140 usleep(BLOCK_TEST_TIME);
141 EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)0);
142 timerManagerHandler_->DestroyTimer(timerId);
143 }
144
145 /**
146 * @tc.name: UidTimerMap003
147 * @tc.desc: 触发timer时uid timer map数据更新测试
148 * @tc.type: FUNC
149 */
150 HWTEST_F(TimeProxyTest, UidTimerMap003, TestSize.Level1)
151 {
152 /* 创建一个timer,可以创建成功 */
153 TimerPara paras;
154 paras.timerType = 2;
155 paras.windowLength = -1;
156 paras.interval = 0;
157 paras.flag = 0;
158 auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
159 int32_t uid = 2000;
160 uint64_t timerId = 1000;
__anonc2863dd40402(const uint64_t) 161 int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {}, wantAgent, uid, timerId);
162 EXPECT_EQ(ret, TimeError::E_TIME_OK);
163 usleep(BLOCK_TEST_TIME);
164
165 /* 启动一个timer, 可以启动成功,可以记录到uidTimerMap_中 */
166 auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
167 uint64_t triggerTime = 10000000 + nowElapsed;
168 ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
169 EXPECT_EQ(ret, TimeError::E_TIME_OK);
170 usleep(BLOCK_TEST_TIME);
171 EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)1);
172
173 /* 触发一个timer,可以触发成功,可以从uidTimerMap_中删除 */
174 std::vector<std::shared_ptr<TimerInfo>> triggerList;
175 std::shared_ptr<Batch> batch = timerManagerHandler_->alarmBatches_.at(0);
176 std::chrono::steady_clock::time_point tpRpoch(nanoseconds(1000000000));
177 batch->start_ = tpRpoch;
178 auto retTrigger = timerManagerHandler_->TriggerTimersLocked(triggerList, timerManagerHandler_->GetBootTimeNs());
179 EXPECT_EQ(retTrigger, true);
180 usleep(BLOCK_TEST_TIME);
181 EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)0);
182 timerManagerHandler_->DestroyTimer(timerId);
183 }
184
185 /**
186 * @tc.name: ProxyTimer001
187 * @tc.desc: 代理解代理基本功能测试
188 * @tc.type: FUNC
189 */
190 HWTEST_F(TimeProxyTest, ProxyTimer001, TestSize.Level1)
191 {
192 /* 代理一个timer,可以代理成功,可以记录到proxyUid_中 */
193 int32_t uid = 1000;
194 bool isProxy = true;
195 bool needRetrigger = true;
196 bool ret = timerManagerHandler_->ProxyTimer(uid, isProxy, needRetrigger);
197 EXPECT_TRUE(ret);
198 usleep(BLOCK_TEST_TIME);
199 EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)1);
200 auto it = TimerProxy::GetInstance().proxyUids_.find(uid);
201 EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
202 EXPECT_EQ(it->second.size(), (const unsigned int)0);
203
204 /* 解代理一个timer,可以解代理成功,可以从proxyUid_中删除 */
205 isProxy = false;
206 ret = timerManagerHandler_->ProxyTimer(uid, isProxy, needRetrigger);
207 EXPECT_TRUE(ret);
208 usleep(BLOCK_TEST_TIME);
209 EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)0);
210 }
211
212 /**
213 * @tc.name: ProxyTimer002
214 * @tc.desc: 代理解代理时proxy timer map数据更新测试
215 * @tc.type: FUNC
216 */
217 HWTEST_F(TimeProxyTest, ProxyTimer002, TestSize.Level1)
218 {
219 /* 创建一个timer,可以创建成功 */
220 TimerPara paras;
221 paras.timerType = 2;
222 paras.windowLength = -1;
223 paras.interval = 0;
224 paras.flag = 0;
225 auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
226 int32_t uid = 2000;
227 uint64_t timerId = 1000;
__anonc2863dd40502(const uint64_t) 228 int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {}, wantAgent, uid, timerId);
229 EXPECT_EQ(ret, TimeError::E_TIME_OK);
230 usleep(BLOCK_TEST_TIME);
231
232 /* 启动一个timer, 可以启动成功,可以记录到uidTimerMap_中 */
233 auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
234 uint64_t triggerTime = 10000000 + nowElapsed;
235 ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
236 EXPECT_EQ(ret, TimeError::E_TIME_OK);
237 usleep(BLOCK_TEST_TIME);
238 EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)1);
239 std::chrono::steady_clock::time_point time = TimerProxy::GetInstance().uidTimersMap_[uid][timerId]->whenElapsed;
240
241 /* 代理一个timer,可以代理成功,可以记录到proxyUid_中 */
242 bool retProxy = timerManagerHandler_->ProxyTimer(uid, true, true);
243 EXPECT_TRUE(retProxy);
244 usleep(BLOCK_TEST_TIME);
245 EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)1);
246 auto it = TimerProxy::GetInstance().proxyUids_.find(uid);
247 EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
248 EXPECT_EQ(it->second.size(), (const unsigned int)1);
249
250 /* uidTimerMap_中的触发时间成功更新,proxyUid_中可以记录老的触发时间 */
251 it = TimerProxy::GetInstance().proxyUids_.find(uid);
252 auto it2 = it->second.find(timerId);
253 EXPECT_NE(it2, it->second.end());
254 EXPECT_EQ(it2->second, time);
255
256 auto it3 = TimerProxy::GetInstance().uidTimersMap_.find(uid);
257 EXPECT_NE(it3, TimerProxy::GetInstance().uidTimersMap_.end());
258 auto it4 = it3->second.find(timerId);
259 EXPECT_NE(it4, it3->second.end());
260 EXPECT_NE(it4->second->whenElapsed, time);
261
262 /* 解代理一个timer,可以解代理成功,可以更新proxyUid_表 */
263 ret = timerManagerHandler_->ProxyTimer(uid, false, true);
264 EXPECT_TRUE(retProxy);
265 usleep(BLOCK_TEST_TIME);
266 EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)0);
267
268 /* uidTimerMap_中的触发时间被恢复回老的触发时间 */
269 auto it5 = TimerProxy::GetInstance().uidTimersMap_.find(uid);
270 EXPECT_NE(it5, TimerProxy::GetInstance().uidTimersMap_.end());
271 auto it6 = it5->second.find(timerId);
272 EXPECT_NE(it6, it5->second.end());
273 EXPECT_EQ(it6->second->whenElapsed, time);
274 timerManagerHandler_->DestroyTimer(timerId);
275 usleep(BLOCK_TEST_TIME);
276 }
277
278 /**
279 * @tc.name: ProxyTimer003
280 * @tc.desc: reset all proxy测试
281 * @tc.type: FUNC
282 */
283 HWTEST_F(TimeProxyTest, ProxyTimer003, TestSize.Level1)
284 {
285 /* 代理三个timer,可以代理成功,可以记录到proxyUid_中 */
286 int32_t uid = 2000;
287 bool retProxy = timerManagerHandler_->ProxyTimer(uid, true, true);
288 EXPECT_TRUE(retProxy);
289 usleep(BLOCK_TEST_TIME);
290 EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)1);
291 auto it = TimerProxy::GetInstance().proxyUids_.find(uid);
292 EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
293 EXPECT_EQ(it->second.size(), (const unsigned int)0);
294
295 uid = 3000;
296 retProxy = timerManagerHandler_->ProxyTimer(uid, true, true);
297 EXPECT_TRUE(retProxy);
298 usleep(BLOCK_TEST_TIME);
299 EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)2);
300 it = TimerProxy::GetInstance().proxyUids_.find(uid);
301 EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
302 EXPECT_EQ(it->second.size(), (const unsigned int)0);
303
304 uid = 4000;
305 retProxy = timerManagerHandler_->ProxyTimer(uid, true, true);
306 EXPECT_TRUE(retProxy);
307 usleep(BLOCK_TEST_TIME);
308 EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)3);
309 it = TimerProxy::GetInstance().proxyUids_.find(uid);
310 EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
311 EXPECT_EQ(it->second.size(), (const unsigned int)0);
312
313 /* 可以正常reset,且map会清空 */
314 retProxy = timerManagerHandler_->ResetAllProxy();
315 EXPECT_TRUE(retProxy);
316 EXPECT_TRUE(TimerProxy::GetInstance().proxyUids_.empty());
317 }
318 } // MiscServices
319 } // OHOS