• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "time_common.h"
22 
23 namespace OHOS {
24 namespace MiscServices {
25 using namespace testing::ext;
26 using namespace std::chrono;
27 
28 namespace {
29 constexpr uint64_t NANO_TO_MILESECOND = 100000;
30 constexpr int BLOCK_TEST_TIME = 100000;
31 const uint64_t TIMER_ID = 88887;
32 const int UID = 999996;
33 const int PID = 999997;
34 }
35 TimerManager* timerManagerHandler_ = nullptr;
36 
37 class TimeProxyTest : public testing::Test {
38 public:
39     static void SetUpTestCase(void);
40 
41     static void TearDownTestCase(void);
42 
43     void SetUp();
44 
45     void TearDown();
46 };
47 
SetUpTestCase(void)48 void TimeProxyTest::SetUpTestCase(void)
49 {}
50 
TearDownTestCase(void)51 void TimeProxyTest::TearDownTestCase(void)
52 {}
53 
SetUp(void)54 void TimeProxyTest::SetUp(void)
55 {
56     TIME_HILOGI(TIME_MODULE_SERVICE, "start SetUp.");
57     timerManagerHandler_ = TimerManager::GetInstance();
58     EXPECT_NE(timerManagerHandler_, nullptr);
59     TIME_HILOGI(TIME_MODULE_SERVICE, "end SetUp.");
60     usleep(BLOCK_TEST_TIME);
61 }
62 
TearDown(void)63 void TimeProxyTest::TearDown(void)
64 {
65     TIME_HILOGI(TIME_MODULE_SERVICE, "start TearDown.");
66     timerManagerHandler_ = nullptr;
67     TIME_HILOGI(TIME_MODULE_SERVICE, "end TearDown.");
68 }
69 
70 /**
71 * @tc.name: UidTimerMap001
72 * @tc.desc: 启动timer时uid timer map数据更新测试
73 * @tc.type: FUNC
74 */
75 HWTEST_F(TimeProxyTest, UidTimerMap001, TestSize.Level1)
76 {
77     /* 创建一个timer,可以创建成功 */
78     TimerPara paras;
79     paras.timerType = 2;
80     paras.windowLength = -1;
81     paras.interval = 0;
82     paras.flag = 0;
83     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
84     int32_t uid = 2000;
85     int pid = 1000;
86     uint64_t timerId = 0;
__anon7af3b1330202(const uint64_t) 87     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
88                                                     wantAgent, uid, pid, timerId, NOT_STORE);
89     EXPECT_EQ(ret, TimeError::E_TIME_OK);
90     usleep(BLOCK_TEST_TIME);
91 
92     /* 启动一个timer, 可以启动成功,可以记录到uidTimerMap_中 */
93     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
94     uint64_t triggerTime = 10000000 + nowElapsed;
95     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
96     EXPECT_EQ(ret, TimeError::E_TIME_OK);
97     usleep(BLOCK_TEST_TIME);
98     EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)1);
99     auto itUidTimerMap = TimerProxy::GetInstance().uidTimersMap_.find(uid);
100     EXPECT_NE(itUidTimerMap, TimerProxy::GetInstance().uidTimersMap_.end());
101     EXPECT_EQ(itUidTimerMap->second.size(), (const unsigned int)1);
102     auto itTimerId = itUidTimerMap->second.find(timerId);
103     EXPECT_NE(itTimerId, itUidTimerMap->second.end());
104     EXPECT_NE(itTimerId->second, nullptr);
105 
106     /* 清理uidTimerMap_,可以清理成功 */
107     TimerProxy::GetInstance().uidTimersMap_.clear();
108     EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)0);
109     timerManagerHandler_->DestroyTimer(timerId);
110     usleep(BLOCK_TEST_TIME);
111 }
112 
113 /**
114 * @tc.name: UidTimerMap002
115 * @tc.desc: 停止timer时uid timer map数据更新测试
116 * @tc.type: FUNC
117 */
118 HWTEST_F(TimeProxyTest, UidTimerMap002, TestSize.Level1)
119 {
120     /* 创建一个timer,可以创建成功 */
121     TimerPara paras;
122     paras.timerType = 2;
123     paras.windowLength = -1;
124     paras.interval = 0;
125     paras.flag = 0;
126     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
127     int32_t uid = 2000;
128     int pid = 1000;
129     uint64_t timerId = 0;
__anon7af3b1330302(const uint64_t) 130     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
131                                                     wantAgent, uid, pid, timerId, NOT_STORE);
132     EXPECT_EQ(ret, TimeError::E_TIME_OK);
133     usleep(BLOCK_TEST_TIME);
134 
135     /* 启动一个timer, 可以启动成功,可以记录到uidTimerMap_中 */
136     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
137     uint64_t triggerTime = 10000000 + nowElapsed;
138     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
139     EXPECT_EQ(ret, TimeError::E_TIME_OK);
140     usleep(BLOCK_TEST_TIME);
141     EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)1);
142 
143     /* 停止一个timer,可以停止成功,可以从uidTimerMap_中删除 */
144     ret = timerManagerHandler_->StopTimerInner(timerId, true);
145     EXPECT_EQ(ret, TimeError::E_TIME_OK);
146     usleep(BLOCK_TEST_TIME);
147     EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)0);
148     timerManagerHandler_->DestroyTimer(timerId);
149 }
150 
151 /**
152 * @tc.name: UidTimerMap003
153 * @tc.desc: 触发timer时uid timer map数据更新测试
154 * @tc.type: FUNC
155 */
156 HWTEST_F(TimeProxyTest, UidTimerMap003, TestSize.Level1)
157 {
158     /* 创建一个timer,可以创建成功 */
159     TimerPara paras;
160     paras.timerType = 2;
161     paras.windowLength = -1;
162     paras.interval = 0;
163     paras.flag = 0;
164     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
165     int32_t uid = 2000;
166     int pid = 1000;
167     uint64_t timerId = 0;
__anon7af3b1330402(const uint64_t) 168     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
169                                                     wantAgent, uid, pid, timerId, NOT_STORE);
170 
171     EXPECT_EQ(ret, TimeError::E_TIME_OK);
172     usleep(BLOCK_TEST_TIME);
173 
174     /* 启动一个timer, 可以启动成功,可以记录到uidTimerMap_中 */
175     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
176     uint64_t triggerTime = 10000000 + nowElapsed;
177     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
178     EXPECT_EQ(ret, TimeError::E_TIME_OK);
179     usleep(BLOCK_TEST_TIME);
180     EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)1);
181 
182     /* 触发一个timer,可以触发成功,可以从uidTimerMap_中删除 */
183     std::vector<std::shared_ptr<TimerInfo>> triggerList;
184     std::shared_ptr<Batch> batch = timerManagerHandler_->alarmBatches_.at(0);
185     std::chrono::steady_clock::time_point tpRpoch(nanoseconds(1000000000));
186     batch->start_ = tpRpoch;
187     auto retTrigger = timerManagerHandler_->TriggerTimersLocked(triggerList, timerManagerHandler_->GetBootTimeNs());
188     EXPECT_EQ(retTrigger, true);
189     usleep(BLOCK_TEST_TIME);
190     EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)0);
191     timerManagerHandler_->DestroyTimer(timerId);
192 }
193 
194 /**
195 * @tc.name: PidTimerMap001
196 * @tc.desc: 启动timer时pid timer map数据更新测试
197 * @tc.type: FUNC
198 */
199 HWTEST_F(TimeProxyTest, PidTimerMap001, TestSize.Level1)
200 {
201     /* 创建一个timer,可以创建成功 */
202     TimerPara paras;
203     paras.timerType = 2;
204     paras.windowLength = -1;
205     paras.interval = 0;
206     paras.flag = 0;
207     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
208     int32_t uid = 2000;
209     int pid = 1001;
210     uint64_t timerId = 0;
__anon7af3b1330502(const uint64_t) 211     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
212                                                     wantAgent, uid, pid, timerId, NOT_STORE);
213     EXPECT_EQ(ret, TimeError::E_TIME_OK);
214     usleep(BLOCK_TEST_TIME);
215 
216     /* 启动一个timer, 可以启动成功,可以记录到PidTimerMap_中 */
217     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
218     uint64_t triggerTime = 10000000 + nowElapsed;
219     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
220     EXPECT_EQ(ret, TimeError::E_TIME_OK);
221     usleep(BLOCK_TEST_TIME);
222     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)1);
223     auto itPidTimerMap = TimerProxy::GetInstance().pidTimersMap_.find(pid);
224     EXPECT_NE(itPidTimerMap, TimerProxy::GetInstance().pidTimersMap_.end());
225     EXPECT_EQ(itPidTimerMap->second.size(), (const unsigned int)1);
226     auto itTimerId = itPidTimerMap->second.find(timerId);
227     EXPECT_NE(itTimerId, itPidTimerMap->second.end());
228     EXPECT_NE(itTimerId->second, nullptr);
229 
230     /* 清理pidTimerMap_,可以清理成功 */
231     TimerProxy::GetInstance().pidTimersMap_.clear();
232     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)0);
233     timerManagerHandler_->DestroyTimer(timerId);
234     usleep(BLOCK_TEST_TIME);
235 }
236 
237 /**
238 * @tc.name: PidTimerMap002
239 * @tc.desc: 停止timer时pid timer map数据更新测试
240 * @tc.type: FUNC
241 */
242 HWTEST_F(TimeProxyTest, PidTimerMap002, TestSize.Level1)
243 {
244     /* 创建一个timer,可以创建成功 */
245     TimerPara paras;
246     paras.timerType = 2;
247     paras.windowLength = -1;
248     paras.interval = 0;
249     paras.flag = 0;
250     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
251     int32_t uid = 2000;
252     int pid = 1000;
253     uint64_t timerId = 0;
254 
255     /* 清理pidTimersMap_,保证测试前pidTimersMap_内无其他测试中曾记录的pid影响 */
256     TimerProxy::GetInstance().pidTimersMap_.clear();
257 
__anon7af3b1330602(const uint64_t) 258     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
259                                                     wantAgent, uid, pid, timerId, NOT_STORE);
260     EXPECT_EQ(ret, TimeError::E_TIME_OK);
261     usleep(BLOCK_TEST_TIME);
262 
263     /* 启动一个timer, 可以启动成功,可以记录到pidTimerMap_中 */
264     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
265     uint64_t triggerTime = 10000000 + nowElapsed;
266     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
267     EXPECT_EQ(ret, TimeError::E_TIME_OK);
268     usleep(BLOCK_TEST_TIME);
269     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)1);
270 
271     /* 停止一个timer,可以停止成功,可以从pidTimerMap_中删除 */
272     ret = timerManagerHandler_->StopTimerInner(timerId, true);
273     EXPECT_EQ(ret, TimeError::E_TIME_OK);
274     usleep(BLOCK_TEST_TIME);
275     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)0);
276     timerManagerHandler_->DestroyTimer(timerId);
277 }
278 
279 /**
280 * @tc.name: PidTimerMap003
281 * @tc.desc: 触发timer时pid timer map数据更新测试
282 * @tc.type: FUNC
283 */
284 HWTEST_F(TimeProxyTest, PidTimerMap003, TestSize.Level1)
285 {
286     /* 创建一个timer,可以创建成功 */
287     TimerPara paras;
288     paras.timerType = 2;
289     paras.windowLength = -1;
290     paras.interval = 0;
291     paras.flag = 0;
292     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
293     int32_t uid = 2000;
294     int pid = 1002;
295     uint64_t timerId = 0;
__anon7af3b1330702(const uint64_t) 296     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
297                                                     wantAgent, uid, pid, timerId, NOT_STORE);
298     EXPECT_EQ(ret, TimeError::E_TIME_OK);
299     usleep(BLOCK_TEST_TIME);
300 
301     /* 启动一个timer, 可以启动成功,可以记录到pidTimerMap_中 */
302     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
303     uint64_t triggerTime = 10000000 + nowElapsed;
304     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
305     EXPECT_EQ(ret, TimeError::E_TIME_OK);
306     usleep(BLOCK_TEST_TIME);
307     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)1);
308 
309     /* 触发一个timer,可以触发成功,可以从pidTimerMap_中删除 */
310     std::vector<std::shared_ptr<TimerInfo>> triggerList;
311     std::shared_ptr<Batch> batch = timerManagerHandler_->alarmBatches_.at(0);
312     std::chrono::steady_clock::time_point tpRpoch(nanoseconds(1000000000));
313     batch->start_ = tpRpoch;
314     auto retTrigger = timerManagerHandler_->TriggerTimersLocked(triggerList, timerManagerHandler_->GetBootTimeNs());
315     EXPECT_EQ(retTrigger, true);
316     usleep(BLOCK_TEST_TIME);
317     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)0);
318     timerManagerHandler_->DestroyTimer(timerId);
319 }
320 
321 /**
322 * @tc.name: ProxyTimer001
323 * @tc.desc: 代理解代理基本功能测试
324 * @tc.type: FUNC
325 */
326 HWTEST_F(TimeProxyTest, ProxyTimer001, TestSize.Level1)
327 {
328     /* 代理一个timer,可以代理成功,可以记录到proxyUid_中 */
329     int32_t uid = 1000;
330     bool isProxy = true;
331     bool needRetrigger = true;
332     bool ret = timerManagerHandler_->ProxyTimer(uid, isProxy, needRetrigger);
333     EXPECT_TRUE(ret);
334     usleep(BLOCK_TEST_TIME);
335     EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)1);
336     auto it = TimerProxy::GetInstance().proxyUids_.find(uid);
337     EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
338     EXPECT_EQ(it->second.size(), (const unsigned int)0);
339 
340     /* 解代理一个timer,可以解代理成功,可以从proxyUid_中删除 */
341     isProxy = false;
342     ret = timerManagerHandler_->ProxyTimer(uid, isProxy, needRetrigger);
343     EXPECT_TRUE(ret);
344     usleep(BLOCK_TEST_TIME);
345     EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)0);
346 }
347 
348 /**
349 * @tc.name: ProxyTimer002
350 * @tc.desc: 代理解代理时proxy timer map数据更新测试
351 * @tc.type: FUNC
352 */
353 HWTEST_F(TimeProxyTest, ProxyTimer002, TestSize.Level1)
354 {
355     /* 创建一个timer,可以创建成功 */
356     TimerPara paras;
357     paras.timerType = 2;
358     paras.windowLength = -1;
359     paras.interval = 0;
360     paras.flag = 0;
361     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
362     int32_t uid = 2000;
363     int pid = 1000;
364     uint64_t timerId = 0;
__anon7af3b1330802(const uint64_t) 365     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
366                                                     wantAgent, uid, pid, timerId, NOT_STORE);
367     EXPECT_EQ(ret, TimeError::E_TIME_OK);
368     usleep(BLOCK_TEST_TIME);
369 
370     /* 启动一个timer, 可以启动成功,可以记录到uidTimerMap_中 */
371     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
372     uint64_t triggerTime = 10000000 + nowElapsed;
373     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
374     EXPECT_EQ(ret, TimeError::E_TIME_OK);
375     usleep(BLOCK_TEST_TIME);
376     EXPECT_EQ(TimerProxy::GetInstance().uidTimersMap_.size(), (const unsigned int)1);
377     std::chrono::steady_clock::time_point time = TimerProxy::GetInstance().uidTimersMap_[uid][timerId]->whenElapsed;
378 
379     /* 代理一个timer,可以代理成功,可以记录到proxyUid_中 */
380     bool retProxy = timerManagerHandler_->ProxyTimer(uid, true, true);
381     EXPECT_TRUE(retProxy);
382     usleep(BLOCK_TEST_TIME);
383     EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)1);
384     auto it = TimerProxy::GetInstance().proxyUids_.find(uid);
385     EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
386     EXPECT_EQ(it->second.size(), (const unsigned int)1);
387 
388     /* uidTimerMap_中的触发时间成功更新,proxyUid_中可以记录老的触发时间 */
389     it = TimerProxy::GetInstance().proxyUids_.find(uid);
390     auto it2 = it->second.find(timerId);
391     EXPECT_NE(it2, it->second.end());
392     EXPECT_EQ(it2->second, time);
393 
394     auto it3 = TimerProxy::GetInstance().uidTimersMap_.find(uid);
395     EXPECT_NE(it3, TimerProxy::GetInstance().uidTimersMap_.end());
396     auto it4 = it3->second.find(timerId);
397     EXPECT_NE(it4, it3->second.end());
398     EXPECT_NE(it4->second->whenElapsed, time);
399 
400     /* 解代理一个timer,可以解代理成功,可以更新proxyUid_表 */
401     ret = timerManagerHandler_->ProxyTimer(uid, false, true);
402     EXPECT_TRUE(retProxy);
403     usleep(BLOCK_TEST_TIME);
404     EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)0);
405 
406     /* uidTimerMap_中的触发时间被恢复回老的触发时间 */
407     auto it5 = TimerProxy::GetInstance().uidTimersMap_.find(uid);
408     EXPECT_NE(it5, TimerProxy::GetInstance().uidTimersMap_.end());
409     auto it6 = it5->second.find(timerId);
410     EXPECT_NE(it6, it5->second.end());
411     EXPECT_EQ(it6->second->whenElapsed, time);
412     timerManagerHandler_->DestroyTimer(timerId);
413     usleep(BLOCK_TEST_TIME);
414 }
415 
416 /**
417 * @tc.name: ProxyTimer003
418 * @tc.desc: reset all proxy测试
419 * @tc.type: FUNC
420 */
421 HWTEST_F(TimeProxyTest, ProxyTimer003, TestSize.Level1)
422 {
423     /* 代理三个timer,可以代理成功,可以记录到proxyUid_中 */
424     int32_t uid = 2000;
425     bool retProxy = timerManagerHandler_->ProxyTimer(uid, true, true);
426     EXPECT_TRUE(retProxy);
427     usleep(BLOCK_TEST_TIME);
428     EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)1);
429     auto it = TimerProxy::GetInstance().proxyUids_.find(uid);
430     EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
431     EXPECT_EQ(it->second.size(), (const unsigned int)0);
432 
433     uid = 3000;
434     retProxy = timerManagerHandler_->ProxyTimer(uid, true, true);
435     EXPECT_TRUE(retProxy);
436     usleep(BLOCK_TEST_TIME);
437     EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)2);
438     it = TimerProxy::GetInstance().proxyUids_.find(uid);
439     EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
440     EXPECT_EQ(it->second.size(), (const unsigned int)0);
441 
442     uid = 4000;
443     retProxy = timerManagerHandler_->ProxyTimer(uid, true, true);
444     EXPECT_TRUE(retProxy);
445     usleep(BLOCK_TEST_TIME);
446     EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int)3);
447     it = TimerProxy::GetInstance().proxyUids_.find(uid);
448     EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
449     EXPECT_EQ(it->second.size(), (const unsigned int)0);
450 
451     /* 可以正常reset,且map会清空 */
452     retProxy = timerManagerHandler_->ResetAllProxy();
453     EXPECT_TRUE(retProxy);
454     EXPECT_TRUE(TimerProxy::GetInstance().proxyUids_.empty());
455 }
456 
457 /**
458 * @tc.name: AdjustTimer001
459 * @tc.desc: adjust timer test
460 * @tc.type: FUNC
461 */
462 HWTEST_F(TimeProxyTest, AdjustTimer001, TestSize.Level1)
463 {
464     /* The system timers can be aligned to a unified time and recorded in adjustTimers_. */
465     bool isAdjust = true;
466     uint32_t interval = 100;
467     bool ret = timerManagerHandler_->AdjustTimer(isAdjust, interval);
468     EXPECT_TRUE(ret);
469     usleep(BLOCK_TEST_TIME);
470     EXPECT_EQ(TimerProxy::GetInstance().adjustTimers_.size(), (const unsigned int)0);
471 
472     /* The unified heartbeat can be deleted successfully and deleted from adjustTimers_. */
473     isAdjust = false;
474     interval = 0;
475     ret = timerManagerHandler_->AdjustTimer(isAdjust, interval);
476     EXPECT_TRUE(ret);
477     usleep(BLOCK_TEST_TIME);
478     EXPECT_EQ(TimerProxy::GetInstance().adjustTimers_.size(), (const unsigned int)0);
479 }
480 
481 /**
482 * @tc.name: AdjustTimer002
483 * @tc.desc: set timer exemption
484 * @tc.type: FUNC
485 */
486 HWTEST_F(TimeProxyTest, AdjustTimer002, TestSize.Level1)
487 {
488     /* Create a timer with windowLen set to 0. */
489     TimerPara paras{.timerType = 2, .windowLength = 0, .interval = 0, .flag = 0};
490     auto wantAgent = std::make_shared<OHOS::AbilityRuntime::WantAgent::WantAgent>();
491     int32_t uid = 2000;
492     int32_t pid = 1000;
493     uint64_t timerId = 0;
__anon7af3b1330902(const uint64_t) 494     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
495                                                      wantAgent, uid, pid, timerId, NOT_STORE);
496     EXPECT_EQ(ret, TimeError::E_TIME_OK);
497     usleep(BLOCK_TEST_TIME);
498 
499     /* Create a timer */
500     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
501     uint64_t triggerTime = 10000000 + nowElapsed;
502     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
503     EXPECT_EQ(ret, TimeError::E_TIME_OK);
504     usleep(BLOCK_TEST_TIME);
505 
506     /* Exempt the timer of the app and update the record to adjustExemptionList_. */
507     std::unordered_set<std::string> nameArr{"time_service"};
508     timerManagerHandler_->SetTimerExemption(nameArr, true);
509     usleep(BLOCK_TEST_TIME);
510     EXPECT_NE(TimerProxy::GetInstance().adjustExemptionList_.size(), (const unsigned int)0);
511 
512     /* Unified heartbeat is triggered. The heartbeat of exempted applications is not unified. */
513     bool isAdjust = true;
514     uint32_t interval = 200;
515     bool adjustRet = timerManagerHandler_->AdjustTimer(isAdjust, interval);
516     EXPECT_TRUE(adjustRet);
517     usleep(BLOCK_TEST_TIME);
518     EXPECT_NE(TimerProxy::GetInstance().adjustTimers_.size(), (const unsigned int)0);
519     bool isExemption = true;
520     for (auto timer : TimerProxy::GetInstance().adjustTimers_) {
521         if (timer->bundleName == "time_service") {
522             isExemption = false;
523         }
524     }
525     EXPECT_TRUE(isExemption);
526 }
527 
528 /**
529 * @tc.name: PidProxyTimer001
530 * @tc.desc: 代理解代理基本功能测试
531 * @tc.type: FUNC
532 */
533 HWTEST_F(TimeProxyTest, PidProxyTimer001, TestSize.Level1)
534 {
535     /* 代理一个timer,可以代理成功,可以记录到proxyUid_中 */
536     int pid = 1003;
537     int uid = 2003;
538     std::set<int> pidList;
539     pidList.insert(pid);
540     bool isProxy = true;
541     bool needRetrigger = true;
542     bool ret = timerManagerHandler_->ProxyTimer(uid, pidList, isProxy, needRetrigger);
543     EXPECT_TRUE(ret);
544     usleep(BLOCK_TEST_TIME);
545     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)1);
546     auto it = TimerProxy::GetInstance().proxyPids_.find(pid);
547     EXPECT_NE(it, TimerProxy::GetInstance().proxyPids_.end());
548     EXPECT_EQ(it->second.size(), (const unsigned int)0);
549 
550     /* 解代理一个timer,可以解代理成功,可以从proxyPid_中删除 */
551     isProxy = false;
552     ret = timerManagerHandler_->ProxyTimer(uid, pidList, isProxy, needRetrigger);
553     EXPECT_TRUE(ret);
554     usleep(BLOCK_TEST_TIME);
555     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)0);
556 }
557 
558 /**
559 * @tc.name: PidProxyTimer002
560 * @tc.desc: 代理解代理时proxy timer map数据更新测试
561 * @tc.type: FUNC
562 */
563 HWTEST_F(TimeProxyTest, PidProxyTimer002, TestSize.Level1)
564 {
565     TimerPara paras;
566     paras.timerType = 2;
567     paras.windowLength = -1;
568     paras.interval = 0;
569     paras.flag = 0;
570     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
571     int32_t uid = 2000;
572     int pid = 1004;
573     std::set<int> pidList;
574     pidList.insert(pid);
575     uint64_t timerId = 0;
576 
577     /* 清理pidTimersMap_,保证测试前pidTimersMap_内无其他测试中曾记录的pid影响 */
578     TimerProxy::GetInstance().pidTimersMap_.clear();
579 
__anon7af3b1330a02(const uint64_t) 580     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
581                                                     wantAgent, uid, pid, timerId, NOT_STORE);
582     EXPECT_EQ(ret, TimeError::E_TIME_OK);
583 
584     /* 启动一个timer, 可以启动成功,可以记录到pidTimerMap_中 */
585     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
586     uint64_t triggerTime = 10000000 + nowElapsed;
587     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
588     EXPECT_EQ(ret, TimeError::E_TIME_OK);
589     usleep(BLOCK_TEST_TIME);
590     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)1);
591     std::chrono::steady_clock::time_point time = TimerProxy::GetInstance().pidTimersMap_[pid][timerId]->whenElapsed;
592 
593     /* 代理一个timer,可以代理成功,可以记录到proxyPid_中 */
594     bool retProxy = timerManagerHandler_->ProxyTimer(uid, pidList, true, true);
595     EXPECT_TRUE(retProxy);
596     usleep(BLOCK_TEST_TIME);
597     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)1);
598     auto it = TimerProxy::GetInstance().proxyPids_.find(pid);
599     EXPECT_NE(it, TimerProxy::GetInstance().proxyPids_.end());
600     EXPECT_EQ(it->second.size(), (const unsigned int)1);
601 
602     /* pidTimerMap_中的触发时间成功更新,proxyPid_中可以记录老的触发时间 */
603     it = TimerProxy::GetInstance().proxyPids_.find(pid);
604     auto it2 = it->second.find(timerId);
605     EXPECT_NE(it2, it->second.end());
606     EXPECT_EQ(it2->second, time);
607 
608     auto it3 = TimerProxy::GetInstance().pidTimersMap_.find(pid);
609     EXPECT_NE(it3, TimerProxy::GetInstance().pidTimersMap_.end());
610     auto it4 = it3->second.find(timerId);
611     EXPECT_NE(it4, it3->second.end());
612     EXPECT_NE(it4->second->whenElapsed, time);
613 
614     /* 解代理一个timer,可以解代理成功,可以更新proxyPid_表 */
615     ret = timerManagerHandler_->ProxyTimer(uid, pidList, false, true);
616     EXPECT_TRUE(retProxy);
617     usleep(BLOCK_TEST_TIME);
618     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)0);
619 
620     /* pidTimerMap_中的触发时间被恢复回老的触发时间 */
621     auto it5 = TimerProxy::GetInstance().pidTimersMap_.find(pid);
622     EXPECT_NE(it5, TimerProxy::GetInstance().pidTimersMap_.end());
623     auto it6 = it5->second.find(timerId);
624     EXPECT_NE(it6, it5->second.end());
625     EXPECT_EQ(it6->second->whenElapsed, time);
626     timerManagerHandler_->DestroyTimer(timerId);
627 }
628 
629 /**
630 * @tc.name: PidProxyTimer003
631 * @tc.desc: reset all proxy测试
632 * @tc.type: FUNC
633 */
634 HWTEST_F(TimeProxyTest, PidProxyTimer003, TestSize.Level1)
635 {
636     int uid = 1000;
637     /* 代理三个timer,可以代理成功,可以记录到proxyPid_中 */
638     int pid1 = 2000;
639     std::set<int> pidList;
640     pidList.insert(pid1);
641 
642     int pid2 = 3000;
643     pidList.insert(pid2);
644 
645     int pid3 = 4000;
646     pidList.insert(pid3);
647 
648     bool retProxy = timerManagerHandler_->ProxyTimer(uid, pidList, true, true);
649     EXPECT_TRUE(retProxy);
650     usleep(BLOCK_TEST_TIME);
651     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)3);
652     auto it = TimerProxy::GetInstance().proxyPids_.find(pid1);
653     EXPECT_NE(it, TimerProxy::GetInstance().proxyPids_.end());
654     it = TimerProxy::GetInstance().proxyPids_.find(pid2);
655     EXPECT_NE(it, TimerProxy::GetInstance().proxyPids_.end());
656     it = TimerProxy::GetInstance().proxyPids_.find(pid3);
657     EXPECT_NE(it, TimerProxy::GetInstance().proxyPids_.end());
658     EXPECT_EQ(it->second.size(), (const unsigned int)0);
659 
660     /* 可以正常reset,且map会清空 */
661     retProxy = timerManagerHandler_->ResetAllProxy();
662     EXPECT_TRUE(retProxy);
663     EXPECT_TRUE(TimerProxy::GetInstance().proxyPids_.empty());
664 }
665 
666 /**
667 * @tc.name: PidProxyTimer004
668 * @tc.desc: test proxy of same pid but different uid.
669 * @tc.type: FUNC
670 */
671 HWTEST_F(TimeProxyTest, PidProxyTimer004, TestSize.Level1)
672 {
673     TimerPara paras;
674     paras.timerType = 2;
675     paras.windowLength = -1;
676     paras.interval = 0;
677     paras.flag = 0;
678     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
679     int32_t uid1 = 2000;
680     int32_t uid2 = 2001;
681     int pid = 1000;
682     uint64_t timerId1 = 0;
683     uint64_t timerId2 = 0;
684 
685     TimerProxy::GetInstance().pidTimersMap_.clear();
686     TimerProxy::GetInstance().proxyPids_.clear();
687     /* create timer by uid1 */
__anon7af3b1330b02(const uint64_t) 688     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
689                                                     wantAgent, uid1, pid, timerId1, NOT_STORE);
690     EXPECT_EQ(ret, TimeError::E_TIME_OK);
691     usleep(BLOCK_TEST_TIME);
692     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
693     uint64_t triggerTime = 10000000 + nowElapsed;
694     ret = timerManagerHandler_->StartTimer(timerId1, triggerTime);
695     EXPECT_EQ(ret, TimeError::E_TIME_OK);
696     usleep(BLOCK_TEST_TIME);
697     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_[pid].size(), (const unsigned int)1);
698 
699     /* create timer by uid2 */
__anon7af3b1330c02(const uint64_t) 700     ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
701                                                     wantAgent, uid2, pid, timerId2, NOT_STORE);
702     EXPECT_EQ(ret, TimeError::E_TIME_OK);
703     usleep(BLOCK_TEST_TIME);
704     nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
705     triggerTime = 10000000 + nowElapsed;
706     ret = timerManagerHandler_->StartTimer(timerId2, triggerTime);
707     EXPECT_EQ(ret, TimeError::E_TIME_OK);
708     usleep(BLOCK_TEST_TIME);
709     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_[pid].size(), (const unsigned int)2);
710 
711     std::set<int> pidList;
712     pidList.insert(pid);
713     /* proxy uid1 expect proxyPids_ only has one element */
714     ret = timerManagerHandler_->ProxyTimer(uid1, pidList, true, true);
715     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)1);
716 }
717 
718 /**
719 * @tc.name: AdjustTimerProxy001
720 * @tc.desc: Determine whether to unify the heartbeat when the timer proxy is disabled.
721 * @tc.type: FUNC
722 */
723 HWTEST_F(TimeProxyTest, AdjustTimerProxy001, TestSize.Level1)
724 {
725     TimerPara paras;
726     paras.timerType = 2;
727     paras.windowLength = -1;
728     paras.interval = 0;
729     paras.flag = 0;
730     auto wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>();
731     int32_t uid = 2001;
732     int pid = 1111;
733     std::set<int> pidList;
734     pidList.insert(pid);
735     uint64_t timerId = 0;
736 
737     /* clear pidTimersMap_ */
738     TimerProxy::GetInstance().pidTimersMap_.clear();
739     TimerProxy::GetInstance().proxyPids_.clear();
740 
__anon7af3b1330d02(const uint64_t) 741     int32_t ret = timerManagerHandler_->CreateTimer(paras, [] (const uint64_t) {return 0;},
742                                                     wantAgent, uid, pid, timerId, NOT_STORE);
743     EXPECT_EQ(ret, TimeError::E_TIME_OK);
744 
745     /* Start a timer. The timer can be started successfully and can be recorded in pidTimerMap_. */
746     auto nowElapsed = timerManagerHandler_->GetBootTimeNs().time_since_epoch().count() / NANO_TO_MILESECOND;
747     uint64_t triggerTime = 10000000 + nowElapsed;
748     ret = timerManagerHandler_->StartTimer(timerId, triggerTime);
749     EXPECT_EQ(ret, TimeError::E_TIME_OK);
750     usleep(BLOCK_TEST_TIME);
751     EXPECT_EQ(TimerProxy::GetInstance().pidTimersMap_.size(), (const unsigned int)1);
752 
753     /* The proxy of a timer is successful and can be recorded in proxyPid_. */
754     bool retProxy = timerManagerHandler_->ProxyTimer(uid, pidList, true, true);
755     EXPECT_TRUE(retProxy);
756     usleep(BLOCK_TEST_TIME);
757     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)1);
758     auto it = TimerProxy::GetInstance().proxyPids_.find(pid);
759     EXPECT_NE(it, TimerProxy::GetInstance().proxyPids_.end());
760     EXPECT_EQ(it->second.size(), (const unsigned int)1);
761 
762     /* Cancel a proxy timer. The proxy is canceled successfully, and the proxyPid_ table is updated. */
763     ret = timerManagerHandler_->ProxyTimer(uid, pidList, false, true);
764     EXPECT_TRUE(retProxy);
765     usleep(BLOCK_TEST_TIME);
766     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)0);
767 
768     /* After the proxy is disabled, determine whether unified heartbeat is required again. */
769     bool isAdjust = true;
770     uint32_t interval = 300;
771     bool adjret = timerManagerHandler_->AdjustTimer(isAdjust, interval);
772     EXPECT_TRUE(adjret);
773     EXPECT_NE(TimerProxy::GetInstance().adjustTimers_.size(), (const unsigned int)0);
774 }
775 
776 /**
777 * @tc.name: ProxyTimerCover001
778 * @tc.desc: test CallbackAlarmIfNeed
779 * @tc.type: FUNC
780 */
781 HWTEST_F(TimeProxyTest, ProxyTimerCover001, TestSize.Level1)
782 {
783     auto res = TimerProxy::GetInstance().CallbackAlarmIfNeed(nullptr);
784     EXPECT_EQ(res, E_TIME_NULLPTR);
785 }
786 
787 /**
788 * @tc.name: ProxyTimerCover002
789 * @tc.desc: test UID
790 * @tc.type: FUNC
791 */
792 HWTEST_F(TimeProxyTest, ProxyTimerCover002, TestSize.Level1)
793 {
794     bool retProxy = timerManagerHandler_->ProxyTimer(UID, true, true);
795     EXPECT_TRUE(retProxy);
796     usleep(BLOCK_TEST_TIME);
797     {
798         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().proxyMutex_);
799         EXPECT_EQ(TimerProxy::GetInstance().proxyUids_.size(), (const unsigned int) 1);
800         auto it = TimerProxy::GetInstance().proxyUids_.find(UID);
801         EXPECT_NE(it, TimerProxy::GetInstance().proxyUids_.end());
802         EXPECT_EQ(it->second.size(), (const unsigned int) 0);
803     }
804 
805     auto duration = std::chrono::milliseconds::zero();
806     auto timePoint = std::chrono::steady_clock::now();
807     auto timerInfo1 = std::make_shared<TimerInfo>(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration,
808                                                  nullptr, nullptr, 0, UID, 0, "");
809     auto res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1);
810     EXPECT_EQ(res, E_TIME_OK);
811     auto timerInfo2 = std::make_shared<TimerInfo>(TIMER_ID + 1, 0, duration, timePoint, duration, timePoint, duration,
812                                                  nullptr, nullptr, 0, UID, 0, "");
813     res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo2);
814     EXPECT_EQ(res, E_TIME_OK);
815 
816     TimerProxy::GetInstance().RemoveProxy(TIMER_ID, UID);
817     TimerProxy::GetInstance().RemoveProxy(TIMER_ID + 1, UID);
818 
819     {
820         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().proxyMutex_);
821         auto it = TimerProxy::GetInstance().proxyMap_.find(UID);
822         EXPECT_EQ(it, TimerProxy::GetInstance().proxyMap_.end());
823     }
824 
825     res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1);
826     EXPECT_EQ(res, E_TIME_OK);
827     retProxy = timerManagerHandler_->ProxyTimer(UID, false, true);
828     EXPECT_TRUE(retProxy);
829 
830     retProxy = timerManagerHandler_->ProxyTimer(UID, true, true);
831     EXPECT_TRUE(retProxy);
832     usleep(BLOCK_TEST_TIME);
833     res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1);
834     EXPECT_EQ(res, E_TIME_OK);
835 
836     TimerProxy::GetInstance().ResetProxyMaps();
837 
838     TimerProxy::GetInstance().EraseTimerFromProxyUidMap(0, UID);
839 }
840 
841 
842 /**
843 * @tc.name: ProxyTimerCover003
844 * @tc.desc: test PID
845 * @tc.type: FUNC
846 */
847 HWTEST_F(TimeProxyTest, ProxyTimerCover003, TestSize.Level1)
848 {
849     TimerProxy::GetInstance().pidTimersMap_.clear();
850     TimerProxy::GetInstance().proxyPids_.clear();
851     int uid = 2000;
852     std::set<int> pidList;
853     pidList.insert(PID);
854     bool retProxy = timerManagerHandler_->ProxyTimer(uid, pidList, true, true);
855     EXPECT_TRUE(retProxy);
856     usleep(BLOCK_TEST_TIME);
857     EXPECT_EQ(TimerProxy::GetInstance().proxyPids_.size(), (const unsigned int)1);
858     auto it = TimerProxy::GetInstance().proxyPids_.find(PID);
859     EXPECT_NE(it, TimerProxy::GetInstance().proxyPids_.end());
860 
861     auto duration = std::chrono::milliseconds::zero();
862     auto timePoint = std::chrono::steady_clock::now();
863     auto timerInfo1 = std::make_shared<TimerInfo>(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration,
864                                                   nullptr, nullptr, 0, 0, PID, "");
865     auto res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1);
866     EXPECT_EQ(res, E_TIME_OK);
867     auto timerInfo2 = std::make_shared<TimerInfo>(TIMER_ID + 1, 0, duration, timePoint, duration, timePoint, duration,
868                                                   nullptr, nullptr, 0, 0, PID, "");
869     res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo2);
870     EXPECT_EQ(res, E_TIME_OK);
871 
872     TimerProxy::GetInstance().RemovePidProxy(TIMER_ID, PID);
873     TimerProxy::GetInstance().RemovePidProxy(TIMER_ID + 1, PID);
874 
875     {
876         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().proxyPidMutex_);
877         auto it = TimerProxy::GetInstance().proxyPidMap_.find(UID);
878         EXPECT_EQ(it, TimerProxy::GetInstance().proxyPidMap_.end());
879     }
880 
881     res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1);
882     EXPECT_EQ(res, E_TIME_OK);
883     retProxy = timerManagerHandler_->ProxyTimer(uid, pidList, false, true);
884     EXPECT_TRUE(retProxy);
885 
886     retProxy = timerManagerHandler_->ProxyTimer(uid, pidList, true, true);
887     EXPECT_TRUE(retProxy);
888     usleep(BLOCK_TEST_TIME);
889     res = TimerProxy::GetInstance().CallbackAlarmIfNeed(timerInfo1);
890     EXPECT_EQ(res, E_TIME_OK);
891 
892     TimerProxy::GetInstance().ResetProxyPidMaps();
893 
894     TimerProxy::GetInstance().EraseTimerFromProxyPidMap(0, PID);
895 }
896 
897 /**
898 * @tc.name: ProxyTimerCover004
899 * @tc.desc: test CallbackAlarmIfNeed
900 * @tc.type: FUNC
901 */
902 HWTEST_F(TimeProxyTest, ProxyTimerCover004, TestSize.Level1)
903 {
904     TimerProxy::GetInstance().RecordUidTimerMap(nullptr, false);
905     TimerProxy::GetInstance().RemoveUidTimerMap(nullptr);
906 
907     auto duration = std::chrono::milliseconds::zero();
908     auto timePoint = std::chrono::steady_clock::now();
909     auto timerInfo = std::make_shared<TimerInfo>(TIMER_ID, 0, duration, timePoint, duration, timePoint, duration,
910                                                   nullptr, nullptr, 0, UID, PID, "");
911     TimerProxy::GetInstance().RecordUidTimerMap(timerInfo, false);
912     {
913         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().uidTimersMutex_);
914         auto it = TimerProxy::GetInstance().uidTimersMap_.find(UID);
915         EXPECT_NE(it, TimerProxy::GetInstance().uidTimersMap_.end());
916     }
917     TimerProxy::GetInstance().RemoveUidTimerMap(timerInfo);
918     {
919         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().uidTimersMutex_);
920         auto it = TimerProxy::GetInstance().uidTimersMap_.find(UID);
921         EXPECT_EQ(it, TimerProxy::GetInstance().uidTimersMap_.end());
922     }
923 
924     TimerProxy::GetInstance().RecordPidTimerMap(nullptr, false);
925     TimerProxy::GetInstance().RemovePidTimerMap(nullptr);
926 
927     TimerProxy::GetInstance().RecordPidTimerMap(timerInfo, false);
928     {
929         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().pidTimersMutex_);
930         auto it = TimerProxy::GetInstance().pidTimersMap_.find(PID);
931         EXPECT_NE(it, TimerProxy::GetInstance().pidTimersMap_.end());
932     }
933     TimerProxy::GetInstance().RemovePidTimerMap(timerInfo);
934     {
935         std::lock_guard<std::mutex> lock(TimerProxy::GetInstance().pidTimersMutex_);
936         auto it = TimerProxy::GetInstance().pidTimersMap_.find(PID);
937         EXPECT_EQ(it, TimerProxy::GetInstance().pidTimersMap_.end());
938     }
939 }
940 
941 }  // MiscServices
942 }  // OHOS