• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <atomic>
16 #include <chrono>
17 #include <cinttypes>
18 #include <fcntl.h>
19 #include <hwext/gtest-ext.h>
20 #include <hwext/gtest-tag.h>
21 #include <memory>
22 #include <sys/eventfd.h>
23 #include <sys/types.h>
24 #include <sys/wait.h>
25 #include <thread>
26 #include <unistd.h>
27 
28 #include "epoll_event_poller.h"
29 #include "event_notifier.h"
30 #include "logging.h"
31 
32 using namespace testing::ext;
33 
34 namespace {
35 constexpr int DEFAULT_POLL_INTERVAL = 1000;
36 }
37 
38 namespace {
39 class EpollEventPollerTest : public testing::Test {
40 protected:
41     std::unique_ptr<EpollEventPoller> eventPoller;
SetUp()42     void SetUp() override
43     {
44         eventPoller = std::make_unique<EpollEventPoller>(DEFAULT_POLL_INTERVAL);
45     }
46 
TearDown()47     void TearDown() override {}
48 };
49 
50 /**
51  * @tc.name: EpollEventPollerTest
52  * @tc.desc: CtorDtor.
53  * @tc.type: FUNC
54  */
55 HWTEST_F(EpollEventPollerTest, CtorDtor, TestSize.Level1)
56 {
57     ASSERT_NE(eventPoller, nullptr);
58 }
59 
60 /**
61  * @tc.name: EpollEventPollerTest
62  * @tc.desc: InitFinalize.
63  * @tc.type: FUNC
64  */
65 HWTEST_F(EpollEventPollerTest, InitFinalize, TestSize.Level1)
66 {
67     ASSERT_NE(eventPoller, nullptr);
68 
69     HILOG_INFO(LOG_CORE, "EpollEventPollerTest.InitFinalize start!");
70     EXPECT_TRUE(eventPoller->Init());
71     EXPECT_TRUE(eventPoller->Finalize());
72 }
73 
74 /**
75  * @tc.name: EpollEventPollerTest
76  * @tc.desc: InitFinalize.
77  * @tc.type: FUNC
78  */
79 HWTEST_F(EpollEventPollerTest, InitOnly, TestSize.Level1)
80 {
81     ASSERT_NE(eventPoller, nullptr);
82     HILOG_INFO(LOG_CORE, "EpollEventPollerTest.InitOnly start!");
83     EXPECT_TRUE(eventPoller->Init());
84 }
85 
86 /**
87  * @tc.name: EpollEventPollerTest
88  * @tc.desc: Init 2 times Finalize 1 time.
89  * @tc.type: FUNC
90  */
91 HWTEST_F(EpollEventPollerTest, Init2Finalize1, TestSize.Level1)
92 {
93     ASSERT_NE(eventPoller, nullptr);
94 
95     HILOG_INFO(LOG_CORE, "EpollEventPollerTest.Init2Finalize1 start!");
96     EXPECT_TRUE(eventPoller->Init());
97     EXPECT_FALSE(eventPoller->Init());
98     EXPECT_TRUE(eventPoller->Finalize());
99 }
100 
101 /**
102  * @tc.name: EpollEventPollerTest
103  * @tc.desc: InitStartStopFinalize.
104  * @tc.type: FUNC
105  */
106 HWTEST_F(EpollEventPollerTest, InitStartStopFinalize, TestSize.Level1)
107 {
108     ASSERT_NE(eventPoller, nullptr);
109 
110     HILOG_INFO(LOG_CORE, "EpollEventPollerTest.InitStartStopFinalize start!");
111     EXPECT_TRUE(eventPoller->Init());
112     EXPECT_TRUE(eventPoller->Start());
113     EXPECT_TRUE(eventPoller->Stop());
114     EXPECT_TRUE(eventPoller->Finalize());
115 }
116 
117 /**
118  * @tc.name: EpollEventPollerTest
119  * @tc.desc: InitStartStop.
120  * @tc.type: FUNC
121  */
122 HWTEST_F(EpollEventPollerTest, InitStartStop, TestSize.Level1)
123 {
124     ASSERT_NE(eventPoller, nullptr);
125 
126     HILOG_INFO(LOG_CORE, "EpollEventPollerTest.InitStartStop start!");
127     EXPECT_TRUE(eventPoller->Init());
128     EXPECT_TRUE(eventPoller->Start());
129     EXPECT_TRUE(eventPoller->Stop());
130 }
131 
132 /**
133  * @tc.name: EpollEventPollerTest
134  * @tc.desc: InitStart.
135  * @tc.type: FUNC
136  */
137 HWTEST_F(EpollEventPollerTest, InitStart, TestSize.Level1)
138 {
139     ASSERT_NE(eventPoller, nullptr);
140 
141     HILOG_INFO(LOG_CORE, "EpollEventPollerTest.InitStart start!");
142     EXPECT_TRUE(eventPoller->Init());
143     EXPECT_TRUE(eventPoller->Start());
144 }
145 
146 /**
147  * @tc.name: EpollEventPollerTest
148  * @tc.desc: AddFd.
149  * @tc.type: FUNC
150  */
151 HWTEST_F(EpollEventPollerTest, InitStartAddFd, TestSize.Level1)
152 {
153     ASSERT_NE(eventPoller, nullptr);
154 
155     HILOG_INFO(LOG_CORE, "EpollEventPollerTest.InitStartAddFd start!");
156     EXPECT_TRUE(eventPoller->Init());
157     EXPECT_TRUE(eventPoller->Start());
158 
159     int eventFd = eventfd(0, O_CLOEXEC | O_NONBLOCK);
160     uint64_t readValue = 0;
__anonf19c1a8c0302() 161     auto onReadable = [&readValue, &eventFd]() {
162         read(eventFd, &readValue, sizeof(readValue));
163         HILOG_INFO(LOG_CORE, "EpollEventPollerTest.InitStartAddFd read %" PRIu64, readValue);
164     };
165     EXPECT_TRUE(eventPoller->AddFileDescriptor(eventFd, onReadable));
166 
167     uint64_t writeValue = 1234;
168     int writeSize = sizeof(writeValue); // run -t UT -ss developtools
169     EXPECT_EQ(write(eventFd, &writeValue, writeSize), writeSize);
170 
171     std::this_thread::yield();
172     usleep(15 * 1000);
173     EXPECT_EQ(readValue, writeValue);
174 
175     EXPECT_TRUE(eventPoller->Stop());
176     EXPECT_TRUE(eventPoller->Finalize());
177 
178     close(eventFd);
179 }
180 
181 HWTEST_F(EpollEventPollerTest, InitStartAddEventFd, TestSize.Level1)
182 {
183     ASSERT_NE(eventPoller, nullptr);
184 
185     HILOG_INFO(LOG_CORE, "EpollEventPollerTest.InitStartAddFd start!");
186     EXPECT_TRUE(eventPoller->Init());
187     EXPECT_TRUE(eventPoller->Start());
188 
189     auto notifier = EventNotifier::Create(0, EventNotifier::NONBLOCK);
190     uint64_t readValue = 0;
__anonf19c1a8c0402() 191     auto onReadable = [&readValue, &notifier]() {
192         readValue = notifier->Take();
193         HILOG_INFO(LOG_CORE, "EpollEventPollerTest.InitStartAddFd read %" PRIu64, readValue);
194     };
195     EXPECT_TRUE(eventPoller->AddFileDescriptor(notifier->GetFd(), onReadable));
196 
197     uint64_t writeValue = 1234;
198     pid_t pid = fork();
199     EXPECT_GE(pid, 0);
200     if (pid == 0) {
201         int evFd = dup(notifier->GetFd());
202         auto evNotifier = EventNotifier::CreateWithFd(evFd);
203         evNotifier->Post(writeValue);
204         _exit(0);
205     } else if (pid > 0) {
206         int wstatus = 0;
207         waitpid(pid, &wstatus, 0);
208     }
209 
210     std::this_thread::yield();
211     usleep(15 * 1000);
212     EXPECT_EQ(readValue, writeValue);
213 
214     EXPECT_TRUE(eventPoller->Stop());
215     EXPECT_TRUE(eventPoller->Finalize());
216 }
217 } // namespace