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: AR000CKRTB AR000CQE0C
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;
__anon31edf1080202() 124     loop->OnLastRef([&finalized]() { finalized = true; });
125     loop->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: AR000CKRTB AR000CQE0C
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;
__anon31edf1080302() 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: AR000CKRTB AR000CQE0C
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;
__anon31edf1080402(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: AR000CKRTB AR000CQE0C
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);
__anon31edf1080602() 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);
__anon31edf1080702(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     timer->DecObjRef(timer);
251 }
252 
253 /**
254  * @tc.name: EventLoopTimerTest005
255  * @tc.desc: Stop a timer
256  * @tc.type: FUNC
257  * @tc.require: AR000CKRTB AR000CQE0C
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);
__anon31edf1080802() 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(
__anon31edf1080902(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: AR000CKRTB AR000CQE0C
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);
__anon31edf1080b02() 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);
__anon31edf1080c02(EventsMask revents) 351     errCode = timer->SetAction([&counter](EventsMask revents) -> int { ++counter; return -E_STALE; },
__anon31edf1080d02() 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     timer->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: AR000CKRTB AR000CQE0C
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);
__anon31edf1080e02() 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     errCode = timer->SetAction(
__anon31edf1080f02(EventsMask revents) 409         [timer, &counter, &lastTime](EventsMask revents) -> int {
410             EventTime now = TimerTester::GetCurrentTime();
411             EventTime delta = now - lastTime;
412             delta -= counter * TIME_PIECE_1000;
413             EXPECT_EQ(delta >= -TIME_INACCURACY && delta <= TIME_INACCURACY, true);
414             if (++counter > RETRY_TIMES_5) {
415                 return -E_STALE;
416             }
417             lastTime = TimerTester::GetCurrentTime();
418             int ret = timer->SetTimeout(counter * TIME_PIECE_1000);
419             if (ret != -E_OBJ_IS_KILLED) {
420                 EXPECT_EQ(ret, E_OK);
421             }
422             return E_OK;
423         }, nullptr);
424     EXPECT_EQ(errCode, E_OK);
425     errCode = g_loop->Add(timer);
426     EXPECT_EQ(errCode, E_OK);
427 
428     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_PIECE_10000));
429     g_loop->KillObj();
430     loopThread.join();
431     timer->DecObjRef(timer);
432 }
433 
434 /**
435  * @tc.name: EventLoopTest001
436  * @tc.desc: Test Initialize twice
437  * @tc.type: FUNC
438  * @tc.require:
439  * @tc.author: chenchaohao
440  */
441 HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTest001, TestSize.Level0)
442 {
443     EventLoopEpoll *loop = new (std::nothrow) EventLoopEpoll;
444 
445     EXPECT_EQ(loop->Initialize(), E_OK);
446     EXPECT_EQ(loop->Initialize(), -E_INVALID_ARGS);
447 }
448 
449 /**
450  * @tc.name: EventLoopTest002
451  * @tc.desc: Test interface if args is invalid
452  * @tc.type: FUNC
453  * @tc.require:
454  * @tc.author: chenchaohao
455  */
456 HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTest002, TestSize.Level0)
457 {
458     // ready data
459     ASSERT_NE(g_loop, nullptr);
460 
461     /**
462      * @tc.steps: step1. test the loop interface.
463      * @tc.expected: step1. return INVALID_AGRS.
464      */
465     EXPECT_EQ(g_loop->Add(nullptr), -E_INVALID_ARGS);
466     EXPECT_EQ(g_loop->Remove(nullptr), -E_INVALID_ARGS);
467     EXPECT_EQ(g_loop->Stop(), E_OK);
468 
469     EventLoopImpl *loopImpl= static_cast<EventLoopImpl *>(g_loop);
470     EventsMask events = 1u;
471     EXPECT_EQ(loopImpl->Modify(nullptr, true, events), -E_INVALID_ARGS);
472     EXPECT_EQ(loopImpl->Modify(nullptr, 0), -E_INVALID_ARGS);
473 }
474 
475 /**
476  * @tc.name: EventTest001
477  * @tc.desc: Test CreateEvent if args is invalid
478  * @tc.type: FUNC
479  * @tc.require:
480  * @tc.author: chenchaohao
481  */
482 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest001, TestSize.Level0)
483 {
484     /**
485      * @tc.steps:step1. set EventTime = -1 and CreateEvent
486      * @tc.expected: step1. return INVALID_ARGS
487      */
488     EventTime eventTime = -1; // -1 is invalid arg
489     int errCode = E_OK;
490     IEvent *event = IEvent::CreateEvent(eventTime, errCode);
491     ASSERT_EQ(event, nullptr);
492     EXPECT_EQ(errCode, -E_INVALID_ARGS);
493 
494     /**
495      * @tc.steps:step2. set EventsMask = 0 and CreateEvent
496      * @tc.expected: step2. return INVALID_ARGS
497      */
498     EventFd eventFd = EventFd();
499     EventsMask events = 0u;
500     event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
501     ASSERT_EQ(event, nullptr);
502     EXPECT_EQ(errCode, -E_INVALID_ARGS);
503 
504     /**
505      * @tc.steps:step3. set EventsMask = 4 and EventFd is invalid then CreateEvent
506      * @tc.expected: step3. return INVALID_ARGS
507      */
508     EventsMask eventsMask = 1u; // 1 is ET_READ
509     event = IEvent::CreateEvent(eventFd, eventsMask, eventTime, errCode);
510     ASSERT_EQ(event, nullptr);
511     EXPECT_EQ(errCode, -E_INVALID_ARGS);
512 
513     /**
514      * @tc.steps:step4. set EventsMask = 8 and CreateEvent
515      * @tc.expected: step4. return INVALID_ARGS
516      */
517     events |= ET_TIMEOUT;
518     event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
519     ASSERT_EQ(event, nullptr);
520     EXPECT_EQ(errCode, -E_INVALID_ARGS);
521 
522     /**
523      * @tc.steps:step5. set EventTime = 1 and CreateEvent
524      * @tc.expected: step5. return OK
525      */
526     eventTime = TIME_PIECE_1;
527     eventFd = EventFd(epoll_create(EPOLL_INIT_REVENTS));
528     event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
529     ASSERT_NE(event, nullptr);
530     EXPECT_EQ(errCode, E_OK);
531 }
532 
533 /**
534  * @tc.name: EventTest002
535  * @tc.desc: Test SetAction if action is nullptr
536  * @tc.type: FUNC
537  * @tc.require:
538  * @tc.author: chenchaohao
539  */
540 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest002, TestSize.Level0)
541 {
542     /**
543      * @tc.steps:step1. CreateEvent
544      * @tc.expected: step1. return OK
545      */
546     EventTime eventTime = TIME_PIECE_1;
547     int errCode = E_OK;
548     IEvent *event = IEvent::CreateEvent(eventTime, errCode);
549     ASSERT_NE(event, nullptr);
550     EXPECT_EQ(errCode, E_OK);
551 
552     /**
553      * @tc.steps:step2. SetAction with nullptr
554      * @tc.expected: step2. return INVALID_ARGS
555      */
556     EXPECT_EQ(event->SetAction(nullptr), -E_INVALID_ARGS);
557 }
558 
559 /**
560  * @tc.name: EventTest003
561  * @tc.desc: Test AddEvents and RemoveEvents with fd is invalid
562  * @tc.type: FUNC
563  * @tc.require:
564  * @tc.author: chenchaohao
565  */
566 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest003, TestSize.Level0)
567 {
568     /**
569      * @tc.steps:step1. CreateEvent
570      * @tc.expected: step1. return OK
571      */
572     EventTime eventTime = TIME_PIECE_1;
573     int errCode = E_OK;
574     IEvent *event = IEvent::CreateEvent(eventTime, errCode);
575     ASSERT_NE(event, nullptr);
576     EXPECT_EQ(errCode, E_OK);
577 
578     /**
579      * @tc.steps:step2. AddEvents and RemoveEvents with events is 0
580      * @tc.expected: step2. return INVALID_ARGS
581      */
582     EventsMask events = 0u;
583     EXPECT_EQ(event->AddEvents(events), -E_INVALID_ARGS);
584     EXPECT_EQ(event->RemoveEvents(events), -E_INVALID_ARGS);
585 
586     /**
587      * @tc.steps:step3. AddEvents and RemoveEvents with fd is invalid
588      * @tc.expected: step3. return OK
589      */
590     events |= ET_READ;
591     EXPECT_EQ(event->AddEvents(events), -E_INVALID_ARGS);
592     EXPECT_EQ(event->RemoveEvents(events), -E_INVALID_ARGS);
593 }
594 
595 /**
596  * @tc.name: EventTest004
597  * @tc.desc: Test AddEvents and RemoveEvents with fd is valid
598  * @tc.type: FUNC
599  * @tc.require:
600  * @tc.author: chenchaohao
601  */
602 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest004, TestSize.Level0)
603 {
604     /**
605      * @tc.steps:step1. CreateEvent
606      * @tc.expected: step1. return OK
607      */
608     EventTime eventTime = TIME_PIECE_1;
609     EventFd eventFd = EventFd(epoll_create(EPOLL_INIT_REVENTS));
610     EventsMask events = 1u; // 1 means ET_READ
611     int errCode = E_OK;
612     IEvent *event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
613     ASSERT_NE(event, nullptr);
614     EXPECT_EQ(errCode, E_OK);
615 
616     /**
617      * @tc.steps:step2. AddEvents and RemoveEvents with fd is valid
618      * @tc.expected: step2. return OK
619      */
620     events |= ET_WRITE;
621     EXPECT_EQ(event->AddEvents(events), E_OK);
622     EXPECT_EQ(event->RemoveEvents(events), E_OK);
623 
624     /**
625      * @tc.steps:step3. AddEvents and RemoveEvents after set action
626      * @tc.expected: step3. return OK
627      */
628     ASSERT_EQ(g_loop->Add(event), -E_INVALID_ARGS);
__anon31edf1081002(EventsMask revents) 629     ASSERT_EQ(event->SetAction([](EventsMask revents) -> int {
630         return E_OK;
631         }), E_OK);
632     ASSERT_EQ(g_loop->Add(event), E_OK);
633     EXPECT_EQ(event->AddEvents(events), E_OK);
634     EXPECT_EQ(event->RemoveEvents(events), E_OK);
635 }
636 
637 /**
638  * @tc.name: EventTest005
639  * @tc.desc: Test constructor method with timeout < 0
640  * @tc.type: FUNC
641  * @tc.require:
642  * @tc.author: chenchaohao
643  */
644 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest005, TestSize.Level0)
645 {
646     /**
647      * @tc.steps:step1. instantiation event with eventTime
648      * @tc.expected: step1. return OK
649      */
650     EventTime eventTime = -1; // -1 is invalid arg
651     IEvent *event = new (std::nothrow) EventImpl(eventTime);
652     ASSERT_NE(event, nullptr);
653 
654     /**
655      * @tc.steps:step2. instantiation event with eventFd, events, eventTime
656      * @tc.expected: step2. return OK
657      */
658     EventFd eventFd = EventFd();
659     EventsMask events = 1u; // 1 means ET_READ
660     EventImpl *eventImpl = new (std::nothrow) EventImpl(eventFd, events, eventTime);
661     ASSERT_NE(eventImpl, nullptr);
662 }
663 
664 /**
665  * @tc.name: EventTest006
666  * @tc.desc: Test SetTimeout
667  * @tc.type: FUNC
668  * @tc.require:
669  * @tc.author: chenchaohao
670  */
671 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest006, TestSize.Level0)
672 {
673     /**
674      * @tc.steps:step1. CreateEvent
675      * @tc.expected: step1. return OK
676      */
677     EventTime eventTime = TIME_PIECE_1;
678     int errCode = E_OK;
679     IEvent *event = IEvent::CreateEvent(eventTime, errCode);
680     ASSERT_NE(event, nullptr);
681     EXPECT_EQ(errCode, E_OK);
682 
683     /**
684      * @tc.steps:step2. SetTimeout
685      * @tc.expected: step2. return INVALID_ARGS
686      */
687     event->IgnoreFinalizer();
688     EXPECT_EQ(event->SetTimeout(eventTime), E_OK);
689     eventTime = -1; // -1 is invalid args
690     EXPECT_EQ(event->SetTimeout(eventTime), -E_INVALID_ARGS);
691 }
692 
693 /**
694  * @tc.name: EventTest007
695  * @tc.desc: Test SetEvents and GetEvents
696  * @tc.type: FUNC
697  * @tc.require:
698  * @tc.author: chenchaohao
699  */
700 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest007, TestSize.Level0)
701 {
702     /**
703      * @tc.steps:step1. CreateEvent
704      * @tc.expected: step1. return OK
705      */
706     EventTime eventTime = TIME_PIECE_1;
707     EventFd eventFd = EventFd(epoll_create(EPOLL_INIT_REVENTS));
708     EventsMask events = 1u; // 1 means ET_READ
709     int errCode = E_OK;
710     IEvent *event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
711     ASSERT_NE(event, nullptr);
712     EXPECT_EQ(errCode, E_OK);
713 
714     /**
715      * @tc.steps:step2. Test GetEventFd and GetEvents
716      * @tc.expected: step2. return OK
717      */
718     EventImpl *eventImpl = static_cast<EventImpl *>(event);
719     eventImpl->SetRevents(events);
720     EXPECT_EQ(eventImpl->GetEventFd(), eventFd);
721     EXPECT_EQ(eventImpl->GetEvents(), events);
722     events = 2u; // 2 means ET_WRITE
723     eventImpl->SetEvents(true, events);
724     EXPECT_EQ(eventImpl->GetEvents(), 3u); // 3 means ET_WRITE | ET_READ
725     eventImpl->SetEvents(false, events);
726     EXPECT_EQ(eventImpl->GetEvents(), 1u); // 1 means ET_READ
727     EXPECT_FALSE(eventImpl->GetTimeoutPoint(eventTime));
728 }
729 
730 /**
731  * @tc.name: EventTest008
732  * @tc.desc: Test SetTimeoutPeriod and GetTimeoutPoint
733  * @tc.type: FUNC
734  * @tc.require:
735  * @tc.author: chenchaohao
736  */
737 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest008, TestSize.Level0)
738 {
739     /**
740      * @tc.steps:step1. CreateEvent
741      * @tc.expected: step1. return OK
742      */
743     EventTime eventTime = TIME_PIECE_1;
744     int errCode = E_OK;
745     IEvent *event = IEvent::CreateEvent(eventTime, errCode);
746     ASSERT_NE(event, nullptr);
747     EXPECT_EQ(errCode, E_OK);
748 
749     /**
750      * @tc.steps:step2. SetTimeoutPeriod and GetTimeoutPoint
751      * @tc.expected: step2. return OK
752      */
753     EventImpl *eventImpl = static_cast<EventImpl *>(event);
754     eventTime = -1; // -1 is invalid args
755     eventImpl->SetTimeoutPeriod(eventTime);
756     EXPECT_TRUE(eventImpl->GetTimeoutPoint(eventTime));
757 
758     /**
759      * @tc.steps:step3. Dispatch
760      * @tc.expected: step3. return INVALID_ARGS
761      */
762     EXPECT_EQ(eventImpl->Dispatch(), -E_INVALID_ARGS);
763 }
764 }
765