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 "timer_manager_test.h"
17
18 #include <unistd.h>
19 #include "ddm_adapter.h"
20
21 #undef LOG_TAG
22 #define LOG_TAG "TimerManagerTest"
23
24 namespace OHOS {
25 namespace Msdp {
26 namespace DeviceStatus {
27
28 using namespace testing::ext;
29 namespace {
30 struct device_status_epoll_event {
31 int32_t fd { -1 };
32 EpollEventType event_type { EPOLL_EVENT_BEGIN };
33 };
34
35 ContextService *g_instance = nullptr;
36 constexpr int32_t TIME_WAIT_FOR_OP_MS { 100 };
37 constexpr int32_t DEFAULT_DELAY_TIME { 40 };
38 constexpr int32_t RETRY_TIME { 2 };
39 constexpr int32_t DEFAULT_TIMEOUT { 30 };
40 constexpr int32_t REPEAT_ONCE { 1 };
41 constexpr int32_t DEFAULT_UNLOAD_COOLING_TIME_MS { 600 };
42 constexpr int32_t ERROR_TIMERID { -1 };
43 constexpr size_t ERROR_REPEAT_COUNT { 128 };
44 constexpr int32_t ERROR_INTERVAL_MS { 1000000 };
45 } // namespace
46
ContextService()47 ContextService::ContextService()
48 {
49 ddm_ = std::make_unique<DDMAdapter>();
50 FI_HILOGI("OHOS_BUILD_ENABLE_INTENTION_FRAMEWORK is on");
51 OnStart();
52 }
53
~ContextService()54 ContextService::~ContextService()
55 {
56 OnStop();
57 }
58
GetDelegateTasks()59 IDelegateTasks& ContextService::GetDelegateTasks()
60 {
61 return delegateTasks_;
62 }
63
GetDeviceManager()64 IDeviceManager& ContextService::GetDeviceManager()
65 {
66 return devMgr_;
67 }
68
GetTimerManager()69 ITimerManager& ContextService::GetTimerManager()
70 {
71 return timerMgr_;
72 }
73
GetDragManager()74 IDragManager& ContextService::GetDragManager()
75 {
76 return dragMgr_;
77 }
78
GetInstance()79 __attribute__((no_sanitize("cfi"))) ContextService* ContextService::GetInstance()
80 {
81 static std::once_flag flag;
82 std::call_once(flag, [&]() {
83 ContextService *cooContext = new (std::nothrow) ContextService();
84 CHKPL(cooContext);
85 g_instance = cooContext;
86 });
87 return g_instance;
88 }
89
GetSocketSessionManager()90 ISocketSessionManager& ContextService::GetSocketSessionManager()
91 {
92 return socketSessionMgr_;
93 }
94
GetDDM()95 IDDMAdapter& ContextService::GetDDM()
96 {
97 return *ddm_;
98 }
99
GetPluginManager()100 IPluginManager& ContextService::GetPluginManager()
101 {
102 return *pluginMgr_;
103 }
104
GetInput()105 IInputAdapter& ContextService::GetInput()
106 {
107 return *input_;
108 }
109
GetDSoftbus()110 IDSoftbusAdapter& ContextService::GetDSoftbus()
111 {
112 return *dsoftbusAda_;
113 }
114
Init()115 bool ContextService::Init()
116 {
117 CALL_DEBUG_ENTER;
118 if (EpollCreate() != RET_OK) {
119 FI_HILOGE("Create epoll failed");
120 return false;
121 }
122 if (InitDelegateTasks() != RET_OK) {
123 FI_HILOGE("Delegate tasks init failed");
124 goto INIT_FAIL;
125 }
126
127 if (InitTimerMgr() != RET_OK) {
128 FI_HILOGE("TimerMgr init failed");
129 goto INIT_FAIL;
130 }
131
132 return true;
133
134 INIT_FAIL:
135 EpollClose();
136 return false;
137 }
138
InitTimerMgr()139 __attribute__((no_sanitize("cfi"))) int32_t ContextService::InitTimerMgr()
140 {
141 CALL_DEBUG_ENTER;
142 int32_t ret = timerMgr_.Init(this);
143 if (ret != RET_OK) {
144 FI_HILOGE("TimerMgr init failed");
145 return ret;
146 }
147
148 ret = AddEpoll(EPOLL_EVENT_TIMER, timerMgr_.GetTimerFd());
149 if (ret != RET_OK) {
150 FI_HILOGE("AddEpoll for timer failed");
151 }
152 return ret;
153 }
154
InitDelegateTasks()155 int32_t ContextService::InitDelegateTasks()
156 {
157 CALL_DEBUG_ENTER;
158 if (!delegateTasks_.Init()) {
159 FI_HILOGE("The delegate task init failed");
160 return RET_ERR;
161 }
162 int32_t ret = AddEpoll(EPOLL_EVENT_ETASK, delegateTasks_.GetReadFd());
163 if (ret != RET_OK) {
164 FI_HILOGE("AddEpoll error ret:%{public}d", ret);
165 }
166 FI_HILOGI("AddEpoll, epollfd:%{public}d, fd:%{public}d", epollFd_, delegateTasks_.GetReadFd());
167 return ret;
168 }
169
EpollCreate()170 int32_t ContextService::EpollCreate()
171 {
172 CALL_DEBUG_ENTER;
173 epollFd_ = ::epoll_create1(EPOLL_CLOEXEC);
174 if (epollFd_ < 0) {
175 FI_HILOGE("epoll_create1 failed:%{public}s", ::strerror(errno));
176 return RET_ERR;
177 }
178 return RET_OK;
179 }
180
AddEpoll(EpollEventType type, int32_t fd)181 int32_t ContextService::AddEpoll(EpollEventType type, int32_t fd)
182 {
183 CALL_DEBUG_ENTER;
184 if (!(type >= EPOLL_EVENT_BEGIN && type < EPOLL_EVENT_END)) {
185 FI_HILOGE("Invalid type:%{public}d", type);
186 return RET_ERR;
187 }
188 if (fd < 0) {
189 FI_HILOGE("Invalid fd:%{public}d", fd);
190 return RET_ERR;
191 }
192 auto eventData = static_cast<device_status_epoll_event*>(malloc(sizeof(device_status_epoll_event)));
193 if (!eventData) {
194 FI_HILOGE("Malloc failed");
195 return RET_ERR;
196 }
197 eventData->fd = fd;
198 eventData->event_type = type;
199 FI_HILOGD("EventData:[fd:%{public}d, type:%{public}d]", eventData->fd, eventData->event_type);
200
201 struct epoll_event ev {};
202 ev.events = EPOLLIN;
203 ev.data.ptr = eventData;
204 if (EpollCtl(fd, EPOLL_CTL_ADD, ev) != RET_OK) {
205 free(eventData);
206 eventData = nullptr;
207 ev.data.ptr = nullptr;
208 FI_HILOGE("EpollCtl failed");
209 return RET_ERR;
210 }
211 return RET_OK;
212 }
213
DelEpoll(EpollEventType type, int32_t fd)214 int32_t ContextService::DelEpoll(EpollEventType type, int32_t fd)
215 {
216 CALL_DEBUG_ENTER;
217 if (!(type >= EPOLL_EVENT_BEGIN && type < EPOLL_EVENT_END)) {
218 FI_HILOGE("Invalid type:%{public}d", type);
219 return RET_ERR;
220 }
221 if (fd < 0) {
222 FI_HILOGE("Invalid fd:%{public}d", fd);
223 return RET_ERR;
224 }
225 struct epoll_event ev {};
226 if (EpollCtl(fd, EPOLL_CTL_DEL, ev) != RET_OK) {
227 FI_HILOGE("DelEpoll failed");
228 return RET_ERR;
229 }
230 return RET_OK;
231 }
232
EpollClose()233 void ContextService::EpollClose()
234 {
235 CALL_DEBUG_ENTER;
236 if (epollFd_ >= 0) {
237 if (close(epollFd_) < 0) {
238 FI_HILOGE("Close epoll fd failed, error:%{public}s, epollFd_:%{public}d", strerror(errno), epollFd_);
239 }
240 epollFd_ = -1;
241 }
242 }
243
EpollCtl(int32_t fd, int32_t op, struct epoll_event &event)244 int32_t ContextService::EpollCtl(int32_t fd, int32_t op, struct epoll_event &event)
245 {
246 CALL_DEBUG_ENTER;
247 if (fd < 0) {
248 FI_HILOGE("Invalid fd:%{public}d", fd);
249 return RET_ERR;
250 }
251 if (epollFd_ < 0) {
252 FI_HILOGE("Invalid epollFd:%{public}d", epollFd_);
253 return RET_ERR;
254 }
255 if (::epoll_ctl(epollFd_, op, fd, &event) != 0) {
256 FI_HILOGE("epoll_ctl(%{public}d,%{public}d,%{public}d) failed:%{public}s", epollFd_, op, fd, ::strerror(errno));
257 return RET_ERR;
258 }
259 return RET_OK;
260 }
261
EpollWait(int32_t maxevents, int32_t timeout, struct epoll_event &events)262 int32_t ContextService::EpollWait(int32_t maxevents, int32_t timeout, struct epoll_event &events)
263 {
264 if (epollFd_ < 0) {
265 FI_HILOGE("Invalid epollFd:%{public}d", epollFd_);
266 return RET_ERR;
267 }
268 return epoll_wait(epollFd_, &events, maxevents, timeout);
269 }
270
OnTimeout(const struct epoll_event &ev)271 void ContextService::OnTimeout(const struct epoll_event &ev)
272 {
273 CALL_DEBUG_ENTER;
274 if ((ev.events & EPOLLIN) == EPOLLIN) {
275 uint64_t expiration {};
276 ssize_t ret = read(timerMgr_.GetTimerFd(), &expiration, sizeof(expiration));
277 if (ret < 0) {
278 FI_HILOGE("Read expiration failed:%{public}s", strerror(errno));
279 }
280 timerMgr_.ProcessTimers();
281 } else if ((ev.events & (EPOLLHUP | EPOLLERR)) != 0) {
282 FI_HILOGE("Epoll hangup:%{public}s", strerror(errno));
283 }
284 }
285
OnStart()286 void ContextService::OnStart()
287 {
288 CALL_DEBUG_ENTER;
289 uint64_t tid = GetThisThreadId();
290 delegateTasks_.SetWorkerThreadId(tid);
291
292 if (!Init()) {
293 FI_HILOGE("On start call init failed");
294 return;
295 }
296 state_ = ServiceRunningState::STATE_RUNNING;
297 ready_ = true;
298
299 worker_ = std::thread(std::bind(&ContextService::OnThread, this));
300 }
301
OnStop()302 void ContextService::OnStop()
303 {
304 CALL_DEBUG_ENTER;
305 if (timerMgr_.GetTimerFd() >= 0) {
306 if (close(timerMgr_.GetTimerFd()) < 0) {
307 FI_HILOGE("Close timer fd failed, error:%{public}s", strerror(errno));
308 }
309 }
310 if (!ready_) {
311 FI_HILOGI("ready state is false");
312 return;
313 }
314 ready_ = false;
315 state_ = ServiceRunningState::STATE_EXIT;
316
317 delegateTasks_.PostAsyncTask([]() -> int32_t {
318 FI_HILOGD("No asynchronous operations");
319 return RET_OK;
320 });
321 if (worker_.joinable()) {
322 worker_.join();
323 }
324 EpollClose();
325 FI_HILOGI("OnStop leave");
326 }
327
OnThread()328 void ContextService::OnThread()
329 {
330 CALL_DEBUG_ENTER;
331 SetThreadName(std::string("os_ds_service"));
332 uint64_t tid = GetThisThreadId();
333 delegateTasks_.SetWorkerThreadId(tid);
334 FI_HILOGD("Main worker thread start, tid:%{public}" PRId64 "", tid);
335
336 while (state_ == ServiceRunningState::STATE_RUNNING) {
337 struct epoll_event ev[MAX_EVENT_SIZE] {};
338 int32_t count = EpollWait(MAX_EVENT_SIZE, -1, ev[0]);
339 for (int32_t i = 0; i < count && state_ == ServiceRunningState::STATE_RUNNING; i++) {
340 auto epollEvent = reinterpret_cast<device_status_epoll_event*>(ev[i].data.ptr);
341 CHKPC(epollEvent);
342 if (epollEvent->event_type == EPOLL_EVENT_TIMER) {
343 OnTimeout(ev[i]);
344 } else if (epollEvent->event_type == EPOLL_EVENT_ETASK) {
345 OnDelegateTask(ev[i]);
346 } else {
347 FI_HILOGW("Unknown epoll event type:%{public}d", epollEvent->event_type);
348 }
349 }
350 }
351 FI_HILOGD("Main worker thread stop, tid:%{public}" PRId64 "", tid);
352 }
353
OnDelegateTask(const struct epoll_event &ev)354 void ContextService::OnDelegateTask(const struct epoll_event &ev)
355 {
356 if ((ev.events & EPOLLIN) == 0) {
357 FI_HILOGW("Not epollin");
358 return;
359 }
360 DelegateTasks::TaskData data {};
361 ssize_t res = read(delegateTasks_.GetReadFd(), &data, sizeof(data));
362 if (res == -1) {
363 FI_HILOGW("Read failed erron:%{public}d", errno);
364 }
365 FI_HILOGD("RemoteRequest notify td:%{public}" PRId64 ", std:%{public}" PRId64 ""
366 ", taskId:%{public}d", GetThisThreadId(), data.tid, data.taskId);
367 delegateTasks_.ProcessTasks();
368 }
369
SetUpTestCase()370 void TimerManagerTest::SetUpTestCase() {}
371
TearDownTestCase()372 void TimerManagerTest::TearDownTestCase()
373 {
374 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
375 }
376
SetUp()377 void TimerManagerTest::SetUp() {}
378
TearDown()379 void TimerManagerTest::TearDown() {}
380
381 /**
382 * @tc.name: TimerManagerTest_AddTimer001
383 * @tc.desc: Test AddTimer, Parameter correct expected success
384 * @tc.type: FUNC
385 */
HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer001, TestSize.Level1)386 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer001, TestSize.Level1)
387 {
388 CALL_TEST_DEBUG;
389 auto env = ContextService::GetInstance();
390 ASSERT_NE(env, nullptr);
391
392 timerId_ = env->GetTimerManager().AddTimer(DEFAULT_DELAY_TIME, RETRY_TIME, [this, env]() {
393 if (timerInfo_.times == 0) {
394 FI_HILOGI("It will be retry to call callback next time");
395 timerInfo_.times++;
396 return;
397 }
398 env->GetTimerManager().RemoveTimer(timerInfo_.timerId);
399 });
400 if (timerId_ < 0) {
401 FI_HILOGE("AddTimer failed");
402 } else {
403 FI_HILOGI("Add the timer %{public}d success", timerId_);
404 }
405
406 timerInfo_.timerId = timerId_;
407 timerInfo_.times = 0;
408
409 EXPECT_GE(timerId_, 0);
410 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS * RETRY_TIME));
411 timerId_ = -1;
412 timerInfo_.timerId = -1;
413 }
414
415 /**
416 * @tc.name: TimerManagerTest_AddTimer002
417 * @tc.desc: Test AddTimer, Parameter correct expected success
418 * @tc.type: FUNC
419 */
HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer002, TestSize.Level1)420 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer002, TestSize.Level1)
421 {
422 CALL_TEST_DEBUG;
423 auto env = ContextService::GetInstance();
424 ASSERT_NE(env, nullptr);
425 timerId_ = env->GetTimerManager().AddTimer(DEFAULT_TIMEOUT, REPEAT_ONCE, [this, env]() {
426 FI_HILOGI("Timer %{public}d excute one times", timerId_);
427 EXPECT_GE(timerId_, 0);
428 env->GetTimerManager().RemoveTimer(timerId_);
429 });
430 if (timerId_ < 0) {
431 FI_HILOGE("AddTimer failed");
432 } else {
433 FI_HILOGI("Add the timer %{public}d success", timerId_);
434 }
435
436 EXPECT_GE(timerId_, 0);
437 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
438 timerId_ = -1;
439 }
440
441 /**
442 * @tc.name: TimerManagerTest_AddTimer003
443 * @tc.desc: Test AddTimer, Parameter correct expected success
444 * @tc.type: FUNC
445 */
HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer003, TestSize.Level1)446 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer003, TestSize.Level1)
447 {
448 CALL_TEST_DEBUG;
449 auto env = ContextService::GetInstance();
450 ASSERT_NE(env, nullptr);
451
452 timerId_ = env->GetTimerManager().AddTimer(DEFAULT_TIMEOUT, REPEAT_ONCE, [this, env]() {
453 if (timerId_ >= 0) {
454 env->GetTimerManager().RemoveTimer(timerId_);
455 EXPECT_GE(timerId_, 0);
456 }
457 });
458
459 if (timerId_ < 0) {
460 FI_HILOGE("AddTimer failed");
461 } else {
462 FI_HILOGI("Add the timer %{public}d success", timerId_);
463 }
464 EXPECT_GE(timerId_, 0);
465 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
466 timerId_ = -1;
467 }
468
469 /**
470 * @tc.name: TimerManagerTest_AddTimer004
471 * @tc.desc: Test AddTimer, Invalid number of repetitions, expected failure
472 * @tc.type: FUNC
473 */
HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer004, TestSize.Level1)474 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer004, TestSize.Level1)
475 {
476 CALL_TEST_DEBUG;
477 auto env = ContextService::GetInstance();
478 ASSERT_NE(env, nullptr);
479 timerId_ = env->GetTimerManager().AddTimer(DEFAULT_TIMEOUT, ERROR_REPEAT_COUNT, [this, env]() {
480 FI_HILOGI("Timer %{public}d excute onetimes", timerId_);
481 env->GetTimerManager().RemoveTimer(timerId_);
482 EXPECT_GE(timerId_, 0);
483 timerId_ = -1;
484 });
485 if (timerId_ < 0) {
486 FI_HILOGI("Invalid repeat-count value, then error, so success");
487 } else {
488 FI_HILOGE("Invalid repeat-count value, but okay, so failed");
489 }
490
491 EXPECT_GE(timerId_, 0);
492 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
493 }
494
495 /**
496 * @tc.name: TimerManagerTest_AddTimer005
497 * @tc.desc: Test AddTimer, Invalid interval time, expected failure
498 * @tc.type: FUNC
499 */
HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer005, TestSize.Level1)500 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer005, TestSize.Level1)
501 {
502 CALL_TEST_DEBUG;
503 auto env = ContextService::GetInstance();
504 ASSERT_NE(env, nullptr);
505 timerId_ = env->GetTimerManager().AddTimer(ERROR_INTERVAL_MS, REPEAT_ONCE, [this, env]() {
506 FI_HILOGI("Timer %{public}d excute onetimes", timerId_);
507 env->GetTimerManager().RemoveTimer(timerId_);
508 EXPECT_GE(timerId_, 0);
509 });
510 if (timerId_ < 0) {
511 FI_HILOGI("Invalid interval value, then error, so success");
512 } else {
513 FI_HILOGE("Invalid interval value, but okay, so failed");
514 }
515
516 EXPECT_GE(timerId_, 0);
517 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
518 timerId_ = -1;
519 }
520
521 /**
522 * @tc.name: TimerManagerTest_AddTimer006
523 * @tc.desc: Test AddTimer, Invalid callback function, expected failure
524 * @tc.type: FUNC
525 */
HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer006, TestSize.Level1)526 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer006, TestSize.Level1)
527 {
528 CALL_TEST_DEBUG;
529 auto env = ContextService::GetInstance();
530 ASSERT_NE(env, nullptr);
531 timerId_ = env->GetTimerManager().AddTimer(ERROR_INTERVAL_MS, REPEAT_ONCE, nullptr);
532 if (timerId_ < 0) {
533 FI_HILOGI("Invalid callback value, then error, so success");
534 } else {
535 FI_HILOGE("Invalid callback value, but okay, so failed");
536 }
537
538 EXPECT_LT(timerId_, 0);
539 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
540 timerId_ = -1;
541 }
542
543 /**
544 * @tc.name: TimerManagerTest_GetTimerFd001
545 * @tc.desc: Test GetTimerFd, Obtaining initialized TimerFd, expected success
546 * @tc.type: FUNC
547 */
HWTEST_F(TimerManagerTest, TimerManagerTest_GetTimerFd001, TestSize.Level1)548 HWTEST_F(TimerManagerTest, TimerManagerTest_GetTimerFd001, TestSize.Level1)
549 {
550 CALL_TEST_DEBUG;
551 auto env = ContextService::GetInstance();
552 ASSERT_NE(env, nullptr);
553 TimerManager *timerMgr = static_cast<TimerManager *>(&env->GetTimerManager());
554 int32_t timerFd = timerMgr->GetTimerFd();
555 if (timerFd < 0) {
556 FI_HILOGE("AddTimer failed");
557 } else {
558 FI_HILOGI("Add the timer %{public}d success", timerId_);
559 }
560 EXPECT_GE(timerFd, 0);
561 }
562
563 /**
564 * @tc.name: TimerManagerTest_GetTimerFd002
565 * @tc.desc: Test GetTimerFd, Uninitialized, directly obtaining TimerFd, expected failure
566 * @tc.type: FUNC
567 */
HWTEST_F(TimerManagerTest, TimerManagerTest_GetTimerFd002, TestSize.Level1)568 HWTEST_F(TimerManagerTest, TimerManagerTest_GetTimerFd002, TestSize.Level1)
569 {
570 CALL_TEST_DEBUG;
571 TimerManager timerMgr;
572 int32_t timerFd = timerMgr.GetTimerFd();
573 if (timerFd < 0) {
574 FI_HILOGI("TimerFd is less than zero. the value is %{public}d", timerFd);
575 } else {
576 FI_HILOGE("Get TimerFd failed. the value is %{public}d", timerFd);
577 }
578 EXPECT_LT(timerFd, 0);
579 }
580
581 /**
582 * @tc.name: TimerManagerTest_IsExist001
583 * @tc.desc: Test IsExist, The newly added clock ID has been determined to exist and is expected to succeed
584 * @tc.type: FUNC
585 */
HWTEST_F(TimerManagerTest, TimerManagerTest_IsExist001, TestSize.Level1)586 HWTEST_F(TimerManagerTest, TimerManagerTest_IsExist001, TestSize.Level1)
587 {
588 CALL_TEST_DEBUG;
589 auto env = ContextService::GetInstance();
590 ASSERT_NE(env, nullptr);
591
592 timerId_ = env->GetTimerManager().AddTimer(DEFAULT_TIMEOUT, REPEAT_ONCE, [this, env]() {
593 if (timerId_ >= 0) {
594 env->GetTimerManager().RemoveTimer(timerId_);
595 EXPECT_GE(timerId_, 0);
596 }
597 });
598
599 if (timerId_ < 0) {
600 FI_HILOGE("AddTimer failed");
601 } else {
602 FI_HILOGI("Add the timer %{public}d success", timerId_);
603 }
604 EXPECT_GE(timerId_, 0);
605 TimerManager *timerMgr = static_cast<TimerManager *>(&env->GetTimerManager());
606 bool exist = timerMgr->IsExist(timerId_);
607 if (exist) {
608 FI_HILOGI("timerId_ is exist, so success");
609 } else {
610 FI_HILOGE("timerId_ is exist, but response unexist, so failed");
611 }
612 EXPECT_TRUE(exist);
613
614 exist = timerMgr->IsExist(ERROR_TIMERID);
615 if (!exist) {
616 FI_HILOGI("The TimerFd(-1) does not exist, so success");
617 } else {
618 FI_HILOGE("The TimerFd(-1) does not exist, but response exist, so failed");
619 }
620 EXPECT_FALSE(exist);
621 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
622 timerId_ = -1;
623 }
624
625 /**
626 * @tc.name: TimerManagerTest_IsExist002
627 * @tc.desc: Test IsExist, Invalid clock ID, determine if it does not exist
628 * @tc.type: FUNC
629 */
HWTEST_F(TimerManagerTest, TimerManagerTest_IsExist002, TestSize.Level1)630 HWTEST_F(TimerManagerTest, TimerManagerTest_IsExist002, TestSize.Level1)
631 {
632 CALL_TEST_DEBUG;
633 TimerManager timerMgr;
634 bool exist = timerMgr.IsExist(ERROR_TIMERID);
635
636 if (!exist) {
637 FI_HILOGI("The TimerFd(-1) is not exist, so success");
638 } else {
639 FI_HILOGE("The TimerFd(-1) is not exist, but response exist, so failed");
640 }
641 EXPECT_FALSE(exist);
642 }
643
644 /**
645 * @tc.name: TimerManagerTest_ResetTimer001
646 * @tc.desc: Test ResetTimer, After adding the clock and resetting it, expected success
647 * @tc.type: FUNC
648 */
HWTEST_F(TimerManagerTest, TimerManagerTest_ResetTimer001, TestSize.Level1)649 HWTEST_F(TimerManagerTest, TimerManagerTest_ResetTimer001, TestSize.Level1)
650 {
651 CALL_TEST_DEBUG;
652 auto env = ContextService::GetInstance();
653 ASSERT_NE(env, nullptr);
654
655 timerId_ = env->GetTimerManager().AddTimer(DEFAULT_UNLOAD_COOLING_TIME_MS, REPEAT_ONCE, [this, env]() {
656 if (timerId_ >= 0) {
657 env->GetTimerManager().RemoveTimer(timerId_);
658 EXPECT_GE(timerId_, 0);
659 }
660 });
661
662 if (timerId_ < 0) {
663 FI_HILOGE("AddTimer failed");
664 } else {
665 TimerManager *timerMgr = static_cast<TimerManager *>(&env->GetTimerManager());
666 int32_t ret = timerMgr->ResetTimer(timerId_);
667 if (ret == RET_OK) {
668 FI_HILOGI("Reset timer success");
669 } else {
670 FI_HILOGI("Reset timer %{public}d failed", timerId_);
671 }
672 EXPECT_EQ(ret, RET_OK);
673 }
674 EXPECT_GE(timerId_, 0);
675 std::this_thread::sleep_for(std::chrono::milliseconds(DEFAULT_UNLOAD_COOLING_TIME_MS));
676 timerId_ = -1;
677 }
678
679 /**
680 * @tc.name: TimerManagerTest_ResetTimer002
681 * @tc.desc: Test ResetTimer, Reset after deleting clock, expected failure
682 * @tc.type: FUNC
683 */
HWTEST_F(TimerManagerTest, TimerManagerTest_ResetTimer002, TestSize.Level1)684 HWTEST_F(TimerManagerTest, TimerManagerTest_ResetTimer002, TestSize.Level1)
685 {
686 CALL_TEST_DEBUG;
687 auto env = ContextService::GetInstance();
688 ASSERT_NE(env, nullptr);
689
690 timerId_ = env->GetTimerManager().AddTimer(DEFAULT_TIMEOUT, REPEAT_ONCE, [this, env]() {
691 if (timerId_ >= 0) {
692 env->GetTimerManager().RemoveTimer(timerId_);
693 EXPECT_GE(timerId_, 0);
694 TimerManager *timerMgr = static_cast<TimerManager *>(&env->GetTimerManager());
695 int32_t ret = timerMgr->ResetTimer(timerId_);
696 if (ret == RET_ERR) {
697 FI_HILOGI("Reset unexist timerid sucess");
698 } else {
699 FI_HILOGE("Reset unexist timerid %{public}d failed", timerId_);
700 }
701 EXPECT_EQ(ret, RET_ERR);
702 }
703 });
704
705 if (timerId_ < 0) {
706 FI_HILOGE("AddTimer failed");
707 } else {
708 FI_HILOGI("AddTimer success");
709 }
710 EXPECT_GE(timerId_, 0);
711 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
712 timerId_ = -1;
713 }
714
715 /**
716 * @tc.name: TimerManagerTest_RemoveTimer001
717 * @tc.desc: Test RemoveTimer, Repeated deletion of clock, first successful, others failed
718 * @tc.type: FUNC
719 */
HWTEST_F(TimerManagerTest, TimerManagerTest_RemoveTimer001, TestSize.Level1)720 HWTEST_F(TimerManagerTest, TimerManagerTest_RemoveTimer001, TestSize.Level1)
721 {
722 CALL_TEST_DEBUG;
723 auto env = ContextService::GetInstance();
724 ASSERT_NE(env, nullptr);
725
726 timerId_ = env->GetTimerManager().AddTimer(DEFAULT_TIMEOUT, REPEAT_ONCE, [this, env]() {
727 if (timerId_ >= 0) {
728 int32_t ret = env->GetTimerManager().RemoveTimer(timerId_);
729 ret = env->GetTimerManager().RemoveTimer(timerId_);
730 if (ret == RET_ERR) {
731 FI_HILOGI("Remove timer two times, then error, this case success");
732 } else {
733 FI_HILOGE("Remove timer two times, but okay, this case failed");
734 }
735 EXPECT_EQ(ret, RET_ERR);
736 }
737 });
738
739 if (timerId_ < 0) {
740 FI_HILOGE("AddTimer failed");
741 } else {
742 FI_HILOGI("AddTimer success");
743 }
744 EXPECT_GE(timerId_, 0);
745 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
746 timerId_ = -1;
747 }
748 } // namespace DeviceStatus
749 } // namespace Msdp
750 } // namespace OHOS