1 /*
2 * Copyright (c) 2021 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 <atomic>
17 #include <gtest/gtest.h>
18 #include <thread>
19
20 #include "db_errno.h"
21 #include "distributeddb_tools_unit_test.h"
22 #include "evloop/src/event_impl.h"
23 #include "evloop/src/event_loop_epoll.h"
24 #include "evloop/src/ievent.h"
25 #include "evloop/src/ievent_loop.h"
26 #include "log_print.h"
27 #include "platform_specific.h"
28
29 using namespace testing::ext;
30 using namespace DistributedDB;
31
32 namespace {
33 IEventLoop *g_loop = nullptr;
34 constexpr int MAX_RETRY_TIMES = 1000;
35 constexpr int RETRY_TIMES_5 = 5;
36 constexpr int EPOLL_INIT_REVENTS = 32;
37 constexpr int ET_READ = 0x01;
38 constexpr int ET_WRITE = 0x02;
39 constexpr int ET_TIMEOUT = 0x08;
40 constexpr EventTime TIME_INACCURACY = 100LL;
41 constexpr EventTime TIME_PIECE_1 = 1LL;
42 constexpr EventTime TIME_PIECE_10 = 10LL;
43 constexpr EventTime TIME_PIECE_50 = 50LL;
44 constexpr EventTime TIME_PIECE_100 = 100LL;
45 constexpr EventTime TIME_PIECE_1000 = 1000LL;
46 constexpr EventTime TIME_PIECE_10000 = 10000LL;
47
48 class TimerTester {
49 public:
50 static EventTime GetCurrentTime();
51 };
52
GetCurrentTime()53 EventTime TimerTester::GetCurrentTime()
54 {
55 uint64_t now;
56 int errCode = OS::GetCurrentSysTimeInMicrosecond(now);
57 if (errCode != E_OK) {
58 LOGE("Get current time failed.");
59 return 0;
60 }
61 return now / 1000; // 1 ms equals to 1000 us
62 }
63
64 class DistributedDBEventLoopTimerTest : public testing::Test {
65 public:
66 static void SetUpTestCase(void);
67 static void TearDownTestCase(void);
68 void SetUp();
69 void TearDown();
70 };
71
SetUpTestCase(void)72 void DistributedDBEventLoopTimerTest::SetUpTestCase(void) {}
73
TearDownTestCase(void)74 void DistributedDBEventLoopTimerTest::TearDownTestCase(void) {}
75
SetUp(void)76 void DistributedDBEventLoopTimerTest::SetUp(void)
77 {
78 DistributedDBUnitTest::DistributedDBToolsUnitTest::PrintTestCaseInfo();
79 /**
80 * @tc.setup: Create a loop object.
81 */
82 if (g_loop == nullptr) {
83 int errCode = E_OK;
84 g_loop = IEventLoop::CreateEventLoop(errCode);
85 if (g_loop == nullptr) {
86 LOGE("Prepare loop in SetUp() failed.");
87 }
88 }
89 }
90
TearDown(void)91 void DistributedDBEventLoopTimerTest::TearDown(void)
92 {
93 /**
94 * @tc.teardown: Destroy the loop object.
95 */
96 if (g_loop != nullptr) {
97 g_loop->KillAndDecObjRef(g_loop);
98 g_loop = nullptr;
99 }
100 }
101
102 /**
103 * @tc.name: EventLoopTimerTest001
104 * @tc.desc: Create and destroy the event loop object.
105 * @tc.type: FUNC
106 * @tc.require:
107 * @tc.author: fangyi
108 */
109 HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest001, TestSize.Level0)
110 {
111 /**
112 * @tc.steps: step1. create a loop.
113 * @tc.expected: step1. create successfully.
114 */
115 int errCode = E_OK;
116 IEventLoop *loop = IEventLoop::CreateEventLoop(errCode);
117 ASSERT_EQ(loop != nullptr, true);
118
119 /**
120 * @tc.steps: step2. destroy the loop.
121 * @tc.expected: step2. destroy successfully.
122 */
123 bool finalized = false;
__anon31ac83b20202() 124 loop->OnLastRef([&finalized]() { finalized = true; });
125 RefObject::DecObjRef(loop);
126 loop = nullptr;
127 EXPECT_EQ(finalized, true);
128 }
129
130 /**
131 * @tc.name: EventLoopTimerTest002
132 * @tc.desc: Start and stop the loop
133 * @tc.type: FUNC
134 * @tc.require:
135 * @tc.author: fangyi
136 */
137 HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest002, TestSize.Level1)
138 {
139 // ready data
140 ASSERT_EQ(g_loop != nullptr, true);
141
142 /**
143 * @tc.steps: step1. create a loop.
144 * @tc.expected: step1. create successfully.
145 */
146 std::atomic<bool> running(false);
147 EventTime delta = 0;
__anon31ac83b20302() 148 std::thread loopThread([&running, &delta]() {
149 running = true;
150 EventTime start = TimerTester::GetCurrentTime();
151 g_loop->Run();
152 EventTime end = TimerTester::GetCurrentTime();
153 delta = end - start;
154 });
155 while (!running) {
156 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PIECE_1));
157 }
158 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PIECE_100));
159 g_loop->KillObj();
160 loopThread.join();
161 EXPECT_EQ(delta > TIME_PIECE_50, true);
162 }
163
164 /**
165 * @tc.name: EventLoopTimerTest003
166 * @tc.desc: Create and destroy a timer object.
167 * @tc.type: FUNC
168 * @tc.require:
169 * @tc.author: fangyi
170 */
171 HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest003, TestSize.Level0)
172 {
173 /**
174 * @tc.steps: step1. create event(timer) object.
175 * @tc.expected: step1. create successfully.
176 */
177 int errCode = E_OK;
178 IEvent *timer = IEvent::CreateEvent(TIME_PIECE_1, errCode);
179 ASSERT_EQ(timer != nullptr, true);
180
181 /**
182 * @tc.steps: step2. destroy the event object.
183 * @tc.expected: step2. destroy successfully.
184 */
185 bool finalized = false;
__anon31ac83b20402(EventsMask revents) 186 errCode = timer->SetAction([](EventsMask revents) -> int {
187 return E_OK;
188 }, [&finalized]() {
189 finalized = true;
190 });
191 EXPECT_EQ(errCode, E_OK);
192 timer->KillAndDecObjRef(timer);
193 timer = nullptr;
194 EXPECT_EQ(finalized, true);
195 }
196
197 /**
198 * @tc.name: EventLoopTimerTest004
199 * @tc.desc: Start a timer
200 * @tc.type: FUNC
201 * @tc.require:
202 * @tc.author: fangyi
203 */
204 HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest004, TestSize.Level1)
205 {
206 // ready data
207 ASSERT_EQ(g_loop != nullptr, true);
208
209 /**
210 * @tc.steps: step1. start the loop.
211 * @tc.expected: step1. start successfully.
212 */
213 std::atomic<bool> running(false);
__anon31ac83b20602() 214 std::thread loopThread([&running]() {
215 running = true;
216 g_loop->Run();
217 });
218
219 int tryCounter = 0;
220 while (!running) {
221 tryCounter++;
222 if (tryCounter >= MAX_RETRY_TIMES) {
223 break;
224 }
225 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PIECE_1));
226 }
227 EXPECT_EQ(running, true);
228
229 /**
230 * @tc.steps: step2. create and start a timer.
231 * @tc.expected: step2. start successfully.
232 */
233 int errCode = E_OK;
234 IEvent *timer = IEvent::CreateEvent(TIME_PIECE_10, errCode);
235 ASSERT_EQ(timer != nullptr, true);
236 std::atomic<int> counter(0);
__anon31ac83b20702(EventsMask revents) 237 errCode = timer->SetAction([&counter](EventsMask revents) -> int { ++counter; return E_OK; }, nullptr);
238 EXPECT_EQ(errCode, E_OK);
239 errCode = g_loop->Add(timer);
240 EXPECT_EQ(errCode, E_OK);
241
242 /**
243 * @tc.steps: step3. wait and check.
244 * @tc.expected: step3. 'counter' increased by the timer.
245 */
246 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PIECE_100));
247 EXPECT_EQ(counter > 0, true);
248 g_loop->KillObj();
249 loopThread.join();
250 RefObject::DecObjRef(timer);
251 }
252
253 /**
254 * @tc.name: EventLoopTimerTest005
255 * @tc.desc: Stop a timer
256 * @tc.type: FUNC
257 * @tc.require:
258 * @tc.author: fangyi
259 */
260 HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest005, TestSize.Level1)
261 {
262 // ready data
263 ASSERT_EQ(g_loop != nullptr, true);
264
265 /**
266 * @tc.steps: step1. start the loop.
267 * @tc.expected: step1. start successfully.
268 */
269 std::atomic<bool> running(false);
__anon31ac83b20802() 270 std::thread loopThread([&running]() {
271 running = true;
272 g_loop->Run();
273 });
274
275 int tryCounter = 1;
276 while (!running && tryCounter <= MAX_RETRY_TIMES) {
277 std::this_thread::sleep_for(std::chrono::milliseconds(1));
278 tryCounter++;
279 }
280 EXPECT_EQ(running, true);
281
282 /**
283 * @tc.steps: step2. create and start a timer.
284 * @tc.expected: step2. start successfully.
285 */
286 int errCode = E_OK;
287 IEvent *timer = IEvent::CreateEvent(10, errCode);
288 ASSERT_EQ(timer != nullptr, true);
289 std::atomic<int> counter(0);
290 std::atomic<bool> finalize(false);
291 errCode = timer->SetAction(
__anon31ac83b20902(EventsMask revents) 292 [&counter](EventsMask revents) -> int {
293 ++counter;
294 return E_OK;
295 }, [&finalize]() { finalize = true; });
296 EXPECT_EQ(errCode, E_OK);
297 errCode = g_loop->Add(timer);
298 EXPECT_EQ(errCode, E_OK);
299
300 /**
301 * @tc.steps: step3. wait and check.
302 * @tc.expected: step3. 'counter' increased by the timer and the timer object finalized.
303 */
304 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PIECE_100));
305 timer->KillAndDecObjRef(timer);
306 timer = nullptr;
307 g_loop->KillObj();
308 loopThread.join();
309 EXPECT_EQ(counter > 0, true);
310 EXPECT_EQ(finalize, true);
311 }
312
313 /**
314 * @tc.name: EventLoopTimerTest006
315 * @tc.desc: Stop a timer
316 * @tc.type: FUNC
317 * @tc.require:
318 * @tc.author: fangyi
319 */
320 HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest006, TestSize.Level1)
321 {
322 // ready data
323 ASSERT_EQ(g_loop != nullptr, true);
324
325 /**
326 * @tc.steps: step1. start the loop.
327 * @tc.expected: step1. start successfully.
328 */
329 std::atomic<bool> running(false);
__anon31ac83b20b02() 330 std::thread loopThread([&running]() {
331 running = true;
332 g_loop->Run();
333 });
334
335 int tryCounter = 1;
336 while (!running && tryCounter <= MAX_RETRY_TIMES) {
337 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PIECE_10));
338 tryCounter++;
339 }
340 EXPECT_EQ(running, true);
341
342 /**
343 * @tc.steps: step2. create and start a timer.
344 * @tc.expected: step2. start successfully.
345 */
346 int errCode = E_OK;
347 IEvent *timer = IEvent::CreateEvent(TIME_PIECE_10, errCode);
348 ASSERT_EQ(timer != nullptr, true);
349 std::atomic<int> counter(0);
350 std::atomic<bool> finalize(false);
__anon31ac83b20c02(EventsMask revents) 351 errCode = timer->SetAction([&counter](EventsMask revents) -> int { ++counter; return -E_STALE; },
__anon31ac83b20d02() 352 [&finalize]() { finalize = true; });
353 EXPECT_EQ(errCode, E_OK);
354 errCode = g_loop->Add(timer);
355 EXPECT_EQ(errCode, E_OK);
356
357 /**
358 * @tc.steps: step3. wait and check.
359 * @tc.expected: step3. 'counter' increased by the timer and the timer object finalized.
360 */
361 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PIECE_100));
362 g_loop->KillObj();
363 loopThread.join();
364 RefObject::DecObjRef(timer);
365 timer = nullptr;
366 EXPECT_EQ(finalize, true);
367 EXPECT_EQ(counter > 0, true);
368 }
369
370 /**
371 * @tc.name: EventLoopTimerTest007
372 * @tc.desc: Modify a timer
373 * @tc.type: FUNC
374 * @tc.require:
375 * @tc.author: fangyi
376 */
377 HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTimerTest007, TestSize.Level2)
378 {
379 // ready data
380 ASSERT_EQ(g_loop != nullptr, true);
381
382 /**
383 * @tc.steps: step1. start the loop.
384 * @tc.expected: step1. start successfully.
385 */
386 std::atomic<bool> running(false);
__anon31ac83b20e02() 387 std::thread loopThread([&running]() {
388 running = true;
389 g_loop->Run();
390 });
391
392 int tryCounter = 1;
393 while (!running && tryCounter <= MAX_RETRY_TIMES) {
394 std::this_thread::sleep_for(std::chrono::milliseconds(1));
395 tryCounter++;
396 }
397 EXPECT_EQ(running, true);
398
399 /**
400 * @tc.steps: step2. create and start a timer.
401 * @tc.expected: step2. start successfully.
402 */
403 int errCode = E_OK;
404 IEvent *timer = IEvent::CreateEvent(TIME_PIECE_1000, errCode);
405 ASSERT_EQ(timer != nullptr, true);
406 int counter = 1; // Interval: 1 * TIME_PIECE_100
407 EventTime lastTime = TimerTester::GetCurrentTime();
408 std::atomic<EventTime> total = 0;
409 errCode = timer->SetAction(
__anon31ac83b20f02(EventsMask revents) 410 [timer, &counter, &lastTime, &total](EventsMask revents) -> int {
411 EventTime now = TimerTester::GetCurrentTime();
412 EventTime delta = now - lastTime;
413 delta -= counter * TIME_PIECE_1000;
414 total += delta;
415 if (++counter > RETRY_TIMES_5) {
416 return -E_STALE;
417 }
418 lastTime = TimerTester::GetCurrentTime();
419 int ret = timer->SetTimeout(counter * TIME_PIECE_1000);
420 if (ret != -E_OBJ_IS_KILLED) {
421 EXPECT_EQ(ret, E_OK);
422 }
423 return E_OK;
424 }, nullptr);
425 EXPECT_LE(std::abs(total), TIME_INACCURACY * RETRY_TIMES_5);
426 EXPECT_EQ(errCode, E_OK);
427 errCode = g_loop->Add(timer);
428 EXPECT_EQ(errCode, E_OK);
429
430 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PIECE_10000));
431 g_loop->KillObj();
432 loopThread.join();
433 RefObject::DecObjRef(timer);
434 }
435
436 /**
437 * @tc.name: EventLoopTest001
438 * @tc.desc: Test Initialize twice
439 * @tc.type: FUNC
440 * @tc.require:
441 * @tc.author: chenchaohao
442 */
443 HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTest001, TestSize.Level0)
444 {
445 EventLoopEpoll *loop = new (std::nothrow) EventLoopEpoll;
446
447 EXPECT_EQ(loop->Initialize(), E_OK);
448 EXPECT_EQ(loop->Initialize(), -E_INVALID_ARGS);
449 DistributedDB::RefObject::KillAndDecObjRef(loop);
450 }
451
452 /**
453 * @tc.name: EventLoopTest002
454 * @tc.desc: Test interface if args is invalid
455 * @tc.type: FUNC
456 * @tc.require:
457 * @tc.author: chenchaohao
458 */
459 HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTest002, TestSize.Level0)
460 {
461 // ready data
462 ASSERT_NE(g_loop, nullptr);
463
464 /**
465 * @tc.steps: step1. test the loop interface.
466 * @tc.expected: step1. return INVALID_AGRS.
467 */
468 EXPECT_EQ(g_loop->Add(nullptr), -E_INVALID_ARGS);
469 EXPECT_EQ(g_loop->Remove(nullptr), -E_INVALID_ARGS);
470 EXPECT_EQ(g_loop->Stop(), E_OK);
471
472 EventLoopImpl *loopImpl= static_cast<EventLoopImpl *>(g_loop);
473 EventsMask events = 1u;
474 EXPECT_EQ(loopImpl->Modify(nullptr, true, events), -E_INVALID_ARGS);
475 EXPECT_EQ(loopImpl->Modify(nullptr, 0), -E_INVALID_ARGS);
476 }
477
478 /**
479 * @tc.name: EventTest001
480 * @tc.desc: Test CreateEvent if args is invalid
481 * @tc.type: FUNC
482 * @tc.require:
483 * @tc.author: chenchaohao
484 */
485 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest001, TestSize.Level0)
486 {
487 /**
488 * @tc.steps:step1. set EventTime = -1 and CreateEvent
489 * @tc.expected: step1. return INVALID_ARGS
490 */
491 EventTime eventTime = -1; // -1 is invalid arg
492 int errCode = E_OK;
493 IEvent *event = IEvent::CreateEvent(eventTime, errCode);
494 ASSERT_EQ(event, nullptr);
495 EXPECT_EQ(errCode, -E_INVALID_ARGS);
496
497 /**
498 * @tc.steps:step2. set EventsMask = 0 and CreateEvent
499 * @tc.expected: step2. return INVALID_ARGS
500 */
501 EventFd eventFd = EventFd();
502 EventsMask events = 0u;
503 event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
504 ASSERT_EQ(event, nullptr);
505 EXPECT_EQ(errCode, -E_INVALID_ARGS);
506
507 /**
508 * @tc.steps:step3. set EventsMask = 4 and EventFd is invalid then CreateEvent
509 * @tc.expected: step3. return INVALID_ARGS
510 */
511 EventsMask eventsMask = 1u; // 1 is ET_READ
512 event = IEvent::CreateEvent(eventFd, eventsMask, eventTime, errCode);
513 ASSERT_EQ(event, nullptr);
514 EXPECT_EQ(errCode, -E_INVALID_ARGS);
515
516 /**
517 * @tc.steps:step4. set EventsMask = 8 and CreateEvent
518 * @tc.expected: step4. return INVALID_ARGS
519 */
520 events |= ET_TIMEOUT;
521 event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
522 ASSERT_EQ(event, nullptr);
523 EXPECT_EQ(errCode, -E_INVALID_ARGS);
524
525 /**
526 * @tc.steps:step5. set EventTime = 1 and CreateEvent
527 * @tc.expected: step5. return OK
528 */
529 eventTime = TIME_PIECE_1;
530 eventFd = EventFd(epoll_create(EPOLL_INIT_REVENTS));
531 event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
532 ASSERT_NE(event, nullptr);
533 EXPECT_EQ(errCode, E_OK);
534 DistributedDB::RefObject::KillAndDecObjRef(event);
535 }
536
537 /**
538 * @tc.name: EventTest002
539 * @tc.desc: Test SetAction if action is nullptr
540 * @tc.type: FUNC
541 * @tc.require:
542 * @tc.author: chenchaohao
543 */
544 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest002, TestSize.Level0)
545 {
546 /**
547 * @tc.steps:step1. CreateEvent
548 * @tc.expected: step1. return OK
549 */
550 EventTime eventTime = TIME_PIECE_1;
551 int errCode = E_OK;
552 IEvent *event = IEvent::CreateEvent(eventTime, errCode);
553 ASSERT_NE(event, nullptr);
554 EXPECT_EQ(errCode, E_OK);
555
556 /**
557 * @tc.steps:step2. SetAction with nullptr
558 * @tc.expected: step2. return INVALID_ARGS
559 */
560 EXPECT_EQ(event->SetAction(nullptr), -E_INVALID_ARGS);
561 DistributedDB::RefObject::KillAndDecObjRef(event);
562 }
563
564 /**
565 * @tc.name: EventTest003
566 * @tc.desc: Test AddEvents and RemoveEvents with fd is invalid
567 * @tc.type: FUNC
568 * @tc.require:
569 * @tc.author: chenchaohao
570 */
571 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest003, TestSize.Level0)
572 {
573 /**
574 * @tc.steps:step1. CreateEvent
575 * @tc.expected: step1. return OK
576 */
577 EventTime eventTime = TIME_PIECE_1;
578 int errCode = E_OK;
579 IEvent *event = IEvent::CreateEvent(eventTime, errCode);
580 ASSERT_NE(event, nullptr);
581 EXPECT_EQ(errCode, E_OK);
582
583 /**
584 * @tc.steps:step2. AddEvents and RemoveEvents with events is 0
585 * @tc.expected: step2. return INVALID_ARGS
586 */
587 EventsMask events = 0u;
588 EXPECT_EQ(event->AddEvents(events), -E_INVALID_ARGS);
589 EXPECT_EQ(event->RemoveEvents(events), -E_INVALID_ARGS);
590
591 /**
592 * @tc.steps:step3. AddEvents and RemoveEvents with fd is invalid
593 * @tc.expected: step3. return OK
594 */
595 events |= ET_READ;
596 EXPECT_EQ(event->AddEvents(events), -E_INVALID_ARGS);
597 EXPECT_EQ(event->RemoveEvents(events), -E_INVALID_ARGS);
598 DistributedDB::RefObject::KillAndDecObjRef(event);
599 }
600
601 /**
602 * @tc.name: EventTest004
603 * @tc.desc: Test AddEvents and RemoveEvents with fd is valid
604 * @tc.type: FUNC
605 * @tc.require:
606 * @tc.author: chenchaohao
607 */
608 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest004, TestSize.Level0)
609 {
610 /**
611 * @tc.steps:step1. CreateEvent
612 * @tc.expected: step1. return OK
613 */
614 EventTime eventTime = TIME_PIECE_1;
615 EventFd eventFd = EventFd(epoll_create(EPOLL_INIT_REVENTS));
616 EventsMask events = 1u; // 1 means ET_READ
617 int errCode = E_OK;
618 IEvent *event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
619 ASSERT_NE(event, nullptr);
620 EXPECT_EQ(errCode, E_OK);
621
622 /**
623 * @tc.steps:step2. AddEvents and RemoveEvents with fd is valid
624 * @tc.expected: step2. return OK
625 */
626 events |= ET_WRITE;
627 EXPECT_EQ(event->AddEvents(events), E_OK);
628 EXPECT_EQ(event->RemoveEvents(events), E_OK);
629
630 /**
631 * @tc.steps:step3. AddEvents and RemoveEvents after set action
632 * @tc.expected: step3. return OK
633 */
634 ASSERT_EQ(g_loop->Add(event), -E_INVALID_ARGS);
__anon31ac83b21002(EventsMask revents) 635 ASSERT_EQ(event->SetAction([](EventsMask revents) -> int {
636 return E_OK;
637 }), E_OK);
638 ASSERT_EQ(g_loop->Add(event), E_OK);
639 EXPECT_EQ(event->AddEvents(events), E_OK);
640 EXPECT_EQ(event->RemoveEvents(events), E_OK);
641 DistributedDB::RefObject::KillAndDecObjRef(event);
642 }
643
644 /**
645 * @tc.name: EventTest005
646 * @tc.desc: Test constructor method with timeout < 0
647 * @tc.type: FUNC
648 * @tc.require:
649 * @tc.author: chenchaohao
650 */
651 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest005, TestSize.Level0)
652 {
653 /**
654 * @tc.steps:step1. instantiation event with eventTime
655 * @tc.expected: step1. return OK
656 */
657 EventTime eventTime = -1; // -1 is invalid arg
658 IEvent *event = new (std::nothrow) EventImpl(eventTime);
659 ASSERT_NE(event, nullptr);
660 DistributedDB::RefObject::KillAndDecObjRef(event);
661
662 /**
663 * @tc.steps:step2. instantiation event with eventFd, events, eventTime
664 * @tc.expected: step2. return OK
665 */
666 EventFd eventFd = EventFd();
667 EventsMask events = 1u; // 1 means ET_READ
668 EventImpl *eventImpl = new (std::nothrow) EventImpl(eventFd, events, eventTime);
669 ASSERT_NE(eventImpl, nullptr);
670 DistributedDB::RefObject::KillAndDecObjRef(eventImpl);
671 }
672
673 /**
674 * @tc.name: EventTest006
675 * @tc.desc: Test SetTimeout
676 * @tc.type: FUNC
677 * @tc.require:
678 * @tc.author: chenchaohao
679 */
680 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest006, TestSize.Level0)
681 {
682 /**
683 * @tc.steps:step1. CreateEvent
684 * @tc.expected: step1. return OK
685 */
686 EventTime eventTime = TIME_PIECE_1;
687 int errCode = E_OK;
688 IEvent *event = IEvent::CreateEvent(eventTime, errCode);
689 ASSERT_NE(event, nullptr);
690 EXPECT_EQ(errCode, E_OK);
691
692 /**
693 * @tc.steps:step2. SetTimeout
694 * @tc.expected: step2. return INVALID_ARGS
695 */
696 event->IgnoreFinalizer();
697 EXPECT_EQ(event->SetTimeout(eventTime), E_OK);
698 eventTime = -1; // -1 is invalid args
699 EXPECT_EQ(event->SetTimeout(eventTime), -E_INVALID_ARGS);
700 DistributedDB::RefObject::KillAndDecObjRef(event);
701 }
702
703 /**
704 * @tc.name: EventTest007
705 * @tc.desc: Test SetEvents and GetEvents
706 * @tc.type: FUNC
707 * @tc.require:
708 * @tc.author: chenchaohao
709 */
710 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest007, TestSize.Level0)
711 {
712 /**
713 * @tc.steps:step1. CreateEvent
714 * @tc.expected: step1. return OK
715 */
716 EventTime eventTime = TIME_PIECE_1;
717 EventFd eventFd = EventFd(epoll_create(EPOLL_INIT_REVENTS));
718 EventsMask events = 1u; // 1 means ET_READ
719 int errCode = E_OK;
720 IEvent *event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
721 ASSERT_NE(event, nullptr);
722 EXPECT_EQ(errCode, E_OK);
723
724 /**
725 * @tc.steps:step2. Test GetEventFd and GetEvents
726 * @tc.expected: step2. return OK
727 */
728 EventImpl *eventImpl = static_cast<EventImpl *>(event);
729 eventImpl->SetRevents(events);
730 EXPECT_EQ(eventImpl->GetEventFd(), eventFd);
731 EXPECT_EQ(eventImpl->GetEvents(), events);
732 events = 2u; // 2 means ET_WRITE
733 eventImpl->SetEvents(true, events);
734 EXPECT_EQ(eventImpl->GetEvents(), 3u); // 3 means ET_WRITE | ET_READ
735 eventImpl->SetEvents(false, events);
736 EXPECT_EQ(eventImpl->GetEvents(), 1u); // 1 means ET_READ
737 EXPECT_FALSE(eventImpl->GetTimeoutPoint(eventTime));
738 DistributedDB::RefObject::KillAndDecObjRef(eventImpl);
739 }
740
741 /**
742 * @tc.name: EventTest008
743 * @tc.desc: Test SetTimeoutPeriod and GetTimeoutPoint
744 * @tc.type: FUNC
745 * @tc.require:
746 * @tc.author: chenchaohao
747 */
748 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest008, TestSize.Level0)
749 {
750 /**
751 * @tc.steps:step1. CreateEvent
752 * @tc.expected: step1. return OK
753 */
754 EventTime eventTime = TIME_PIECE_1;
755 int errCode = E_OK;
756 IEvent *event = IEvent::CreateEvent(eventTime, errCode);
757 ASSERT_NE(event, nullptr);
758 EXPECT_EQ(errCode, E_OK);
759
760 /**
761 * @tc.steps:step2. SetTimeoutPeriod and GetTimeoutPoint
762 * @tc.expected: step2. return OK
763 */
764 EventImpl *eventImpl = static_cast<EventImpl *>(event);
765 eventTime = -1; // -1 is invalid args
766 eventImpl->SetTimeoutPeriod(eventTime);
767 EXPECT_TRUE(eventImpl->GetTimeoutPoint(eventTime));
768
769 /**
770 * @tc.steps:step3. Dispatch
771 * @tc.expected: step3. return INVALID_ARGS
772 */
773 EXPECT_EQ(eventImpl->Dispatch(), -E_INVALID_ARGS);
774 DistributedDB::RefObject::KillAndDecObjRef(eventImpl);
775 }
776 }
777