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 */
386 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer001, TestSize.Level1)
387 {
388 CALL_TEST_DEBUG;
389 auto env = ContextService::GetInstance();
390 ASSERT_NE(env, nullptr);
391
__anonf642abcf0402() 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 */
420 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer002, TestSize.Level1)
421 {
422 CALL_TEST_DEBUG;
423 auto env = ContextService::GetInstance();
424 ASSERT_NE(env, nullptr);
__anonf642abcf0502() 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 */
446 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer003, TestSize.Level1)
447 {
448 CALL_TEST_DEBUG;
449 auto env = ContextService::GetInstance();
450 ASSERT_NE(env, nullptr);
451
__anonf642abcf0602() 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 */
474 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer004, TestSize.Level1)
475 {
476 CALL_TEST_DEBUG;
477 auto env = ContextService::GetInstance();
478 ASSERT_NE(env, nullptr);
__anonf642abcf0702() 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 */
500 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimer005, TestSize.Level1)
501 {
502 CALL_TEST_DEBUG;
503 auto env = ContextService::GetInstance();
504 ASSERT_NE(env, nullptr);
__anonf642abcf0802() 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 */
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_AddTimerAsync001
545 * @tc.desc: Test AddTimer, Parameter correct expected success
546 * @tc.type: FUNC
547 */
548 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimerAsync001, TestSize.Level1)
549 {
550 CALL_TEST_DEBUG;
551 auto env = ContextService::GetInstance();
552 ASSERT_NE(env, nullptr);
553
__anonf642abcf0902() 554 timerId_ = env->GetTimerManager().AddTimerAsync(DEFAULT_DELAY_TIME, RETRY_TIME, [this, env]() {
555 if (timerInfo_.times == 0) {
556 FI_HILOGI("It will be retry to call callback next time");
557 timerInfo_.times++;
558 } else {
559 env->GetTimerManager().RemoveTimerAsync(timerInfo_.timerId);
560 }
561 });
562 if (timerId_ < 0) {
563 FI_HILOGE("AddTimer failed");
564 } else {
565 FI_HILOGI("Add the timer %{public}d success", timerId_);
566 }
567
568 timerInfo_.timerId = timerId_;
569 timerInfo_.times = 0;
570
571 EXPECT_GE(timerId_, 0);
572 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS * RETRY_TIME));
573 timerId_ = -1;
574 timerInfo_.timerId = -1;
575 }
576
577 /**
578 * @tc.name: TimerManagerTest_AddTimerAsync002
579 * @tc.desc: Test AddTimer, Parameter correct expected success
580 * @tc.type: FUNC
581 */
582 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimerAsync002, TestSize.Level1)
583 {
584 CALL_TEST_DEBUG;
585 auto env = ContextService::GetInstance();
586 ASSERT_NE(env, nullptr);
587
__anonf642abcf0a02() 588 timerId_ = env->GetTimerManager().AddTimerAsync(DEFAULT_TIMEOUT, REPEAT_ONCE, [this, env]() {
589 FI_HILOGI("Timer %{public}d excute one times", timerId_);
590 EXPECT_GE(timerId_, 0);
591 env->GetTimerManager().RemoveTimerAsync(timerId_);
592 });
593 if (timerId_ < 0) {
594 FI_HILOGE("AddTimer failed");
595 } else {
596 FI_HILOGI("Add the timer %{public}d success", timerId_);
597 }
598
599 EXPECT_GE(timerId_, 0);
600 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
601 timerId_ = -1;
602 }
603
604 /**
605 * @tc.name: TimerManagerTest_AddTimerAsync003
606 * @tc.desc: Test AddTimer, Parameter correct expected success
607 * @tc.type: FUNC
608 */
609 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimerAsync003, TestSize.Level1)
610 {
611 CALL_TEST_DEBUG;
612 auto env = ContextService::GetInstance();
613 ASSERT_NE(env, nullptr);
614
__anonf642abcf0b02() 615 timerId_ = env->GetTimerManager().AddTimerAsync(DEFAULT_TIMEOUT, REPEAT_ONCE, [this, env]() {
616 if (timerId_ >= 0) {
617 env->GetTimerManager().RemoveTimerAsync(timerId_);
618 EXPECT_GE(timerId_, 0);
619 }
620 });
621
622 if (timerId_ < 0) {
623 FI_HILOGE("AddTimer failed");
624 } else {
625 FI_HILOGI("Add the timer %{public}d success", timerId_);
626 }
627
628 EXPECT_GE(timerId_, 0);
629 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
630 timerId_ = -1;
631 }
632
633 /**
634 * @tc.name: TimerManagerTest_AddTimerAsync004
635 * @tc.desc: Test AddTimer, Invalid number of repetitions, expected failure
636 * @tc.type: FUNC
637 */
638 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimerAsync004, TestSize.Level1)
639 {
640 CALL_TEST_DEBUG;
641 auto env = ContextService::GetInstance();
642 ASSERT_NE(env, nullptr);
643
__anonf642abcf0c02() 644 timerId_ = env->GetTimerManager().AddTimerAsync(DEFAULT_TIMEOUT, ERROR_REPEAT_COUNT, [this, env]() {
645 FI_HILOGI("Timer %{public}d excute onetimes", timerId_);
646 env->GetTimerManager().RemoveTimerAsync(timerId_);
647 EXPECT_GE(timerId_, 0);
648 timerId_ = -1;
649 });
650
651 if (timerId_ < 0) {
652 FI_HILOGI("Invalid repeat-count value, then error, so success");
653 } else {
654 FI_HILOGE("Invalid repeat-count value, but okay, so failed");
655 }
656
657 EXPECT_GE(timerId_, 0);
658 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
659 }
660
661 /**
662 * @tc.name: TimerManagerTest_AddTimerAsync005
663 * @tc.desc: Test AddTimer, Invalid interval time, expected failure
664 * @tc.type: FUNC
665 */
666 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimerAsync005, TestSize.Level1)
667 {
668 CALL_TEST_DEBUG;
669 auto env = ContextService::GetInstance();
670 ASSERT_NE(env, nullptr);
671
__anonf642abcf0d02() 672 timerId_ = env->GetTimerManager().AddTimerAsync(ERROR_INTERVAL_MS, REPEAT_ONCE, [this, env]() {
673 FI_HILOGI("Timer %{public}d excute onetimes", timerId_);
674 env->GetTimerManager().RemoveTimerAsync(timerId_);
675 EXPECT_GE(timerId_, 0);
676 });
677 if (timerId_ < 0) {
678 FI_HILOGI("Invalid interval value, then error, so success");
679 } else {
680 FI_HILOGE("Invalid interval value, but okay, so failed");
681 }
682
683 EXPECT_GE(timerId_, 0);
684 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
685 timerId_ = -1;
686 }
687
688 /**
689 * @tc.name: TimerManagerTest_AddTimerAsync006
690 * @tc.desc: Test AddTimer, Invalid callback function, expected failure
691 * @tc.type: FUNC
692 */
693 HWTEST_F(TimerManagerTest, TimerManagerTest_AddTimerAsync006, TestSize.Level1)
694 {
695 CALL_TEST_DEBUG;
696 auto env = ContextService::GetInstance();
697 ASSERT_NE(env, nullptr);
698 timerId_ = env->GetTimerManager().AddTimerAsync(ERROR_INTERVAL_MS, REPEAT_ONCE, nullptr);
699 if (timerId_ < 0) {
700 FI_HILOGI("Invalid callback value, then error, so success");
701 } else {
702 FI_HILOGE("Invalid callback value, but okay, so failed");
703 }
704
705 EXPECT_GE(timerId_, 0);
706 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
707 timerId_ = -1;
708 }
709
710 /**
711 * @tc.name: TimerManagerTest_GetTimerFd001
712 * @tc.desc: Test GetTimerFd, Obtaining initialized TimerFd, expected success
713 * @tc.type: FUNC
714 */
715 HWTEST_F(TimerManagerTest, TimerManagerTest_GetTimerFd001, TestSize.Level1)
716 {
717 CALL_TEST_DEBUG;
718 auto env = ContextService::GetInstance();
719 ASSERT_NE(env, nullptr);
720 TimerManager *timerMgr = static_cast<TimerManager *>(&env->GetTimerManager());
721 int32_t timerFd = timerMgr->GetTimerFd();
722 if (timerFd < 0) {
723 FI_HILOGE("AddTimer failed");
724 } else {
725 FI_HILOGI("Add the timer %{public}d success", timerId_);
726 }
727 EXPECT_GE(timerFd, 0);
728 }
729
730 /**
731 * @tc.name: TimerManagerTest_GetTimerFd002
732 * @tc.desc: Test GetTimerFd, Uninitialized, directly obtaining TimerFd, expected failure
733 * @tc.type: FUNC
734 */
735 HWTEST_F(TimerManagerTest, TimerManagerTest_GetTimerFd002, TestSize.Level1)
736 {
737 CALL_TEST_DEBUG;
738 TimerManager timerMgr;
739 int32_t timerFd = timerMgr.GetTimerFd();
740 if (timerFd < 0) {
741 FI_HILOGI("TimerFd is less than zero. the value is %{public}d", timerFd);
742 } else {
743 FI_HILOGE("Get TimerFd failed. the value is %{public}d", timerFd);
744 }
745 EXPECT_LT(timerFd, 0);
746 }
747
748 /**
749 * @tc.name: TimerManagerTest_IsExist001
750 * @tc.desc: Test IsExist, The newly added clock ID has been determined to exist and is expected to succeed
751 * @tc.type: FUNC
752 */
753 HWTEST_F(TimerManagerTest, TimerManagerTest_IsExist001, TestSize.Level1)
754 {
755 CALL_TEST_DEBUG;
756 auto env = ContextService::GetInstance();
757 ASSERT_NE(env, nullptr);
758
__anonf642abcf0e02() 759 timerId_ = env->GetTimerManager().AddTimer(DEFAULT_TIMEOUT, REPEAT_ONCE, [this, env]() {
760 if (timerId_ >= 0) {
761 env->GetTimerManager().RemoveTimer(timerId_);
762 EXPECT_GE(timerId_, 0);
763 }
764 });
765
766 if (timerId_ < 0) {
767 FI_HILOGE("AddTimer failed");
768 } else {
769 FI_HILOGI("Add the timer %{public}d success", timerId_);
770 }
771 EXPECT_GE(timerId_, 0);
772 TimerManager *timerMgr = static_cast<TimerManager *>(&env->GetTimerManager());
773 bool exist = timerMgr->IsExist(timerId_);
774 if (exist) {
775 FI_HILOGI("timerId_ is exist, so success");
776 } else {
777 FI_HILOGE("timerId_ is exist, but response unexist, so failed");
778 }
779 EXPECT_TRUE(exist);
780
781 exist = timerMgr->IsExist(ERROR_TIMERID);
782 if (!exist) {
783 FI_HILOGI("The TimerFd(-1) does not exist, so success");
784 } else {
785 FI_HILOGE("The TimerFd(-1) does not exist, but response exist, so failed");
786 }
787 EXPECT_FALSE(exist);
788 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
789 timerId_ = -1;
790 }
791
792 /**
793 * @tc.name: TimerManagerTest_IsExist002
794 * @tc.desc: Test IsExist, Invalid clock ID, determine if it does not exist
795 * @tc.type: FUNC
796 */
797 HWTEST_F(TimerManagerTest, TimerManagerTest_IsExist002, TestSize.Level1)
798 {
799 CALL_TEST_DEBUG;
800 TimerManager timerMgr;
801 bool exist = timerMgr.IsExist(ERROR_TIMERID);
802
803 if (!exist) {
804 FI_HILOGI("The TimerFd(-1) is not exist, so success");
805 } else {
806 FI_HILOGE("The TimerFd(-1) is not exist, but response exist, so failed");
807 }
808 EXPECT_FALSE(exist);
809 }
810
811 /**
812 * @tc.name: TimerManagerTest_ResetTimer001
813 * @tc.desc: Test ResetTimer, After adding the clock and resetting it, expected success
814 * @tc.type: FUNC
815 */
816 HWTEST_F(TimerManagerTest, TimerManagerTest_ResetTimer001, TestSize.Level1)
817 {
818 CALL_TEST_DEBUG;
819 auto env = ContextService::GetInstance();
820 ASSERT_NE(env, nullptr);
821
__anonf642abcf0f02() 822 timerId_ = env->GetTimerManager().AddTimer(DEFAULT_UNLOAD_COOLING_TIME_MS, REPEAT_ONCE, [this, env]() {
823 if (timerId_ >= 0) {
824 env->GetTimerManager().RemoveTimer(timerId_);
825 EXPECT_GE(timerId_, 0);
826 }
827 });
828
829 if (timerId_ < 0) {
830 FI_HILOGE("AddTimer failed");
831 } else {
832 TimerManager *timerMgr = static_cast<TimerManager *>(&env->GetTimerManager());
833 int32_t ret = timerMgr->ResetTimer(timerId_);
834 if (ret == RET_OK) {
835 FI_HILOGI("Reset timer success");
836 } else {
837 FI_HILOGI("Reset timer %{public}d failed", timerId_);
838 }
839 EXPECT_EQ(ret, RET_OK);
840 }
841 EXPECT_GE(timerId_, 0);
842 std::this_thread::sleep_for(std::chrono::milliseconds(DEFAULT_UNLOAD_COOLING_TIME_MS));
843 timerId_ = -1;
844 }
845
846 /**
847 * @tc.name: TimerManagerTest_ResetTimer002
848 * @tc.desc: Test ResetTimer, Reset after deleting clock, expected failure
849 * @tc.type: FUNC
850 */
851 HWTEST_F(TimerManagerTest, TimerManagerTest_ResetTimer002, TestSize.Level1)
852 {
853 CALL_TEST_DEBUG;
854 auto env = ContextService::GetInstance();
855 ASSERT_NE(env, nullptr);
856
__anonf642abcf1002() 857 timerId_ = env->GetTimerManager().AddTimer(DEFAULT_TIMEOUT, REPEAT_ONCE, [this, env]() {
858 if (timerId_ >= 0) {
859 env->GetTimerManager().RemoveTimer(timerId_);
860 EXPECT_GE(timerId_, 0);
861 TimerManager *timerMgr = static_cast<TimerManager *>(&env->GetTimerManager());
862 int32_t ret = timerMgr->ResetTimer(timerId_);
863 if (ret == RET_ERR) {
864 FI_HILOGI("Reset unexist timerid sucess");
865 } else {
866 FI_HILOGE("Reset unexist timerid %{public}d failed", timerId_);
867 }
868 EXPECT_EQ(ret, RET_ERR);
869 }
870 });
871
872 if (timerId_ < 0) {
873 FI_HILOGE("AddTimer failed");
874 } else {
875 FI_HILOGI("AddTimer success");
876 }
877 EXPECT_GE(timerId_, 0);
878 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
879 timerId_ = -1;
880 }
881
882 /**
883 * @tc.name: TimerManagerTest_RemoveTimer001
884 * @tc.desc: Test RemoveTimer, Repeated deletion of clock, first successful, others failed
885 * @tc.type: FUNC
886 */
887 HWTEST_F(TimerManagerTest, TimerManagerTest_RemoveTimer001, TestSize.Level1)
888 {
889 CALL_TEST_DEBUG;
890 auto env = ContextService::GetInstance();
891 ASSERT_NE(env, nullptr);
892
__anonf642abcf1102() 893 timerId_ = env->GetTimerManager().AddTimer(DEFAULT_TIMEOUT, REPEAT_ONCE, [this, env]() {
894 if (timerId_ >= 0) {
895 int32_t ret = env->GetTimerManager().RemoveTimer(timerId_);
896 ret = env->GetTimerManager().RemoveTimer(timerId_);
897 if (ret == RET_ERR) {
898 FI_HILOGI("Remove timer two times, then error, this case success");
899 } else {
900 FI_HILOGE("Remove timer two times, but okay, this case failed");
901 }
902 EXPECT_EQ(ret, RET_ERR);
903 }
904 });
905
906 if (timerId_ < 0) {
907 FI_HILOGE("AddTimer failed");
908 } else {
909 FI_HILOGI("AddTimer success");
910 }
911 EXPECT_GE(timerId_, 0);
912 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
913 timerId_ = -1;
914 }
915 } // namespace DeviceStatus
916 } // namespace Msdp
917 } // namespace OHOS