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;
__anon382418100202() 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: 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;
__anon382418100302() 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;
__anon382418100402(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);
__anon382418100602() 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);
__anon382418100702(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: 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);
__anon382418100802() 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(
__anon382418100902(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);
__anon382418100b02() 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);
__anon382418100c02(EventsMask revents) 351 errCode = timer->SetAction([&counter](EventsMask revents) -> int { ++counter; return -E_STALE; },
__anon382418100d02() 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: 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);
__anon382418100e02() 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(
__anon382418100f02(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 RefObject::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 DistributedDB::RefObject::KillAndDecObjRef(loop);
448 }
449
450 /**
451 * @tc.name: EventLoopTest002
452 * @tc.desc: Test interface if args is invalid
453 * @tc.type: FUNC
454 * @tc.require:
455 * @tc.author: chenchaohao
456 */
457 HWTEST_F(DistributedDBEventLoopTimerTest, EventLoopTest002, TestSize.Level0)
458 {
459 // ready data
460 ASSERT_NE(g_loop, nullptr);
461
462 /**
463 * @tc.steps: step1. test the loop interface.
464 * @tc.expected: step1. return INVALID_AGRS.
465 */
466 EXPECT_EQ(g_loop->Add(nullptr), -E_INVALID_ARGS);
467 EXPECT_EQ(g_loop->Remove(nullptr), -E_INVALID_ARGS);
468 EXPECT_EQ(g_loop->Stop(), E_OK);
469
470 EventLoopImpl *loopImpl= static_cast<EventLoopImpl *>(g_loop);
471 EventsMask events = 1u;
472 EXPECT_EQ(loopImpl->Modify(nullptr, true, events), -E_INVALID_ARGS);
473 EXPECT_EQ(loopImpl->Modify(nullptr, 0), -E_INVALID_ARGS);
474 }
475
476 /**
477 * @tc.name: EventTest001
478 * @tc.desc: Test CreateEvent if args is invalid
479 * @tc.type: FUNC
480 * @tc.require:
481 * @tc.author: chenchaohao
482 */
483 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest001, TestSize.Level0)
484 {
485 /**
486 * @tc.steps:step1. set EventTime = -1 and CreateEvent
487 * @tc.expected: step1. return INVALID_ARGS
488 */
489 EventTime eventTime = -1; // -1 is invalid arg
490 int errCode = E_OK;
491 IEvent *event = IEvent::CreateEvent(eventTime, errCode);
492 ASSERT_EQ(event, nullptr);
493 EXPECT_EQ(errCode, -E_INVALID_ARGS);
494
495 /**
496 * @tc.steps:step2. set EventsMask = 0 and CreateEvent
497 * @tc.expected: step2. return INVALID_ARGS
498 */
499 EventFd eventFd = EventFd();
500 EventsMask events = 0u;
501 event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
502 ASSERT_EQ(event, nullptr);
503 EXPECT_EQ(errCode, -E_INVALID_ARGS);
504
505 /**
506 * @tc.steps:step3. set EventsMask = 4 and EventFd is invalid then CreateEvent
507 * @tc.expected: step3. return INVALID_ARGS
508 */
509 EventsMask eventsMask = 1u; // 1 is ET_READ
510 event = IEvent::CreateEvent(eventFd, eventsMask, eventTime, errCode);
511 ASSERT_EQ(event, nullptr);
512 EXPECT_EQ(errCode, -E_INVALID_ARGS);
513
514 /**
515 * @tc.steps:step4. set EventsMask = 8 and CreateEvent
516 * @tc.expected: step4. return INVALID_ARGS
517 */
518 events |= ET_TIMEOUT;
519 event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
520 ASSERT_EQ(event, nullptr);
521 EXPECT_EQ(errCode, -E_INVALID_ARGS);
522
523 /**
524 * @tc.steps:step5. set EventTime = 1 and CreateEvent
525 * @tc.expected: step5. return OK
526 */
527 eventTime = TIME_PIECE_1;
528 eventFd = EventFd(epoll_create(EPOLL_INIT_REVENTS));
529 event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
530 ASSERT_NE(event, nullptr);
531 EXPECT_EQ(errCode, E_OK);
532 DistributedDB::RefObject::KillAndDecObjRef(event);
533 }
534
535 /**
536 * @tc.name: EventTest002
537 * @tc.desc: Test SetAction if action is nullptr
538 * @tc.type: FUNC
539 * @tc.require:
540 * @tc.author: chenchaohao
541 */
542 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest002, TestSize.Level0)
543 {
544 /**
545 * @tc.steps:step1. CreateEvent
546 * @tc.expected: step1. return OK
547 */
548 EventTime eventTime = TIME_PIECE_1;
549 int errCode = E_OK;
550 IEvent *event = IEvent::CreateEvent(eventTime, errCode);
551 ASSERT_NE(event, nullptr);
552 EXPECT_EQ(errCode, E_OK);
553
554 /**
555 * @tc.steps:step2. SetAction with nullptr
556 * @tc.expected: step2. return INVALID_ARGS
557 */
558 EXPECT_EQ(event->SetAction(nullptr), -E_INVALID_ARGS);
559 DistributedDB::RefObject::KillAndDecObjRef(event);
560 }
561
562 /**
563 * @tc.name: EventTest003
564 * @tc.desc: Test AddEvents and RemoveEvents with fd is invalid
565 * @tc.type: FUNC
566 * @tc.require:
567 * @tc.author: chenchaohao
568 */
569 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest003, TestSize.Level0)
570 {
571 /**
572 * @tc.steps:step1. CreateEvent
573 * @tc.expected: step1. return OK
574 */
575 EventTime eventTime = TIME_PIECE_1;
576 int errCode = E_OK;
577 IEvent *event = IEvent::CreateEvent(eventTime, errCode);
578 ASSERT_NE(event, nullptr);
579 EXPECT_EQ(errCode, E_OK);
580
581 /**
582 * @tc.steps:step2. AddEvents and RemoveEvents with events is 0
583 * @tc.expected: step2. return INVALID_ARGS
584 */
585 EventsMask events = 0u;
586 EXPECT_EQ(event->AddEvents(events), -E_INVALID_ARGS);
587 EXPECT_EQ(event->RemoveEvents(events), -E_INVALID_ARGS);
588
589 /**
590 * @tc.steps:step3. AddEvents and RemoveEvents with fd is invalid
591 * @tc.expected: step3. return OK
592 */
593 events |= ET_READ;
594 EXPECT_EQ(event->AddEvents(events), -E_INVALID_ARGS);
595 EXPECT_EQ(event->RemoveEvents(events), -E_INVALID_ARGS);
596 DistributedDB::RefObject::KillAndDecObjRef(event);
597 }
598
599 /**
600 * @tc.name: EventTest004
601 * @tc.desc: Test AddEvents and RemoveEvents with fd is valid
602 * @tc.type: FUNC
603 * @tc.require:
604 * @tc.author: chenchaohao
605 */
606 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest004, TestSize.Level0)
607 {
608 /**
609 * @tc.steps:step1. CreateEvent
610 * @tc.expected: step1. return OK
611 */
612 EventTime eventTime = TIME_PIECE_1;
613 EventFd eventFd = EventFd(epoll_create(EPOLL_INIT_REVENTS));
614 EventsMask events = 1u; // 1 means ET_READ
615 int errCode = E_OK;
616 IEvent *event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
617 ASSERT_NE(event, nullptr);
618 EXPECT_EQ(errCode, E_OK);
619
620 /**
621 * @tc.steps:step2. AddEvents and RemoveEvents with fd is valid
622 * @tc.expected: step2. return OK
623 */
624 events |= ET_WRITE;
625 EXPECT_EQ(event->AddEvents(events), E_OK);
626 EXPECT_EQ(event->RemoveEvents(events), E_OK);
627
628 /**
629 * @tc.steps:step3. AddEvents and RemoveEvents after set action
630 * @tc.expected: step3. return OK
631 */
632 ASSERT_EQ(g_loop->Add(event), -E_INVALID_ARGS);
__anon382418101002(EventsMask revents) 633 ASSERT_EQ(event->SetAction([](EventsMask revents) -> int {
634 return E_OK;
635 }), E_OK);
636 ASSERT_EQ(g_loop->Add(event), E_OK);
637 EXPECT_EQ(event->AddEvents(events), E_OK);
638 EXPECT_EQ(event->RemoveEvents(events), E_OK);
639 DistributedDB::RefObject::KillAndDecObjRef(event);
640 }
641
642 /**
643 * @tc.name: EventTest005
644 * @tc.desc: Test constructor method with timeout < 0
645 * @tc.type: FUNC
646 * @tc.require:
647 * @tc.author: chenchaohao
648 */
649 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest005, TestSize.Level0)
650 {
651 /**
652 * @tc.steps:step1. instantiation event with eventTime
653 * @tc.expected: step1. return OK
654 */
655 EventTime eventTime = -1; // -1 is invalid arg
656 IEvent *event = new (std::nothrow) EventImpl(eventTime);
657 ASSERT_NE(event, nullptr);
658 DistributedDB::RefObject::KillAndDecObjRef(event);
659
660 /**
661 * @tc.steps:step2. instantiation event with eventFd, events, eventTime
662 * @tc.expected: step2. return OK
663 */
664 EventFd eventFd = EventFd();
665 EventsMask events = 1u; // 1 means ET_READ
666 EventImpl *eventImpl = new (std::nothrow) EventImpl(eventFd, events, eventTime);
667 ASSERT_NE(eventImpl, nullptr);
668 DistributedDB::RefObject::KillAndDecObjRef(eventImpl);
669 }
670
671 /**
672 * @tc.name: EventTest006
673 * @tc.desc: Test SetTimeout
674 * @tc.type: FUNC
675 * @tc.require:
676 * @tc.author: chenchaohao
677 */
678 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest006, TestSize.Level0)
679 {
680 /**
681 * @tc.steps:step1. CreateEvent
682 * @tc.expected: step1. return OK
683 */
684 EventTime eventTime = TIME_PIECE_1;
685 int errCode = E_OK;
686 IEvent *event = IEvent::CreateEvent(eventTime, errCode);
687 ASSERT_NE(event, nullptr);
688 EXPECT_EQ(errCode, E_OK);
689
690 /**
691 * @tc.steps:step2. SetTimeout
692 * @tc.expected: step2. return INVALID_ARGS
693 */
694 event->IgnoreFinalizer();
695 EXPECT_EQ(event->SetTimeout(eventTime), E_OK);
696 eventTime = -1; // -1 is invalid args
697 EXPECT_EQ(event->SetTimeout(eventTime), -E_INVALID_ARGS);
698 DistributedDB::RefObject::KillAndDecObjRef(event);
699 }
700
701 /**
702 * @tc.name: EventTest007
703 * @tc.desc: Test SetEvents and GetEvents
704 * @tc.type: FUNC
705 * @tc.require:
706 * @tc.author: chenchaohao
707 */
708 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest007, TestSize.Level0)
709 {
710 /**
711 * @tc.steps:step1. CreateEvent
712 * @tc.expected: step1. return OK
713 */
714 EventTime eventTime = TIME_PIECE_1;
715 EventFd eventFd = EventFd(epoll_create(EPOLL_INIT_REVENTS));
716 EventsMask events = 1u; // 1 means ET_READ
717 int errCode = E_OK;
718 IEvent *event = IEvent::CreateEvent(eventFd, events, eventTime, errCode);
719 ASSERT_NE(event, nullptr);
720 EXPECT_EQ(errCode, E_OK);
721
722 /**
723 * @tc.steps:step2. Test GetEventFd and GetEvents
724 * @tc.expected: step2. return OK
725 */
726 EventImpl *eventImpl = static_cast<EventImpl *>(event);
727 eventImpl->SetRevents(events);
728 EXPECT_EQ(eventImpl->GetEventFd(), eventFd);
729 EXPECT_EQ(eventImpl->GetEvents(), events);
730 events = 2u; // 2 means ET_WRITE
731 eventImpl->SetEvents(true, events);
732 EXPECT_EQ(eventImpl->GetEvents(), 3u); // 3 means ET_WRITE | ET_READ
733 eventImpl->SetEvents(false, events);
734 EXPECT_EQ(eventImpl->GetEvents(), 1u); // 1 means ET_READ
735 EXPECT_FALSE(eventImpl->GetTimeoutPoint(eventTime));
736 DistributedDB::RefObject::KillAndDecObjRef(eventImpl);
737 }
738
739 /**
740 * @tc.name: EventTest008
741 * @tc.desc: Test SetTimeoutPeriod and GetTimeoutPoint
742 * @tc.type: FUNC
743 * @tc.require:
744 * @tc.author: chenchaohao
745 */
746 HWTEST_F(DistributedDBEventLoopTimerTest, EventTest008, TestSize.Level0)
747 {
748 /**
749 * @tc.steps:step1. CreateEvent
750 * @tc.expected: step1. return OK
751 */
752 EventTime eventTime = TIME_PIECE_1;
753 int errCode = E_OK;
754 IEvent *event = IEvent::CreateEvent(eventTime, errCode);
755 ASSERT_NE(event, nullptr);
756 EXPECT_EQ(errCode, E_OK);
757
758 /**
759 * @tc.steps:step2. SetTimeoutPeriod and GetTimeoutPoint
760 * @tc.expected: step2. return OK
761 */
762 EventImpl *eventImpl = static_cast<EventImpl *>(event);
763 eventTime = -1; // -1 is invalid args
764 eventImpl->SetTimeoutPeriod(eventTime);
765 EXPECT_TRUE(eventImpl->GetTimeoutPoint(eventTime));
766
767 /**
768 * @tc.steps:step3. Dispatch
769 * @tc.expected: step3. return INVALID_ARGS
770 */
771 EXPECT_EQ(eventImpl->Dispatch(), -E_INVALID_ARGS);
772 DistributedDB::RefObject::KillAndDecObjRef(eventImpl);
773 }
774 }
775