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 #define private public
17 #include "epoll_manager_test.h"
18
19 #include <sys/timerfd.h>
20 #include <unistd.h>
21
22 #include "devicestatus_common.h"
23 #include "fi_log.h"
24
25 #undef LOG_TAG
26 #define LOG_TAG "EpollManagerTest"
27
28 namespace OHOS {
29 namespace Msdp {
30 namespace DeviceStatus {
31 using namespace testing::ext;
32 namespace {
33 constexpr int32_t MAX_N_EVENTS { 64 };
34 constexpr int32_t TIMEOUT { 30 };
35 constexpr int32_t BLOCK_EPOLL { -1 };
36 constexpr int32_t UNBLOCK_EPOLL { 0 };
37 constexpr int32_t EVENTS_COUNT { 10 };
38 constexpr int32_t TIME_WAIT_FOR_OP_MS { 1001 };
39 constexpr int32_t DISPATCH_TIMES { 5 };
40 constexpr int32_t EXPIRE_TIME { 2 };
41 } // namespace
42
SetUpTestCase()43 void EpollManagerTest::SetUpTestCase() {}
44
TearDownTestCase()45 void EpollManagerTest::TearDownTestCase() {}
46
MonitorEvent()47 MonitorEvent::MonitorEvent()
48 {
49 inotifyFd_ = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
50 if (inotifyFd_ < 0) {
51 FI_HILOGE("timerfd_create failed");
52 }
53 }
54
~MonitorEvent()55 MonitorEvent::~MonitorEvent()
56 {
57 if (inotifyFd_ < 0) {
58 FI_HILOGE("Invalid timerFd_");
59 return;
60 }
61 if (close(inotifyFd_) < 0) {
62 FI_HILOGE("Close timer fd failed, error:%{public}s, timerFd_:%{public}d", strerror(errno), inotifyFd_);
63 }
64 inotifyFd_ = -1;
65 }
66
Dispatch(const struct epoll_event & ev)67 void MonitorEvent::Dispatch(const struct epoll_event &ev)
68 {
69 CALL_DEBUG_ENTER;
70 if (inotifyFd_ < 0) {
71 FI_HILOGE("inotifyFd_ is invalid.");
72 return;
73 }
74 if ((ev.events & EPOLLIN) == EPOLLIN) {
75 int32_t buf[EXPIRE_TIME] = { 0 };
76 int32_t ret = read(inotifyFd_, buf, sizeof(buf));
77 if (ret < 0) {
78 FI_HILOGE("epoll callback read error");
79 } else {
80 FI_HILOGI("Epoll input, buf[0]: %{public}d, buf[1]: %{public}d", buf[0], buf[1]);
81 }
82 } else if ((ev.events & (EPOLLHUP | EPOLLERR)) != 0) {
83 FI_HILOGE("Epoll hangup, errno:%{public}s", strerror(errno));
84 }
85 }
86
SetTimer()87 int32_t MonitorEvent::SetTimer()
88 {
89 if (inotifyFd_ < 0) {
90 FI_HILOGE("TimerManager is not initialized");
91 return RET_ERR;
92 }
93 struct itimerspec tspec {};
94 tspec.it_interval = {1, 0}; // period timeout value = 1s
95 tspec.it_value = {1, 0}; // initial timeout value = 1s0ns
96 if (timerfd_settime(inotifyFd_, 0, &tspec, NULL) != 0) {
97 FI_HILOGE("Timer: the inotifyFd_ is error");
98 return RET_ERR;
99 }
100 return RET_OK;
101 }
102
SetUp()103 void EpollManagerTest::SetUp() {}
104
TearDown()105 void EpollManagerTest::TearDown() {}
106
107 /**
108 * @tc.name: EpollManagerTest_Open001
109 * @tc.desc: Test Open, open and close once
110 * @tc.type: FUNC
111 */
112 HWTEST_F(EpollManagerTest, EpollManagerTest_Open001, TestSize.Level1)
113 {
114 CALL_TEST_DEBUG;
115 EpollManager epollMgr;
116 int32_t ret = epollMgr.Open();
117 EXPECT_EQ(ret, RET_OK);
118 if (ret != RET_OK) {
119 FI_HILOGE("Open epoll-manager failed");
120 return;
121 }
122 MonitorEvent monitor;
123 ret = epollMgr.Add(monitor);
124 if (ret != RET_OK) {
125 FI_HILOGE("Add listening event failed");
126 }
127 epollMgr.Close();
128 EXPECT_EQ(ret, RET_OK);
129 }
130
131 /**
132 * @tc.name: EpollManagerTest_Open002
133 * @tc.desc: Test Open, open and close twice
134 * @tc.type: FUNC
135 */
136 HWTEST_F(EpollManagerTest, EpollManagerTest_Open002, TestSize.Level1)
137 {
138 CALL_TEST_DEBUG;
139 EpollManager epollMgr;
140 int32_t ret = epollMgr.Open();
141 EXPECT_EQ(ret, RET_OK);
142 if (ret != RET_OK) {
143 FI_HILOGE("Open epoll-manager failed");
144 return;
145 }
146 epollMgr.Close();
147 EXPECT_EQ(ret, RET_OK);
148
149 ret = epollMgr.Open();
150 EXPECT_EQ(ret, RET_OK);
151 if (ret != RET_OK) {
152 FI_HILOGE("Open epoll-manager failed");
153 return;
154 }
155 epollMgr.Close();
156 EXPECT_EQ(ret, RET_OK);
157 }
158
159 /**
160 * @tc.name: EpollManagerTest_Close001
161 * @tc.desc: Test Close
162 * @tc.type: FUNC
163 */
164 HWTEST_F(EpollManagerTest, EpollManagerTest_Close001, TestSize.Level1)
165 {
166 CALL_TEST_DEBUG;
167 EpollManager epollMgr;
168 epollMgr.Close();
169 int32_t ret = epollMgr.Open();
170 EXPECT_EQ(ret, RET_OK);
171 if (ret != RET_OK) {
172 FI_HILOGE("Open epoll-manager failed");
173 return;
174 }
175 epollMgr.Close();
176 EXPECT_EQ(ret, RET_OK);
177 }
178
179 /**
180 * @tc.name: EpollManagerTest_GetFd001
181 * @tc.desc: Test GetFd
182 * @tc.type: FUNC
183 */
184 HWTEST_F(EpollManagerTest, EpollManagerTest_GetFd001, TestSize.Level1)
185 {
186 CALL_TEST_DEBUG;
187 EpollManager epollMgr;
188 int32_t ret = epollMgr.GetFd();
189 if (ret < 0) {
190 FI_HILOGI("Get the fd which does not exist, then error, so success");
191 } else {
192 FI_HILOGE("Get the fd which does not exist, but okay, so failed");
193 }
194 EXPECT_NE(ret, RET_OK);
195
196 ret = epollMgr.Open();
197 EXPECT_EQ(ret, RET_OK);
198 if (ret != RET_OK) {
199 FI_HILOGE("Open epoll-manager failed");
200 return;
201 }
202 ret = epollMgr.GetFd();
203 if (ret < 0) {
204 FI_HILOGE("Get fd failed");
205 } else {
206 FI_HILOGI("Get the fd %{public}d success", ret);
207 }
208 epollMgr.Close();
209 EXPECT_GE(ret, RET_OK);
210 }
211
212 /**
213 * @tc.name: EpollManagerTest_Add001
214 * @tc.desc: Test Add, Add duplicate events
215 * @tc.type: FUNC
216 */
217 HWTEST_F(EpollManagerTest, EpollManagerTest_Add001, TestSize.Level1)
218 {
219 CALL_TEST_DEBUG;
220 EpollManager epollMgr;
221 int32_t ret = epollMgr.Open();
222 EXPECT_EQ(ret, RET_OK);
223 if (ret != RET_OK) {
224 FI_HILOGE("Open epoll-manager failed");
225 return;
226 }
227 MonitorEvent monitor;
228 ret = epollMgr.Add(monitor);
229 if (ret != RET_OK) {
230 FI_HILOGE("Add listening event failed");
231 }
232 ret = epollMgr.Add(monitor);
233 if (ret == RET_ERR) {
234 FI_HILOGI("Do not repeat add the same event, so success");
235 } else {
236 FI_HILOGE("Add the same event but okay, so failed");
237 }
238 epollMgr.Close();
239 EXPECT_EQ(ret, RET_ERR);
240 }
241
242 /**
243 * @tc.name: EpollManagerTest_Add002
244 * @tc.desc: Test Add, Add the event with fd of -1
245 * @tc.type: FUNC
246 */
247 HWTEST_F(EpollManagerTest, EpollManagerTest_Add002, TestSize.Level1)
248 {
249 CALL_TEST_DEBUG;
250 EpollManager epollMgr;
251 int32_t ret = epollMgr.Open();
252 EXPECT_EQ(ret, RET_OK);
253 if (ret != RET_OK) {
254 FI_HILOGE("Open epoll-manager failed");
255 return;
256 }
257 MonitorEvent monitor;
258 int32_t fd = monitor.GetFd();
259 monitor.inotifyFd_ = -1;
260 ret = epollMgr.Add(monitor);
261 monitor.inotifyFd_ = fd;
262 if (ret == RET_ERR) {
263 FI_HILOGI("Add the event with fd of -1, then error, so success");
264 } else {
265 FI_HILOGE("Add the event with fd of -1, but okay, so failed");
266 }
267 epollMgr.Close();
268 EXPECT_EQ(ret, RET_ERR);
269 }
270
271 /**
272 * @tc.name: EpollManagerTest_Remove001
273 * @tc.desc: Test Remove, remove existing events
274 * @tc.type: FUNC
275 */
276 HWTEST_F(EpollManagerTest, EpollManagerTest_Remove001, TestSize.Level1)
277 {
278 CALL_TEST_DEBUG;
279 EpollManager epollMgr;
280 int32_t ret = epollMgr.Open();
281 EXPECT_EQ(ret, RET_OK);
282 if (ret != RET_OK) {
283 FI_HILOGE("Open epoll-manager failed");
284 return;
285 }
286 MonitorEvent monitor;
287 ret = epollMgr.Add(monitor);
288 if (ret != RET_OK) {
289 FI_HILOGE("Add listening event failed");
290 } else {
291 epollMgr.Remove(monitor);
292 FI_HILOGI("Remove the event");
293 }
294 epollMgr.Close();
295 EXPECT_EQ(ret, RET_OK);
296 }
297
298 /**
299 * @tc.name: EpollManagerTest_Remove002
300 * @tc.desc: Test Remove, remove events that have not been added
301 * @tc.type: FUNC
302 */
303 HWTEST_F(EpollManagerTest, EpollManagerTest_Remove002, TestSize.Level1)
304 {
305 CALL_TEST_DEBUG;
306 EpollManager epollMgr;
307 int32_t ret = epollMgr.Open();
308 EXPECT_EQ(ret, RET_OK);
309 if (ret != RET_OK) {
310 FI_HILOGE("Open epoll-manager failed");
311 return;
312 }
313 MonitorEvent monitor;
314 epollMgr.Remove(monitor);
315 epollMgr.Close();
316 EXPECT_EQ(ret, RET_OK);
317 }
318
319 /**
320 * @tc.name: EpollManagerTest_Remove003
321 * @tc.desc: Test Remove, remove the event with fd of -1
322 * @tc.type: FUNC
323 */
324 HWTEST_F(EpollManagerTest, EpollManagerTest_Remove003, TestSize.Level1)
325 {
326 CALL_TEST_DEBUG;
327 EpollManager epollMgr;
328 int32_t ret = epollMgr.Open();
329 EXPECT_EQ(ret, RET_OK);
330 if (ret != RET_OK) {
331 FI_HILOGE("Open epoll-manager failed");
332 return;
333 }
334 MonitorEvent monitor;
335 int32_t fd = monitor.GetFd();
336 monitor.inotifyFd_ = -1;
337 epollMgr.Remove(monitor);
338 monitor.inotifyFd_ = fd;
339 epollMgr.Close();
340 EXPECT_EQ(ret, RET_OK);
341 }
342
343 /**
344 * @tc.name: EpollManagerTest_Update001
345 * @tc.desc: Test Update
346 * @tc.type: FUNC
347 */
348 HWTEST_F(EpollManagerTest, EpollManagerTest_Update001, TestSize.Level1)
349 {
350 CALL_TEST_DEBUG;
351 EpollManager epollMgr;
352 int32_t ret = epollMgr.Open();
353 EXPECT_EQ(ret, RET_OK);
354 if (ret != RET_OK) {
355 FI_HILOGE("Open epoll-manager failed");
356 return;
357 }
358 MonitorEvent monitor;
359 ret = epollMgr.Add(monitor);
360 if (ret != RET_OK) {
361 FI_HILOGE("Add listening event failed");
362 } else {
363 uint32_t evtType = monitor.GetEvents();
364 ret = epollMgr.Update(monitor);
365 FI_HILOGI("Update the event. the eventtype is 0x%{public}x", evtType);
366 }
367 epollMgr.Close();
368 EXPECT_EQ(ret, RET_OK);
369 }
370
371 /**
372 * @tc.name: EpollManagerTest_Update002
373 * @tc.desc: Test Update, modify a non-existent event
374 * @tc.type: FUNC
375 */
376 HWTEST_F(EpollManagerTest, EpollManagerTest_Update002, TestSize.Level1)
377 {
378 CALL_TEST_DEBUG;
379 EpollManager epollMgr;
380 int32_t ret = epollMgr.Open();
381 EXPECT_EQ(ret, RET_OK);
382 if (ret != RET_OK) {
383 FI_HILOGE("Open epoll-manager failed");
384 return;
385 }
386 MonitorEvent monitor;
387 ret = epollMgr.Update(monitor);
388 if (ret == RET_ERR) {
389 FI_HILOGI("Modifying a non-existent event will result in an error, so success");
390 } else {
391 FI_HILOGE("Modify a non-existent event but okay, so failed");
392 }
393 epollMgr.Close();
394 EXPECT_EQ(ret, RET_ERR);
395 }
396
397 /**
398 * @tc.name: EpollManagerTest_WaitTimeout001
399 * @tc.desc: Test WaitTimeout
400 * @tc.type: FUNC
401 */
402 HWTEST_F(EpollManagerTest, EpollManagerTest_WaitTimeout001, TestSize.Level1)
403 {
404 CALL_TEST_DEBUG;
405 EpollManager epollMgr;
406 int32_t ret = epollMgr.Open();
407 EXPECT_EQ(ret, RET_OK);
408 if (ret != RET_OK) {
409 FI_HILOGE("Open epoll-manager failed");
410 return;
411 }
412 MonitorEvent monitor;
413 ret = epollMgr.Add(monitor);
414 if (ret != RET_OK) {
415 FI_HILOGE("Add listening event failed");
416 }
417 struct epoll_event evs[MAX_N_EVENTS];
418 int32_t cnt = epollMgr.WaitTimeout(evs, MAX_N_EVENTS, TIMEOUT);
419 FI_HILOGI("No event input, timeout will end, cnt is %{public}d", cnt);
420 epollMgr.Close();
421 EXPECT_GE(cnt, 0);
422 EXPECT_EQ(ret, RET_OK);
423 }
424
425 /**
426 * @tc.name: EpollManagerTest_WaitTimeout002
427 * @tc.desc: Test WaitTimeout, no event to wait
428 * @tc.type: FUNC
429 */
430 HWTEST_F(EpollManagerTest, EpollManagerTest_WaitTimeout002, TestSize.Level1)
431 {
432 CALL_TEST_DEBUG;
433 EpollManager epollMgr;
434 int32_t ret = epollMgr.Open();
435 EXPECT_EQ(ret, RET_OK);
436 if (ret != RET_OK) {
437 FI_HILOGE("Open epoll-manager failed");
438 return;
439 }
440 struct epoll_event evs[MAX_N_EVENTS];
441 int32_t cnt = epollMgr.WaitTimeout(evs, MAX_N_EVENTS, TIMEOUT);
442 FI_HILOGI("No event input, timeout will end, cnt is %{public}d", cnt);
443 epollMgr.Close();
444 EXPECT_GE(cnt, 0);
445 EXPECT_EQ(ret, RET_OK);
446 }
447
448 /**
449 * @tc.name: EpollManagerTest_WaitTimeout003
450 * @tc.desc: Test WaitTimeout, use of block mode
451 * @tc.type: FUNC
452 */
453 HWTEST_F(EpollManagerTest, EpollManagerTest_WaitTimeout003, TestSize.Level1)
454 {
455 CALL_TEST_DEBUG;
456 EpollManager epollMgr;
457 int32_t ret = epollMgr.Open();
458 EXPECT_EQ(ret, RET_OK);
459 if (ret != RET_OK) {
460 FI_HILOGE("Open epoll-manager failed");
461 return;
462 }
463 MonitorEvent monitor;
464 ret = monitor.SetTimer();
465 EXPECT_EQ(ret, RET_OK);
466 ret = epollMgr.Add(monitor);
467 if (ret != RET_OK) {
468 FI_HILOGE("Add listening event failed");
469 }
470 EXPECT_EQ(ret, RET_OK);
471 struct epoll_event evs[MAX_N_EVENTS];
472 int32_t cnt = epollMgr.WaitTimeout(evs, MAX_N_EVENTS, BLOCK_EPOLL);
473 FI_HILOGI("No event input, timeout will end, cnt is %{public}d", cnt);
474 epollMgr.Close();
475 EXPECT_GE(cnt, 0);
476 }
477
478 /**
479 * @tc.name: EpollManagerTest_WaitTimeout004
480 * @tc.desc: Test WaitTimeout, use of non blocking mode
481 * @tc.type: FUNC
482 */
483 HWTEST_F(EpollManagerTest, EpollManagerTest_WaitTimeout004, TestSize.Level1)
484 {
485 CALL_TEST_DEBUG;
486 EpollManager epollMgr;
487 int32_t ret = epollMgr.Open();
488 EXPECT_EQ(ret, RET_OK);
489 if (ret != RET_OK) {
490 FI_HILOGE("Open epoll-manager failed");
491 return;
492 }
493 MonitorEvent monitor;
494 ret = epollMgr.Add(monitor);
495 if (ret != RET_OK) {
496 FI_HILOGE("Add listening event failed");
497 }
498 struct epoll_event evs[MAX_N_EVENTS];
499 int32_t cnt = epollMgr.WaitTimeout(evs, MAX_N_EVENTS, UNBLOCK_EPOLL);
500 FI_HILOGI("No event input, timeout will end, cnt is %{public}d", cnt);
501 epollMgr.Close();
502 EXPECT_GE(cnt, 0);
503 EXPECT_EQ(ret, RET_OK);
504 }
505
506 /**
507 * @tc.name: EpollManagerTest_WaitTimeout005
508 * @tc.desc: Test WaitTimeout, Abnormal scenarios with small memory and multiple events
509 * @tc.type: FUNC
510 */
511 HWTEST_F(EpollManagerTest, EpollManagerTest_WaitTimeout005, TestSize.Level1)
512 {
513 CALL_TEST_DEBUG;
514 EpollManager epollMgr;
515 int32_t ret = epollMgr.Open();
516 EXPECT_EQ(ret, RET_OK);
517 if (ret != RET_OK) {
518 FI_HILOGE("Open epoll-manager failed");
519 return;
520 }
521 MonitorEvent monitor;
522 ret = epollMgr.Add(monitor);
523 if (ret != RET_OK) {
524 FI_HILOGE("Add listening event failed");
525 }
526 struct epoll_event evs[EVENTS_COUNT];
527 int32_t cnt = epollMgr.WaitTimeout(evs, MAX_N_EVENTS, TIMEOUT);
528 FI_HILOGI("No event input, timeout will end, cnt is %{public}d", cnt);
529 epollMgr.Close();
530 EXPECT_GE(cnt, 0);
531 EXPECT_EQ(ret, RET_OK);
532 }
533
534 /**
535 * @tc.name: EpollManagerTest_WaitTimeout006
536 * @tc.desc: Test WaitTimeout, Abnormal scenarios with big memory and zero events
537 * @tc.type: FUNC
538 */
539 HWTEST_F(EpollManagerTest, EpollManagerTest_WaitTimeout006, TestSize.Level1)
540 {
541 CALL_TEST_DEBUG;
542 EpollManager epollMgr;
543 int32_t ret = epollMgr.Open();
544 EXPECT_EQ(ret, RET_OK);
545 if (ret != RET_OK) {
546 FI_HILOGE("Open epoll-manager failed");
547 return;
548 }
549 MonitorEvent monitor;
550 ret = epollMgr.Add(monitor);
551 if (ret != RET_OK) {
552 FI_HILOGE("Add listening event failed");
553 }
554 struct epoll_event evs[EVENTS_COUNT];
555 int32_t cnt = epollMgr.WaitTimeout(evs, 0, TIMEOUT);
556 FI_HILOGI("No event input, timeout will end, cnt is %{public}d", cnt);
557 epollMgr.Close();
558 EXPECT_LT(cnt, 0);
559 EXPECT_EQ(ret, RET_OK);
560 }
561
562 /**
563 * @tc.name: EpollManagerTest_Wait001
564 * @tc.desc: Test Wait
565 * @tc.type: FUNC
566 */
567 HWTEST_F(EpollManagerTest, EpollManagerTest_Wait001, TestSize.Level1)
568 {
569 CALL_TEST_DEBUG;
570 EpollManager epollMgr;
571 int32_t ret = epollMgr.Open();
572 EXPECT_EQ(ret, RET_OK);
573 if (ret != RET_OK) {
574 FI_HILOGE("Open epoll-manager failed");
575 return;
576 }
577 MonitorEvent monitor;
578 ret = monitor.SetTimer();
579 EXPECT_EQ(ret, RET_OK);
580 ret = epollMgr.Add(monitor);
581 if (ret != RET_OK) {
582 FI_HILOGE("Add listening event failed");
583 }
584 struct epoll_event evs[MAX_N_EVENTS];
585 int32_t cnt = epollMgr.Wait(evs, MAX_N_EVENTS);
586 FI_HILOGI("No event input, timeout will end, cnt is %{public}d", cnt);
587 epollMgr.Close();
588 EXPECT_GE(cnt, 0);
589 EXPECT_EQ(ret, RET_OK);
590 }
591
592 /**
593 * @tc.name: EpollManagerTest_Wait002
594 * @tc.desc: Test Wait, Abnormal scenarios with small memory and multiple events
595 * @tc.type: FUNC
596 */
597 HWTEST_F(EpollManagerTest, EpollManagerTest_Wait002, TestSize.Level1)
598 {
599 CALL_TEST_DEBUG;
600 EpollManager epollMgr;
601 int32_t ret = epollMgr.Open();
602 EXPECT_EQ(ret, RET_OK);
603 if (ret != RET_OK) {
604 FI_HILOGE("Open epoll-manager failed");
605 return;
606 }
607 MonitorEvent monitor;
608 ret = monitor.SetTimer();
609 EXPECT_EQ(ret, RET_OK);
610 ret = epollMgr.Add(monitor);
611 if (ret != RET_OK) {
612 FI_HILOGE("Add listening event failed");
613 }
614 struct epoll_event evs[EVENTS_COUNT];
615 int32_t cnt = epollMgr.Wait(evs, MAX_N_EVENTS);
616 FI_HILOGI("No event input, timeout will end, cnt is %{public}d", cnt);
617 epollMgr.Close();
618 EXPECT_GE(cnt, 0);
619 EXPECT_EQ(ret, RET_OK);
620 }
621
622 /**
623 * @tc.name: EpollManagerTest_Dispatch001
624 * @tc.desc: Test Dispatch, dispatch when there is event input
625 * @tc.type: FUNC
626 */
627 HWTEST_F(EpollManagerTest, EpollManagerTest_Dispatch001, TestSize.Level1)
628 {
629 CALL_TEST_DEBUG;
630 EpollManager epollMgr;
631 int32_t ret = epollMgr.Open();
632 EXPECT_EQ(ret, RET_OK);
633 if (ret != RET_OK) {
634 FI_HILOGE("Open epoll-manager failed");
635 return;
636 }
637 MonitorEvent monitor;
638 ret = epollMgr.Add(monitor);
639 if (ret != RET_OK) {
640 FI_HILOGE("Add listening event failed");
641 }
642 struct epoll_event ev {};
643 ev.events = EPOLLIN;
644 ev.data.ptr = &epollMgr;
645 epollMgr.Dispatch(ev);
646 epollMgr.Close();
647 EXPECT_EQ(ret, RET_OK);
648 }
649
650 /**
651 * @tc.name: EpollManagerTest_Dispatch002
652 * @tc.desc: Test Dispatch, dispatch when there are errors
653 * @tc.type: FUNC
654 */
655 HWTEST_F(EpollManagerTest, EpollManagerTest_Dispatch002, TestSize.Level1)
656 {
657 CALL_TEST_DEBUG;
658 EpollManager epollMgr;
659 int32_t ret = epollMgr.Open();
660 EXPECT_EQ(ret, RET_OK);
661 if (ret != RET_OK) {
662 FI_HILOGE("Open epoll-manager failed");
663 return;
664 }
665 MonitorEvent monitor;
666 ret = epollMgr.Add(monitor);
667 if (ret != RET_OK) {
668 FI_HILOGE("Add listening event failed");
669 }
670 struct epoll_event ev {};
671 ev.events = EPOLLERR;
672 ev.data.ptr = &epollMgr;
673 epollMgr.Dispatch(ev);
674 epollMgr.Close();
675 EXPECT_EQ(ret, RET_OK);
676 }
677
678 /**
679 * @tc.name: EpollManagerTest_Dispatch003
680 * @tc.desc: Test Dispatch, dispatch when there is event input
681 * @tc.type: FUNC
682 */
683 HWTEST_F(EpollManagerTest, EpollManagerTest_Dispatch003, TestSize.Level1)
684 {
685 CALL_TEST_DEBUG;
686 EpollManager epollMgr;
687 int32_t ret = epollMgr.Open();
688 EXPECT_EQ(ret, RET_OK);
689 if (ret != RET_OK) {
690 FI_HILOGE("Open epoll-manager failed");
691 return;
692 }
693 MonitorEvent monitor;
694 ret = monitor.SetTimer();
695 EXPECT_EQ(ret, RET_OK);
696 ret = epollMgr.Add(monitor);
697 if (ret != RET_OK) {
698 FI_HILOGE("Add listening event failed");
699 }
700 struct epoll_event ev {};
701 ev.events = EPOLLIN;
702 ev.data.ptr = &monitor;
703 ev.data.fd = monitor.GetFd();
704 FI_HILOGI("The timerfd is %{public}d", monitor.GetFd());
705 for (int32_t i = 0; i < DISPATCH_TIMES; ++i) {
706 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
707 FI_HILOGI("Dispatch times: %{public}d", i);
708 epollMgr.Dispatch(ev);
709 }
710 epollMgr.Close();
711 EXPECT_EQ(ret, RET_OK);
712 }
713 } // namespace DeviceStatus
714 } // namespace Msdp
715 } // namespace OHOS