1 /*
2 * Copyright (c) 2021-2022 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 <fcntl.h>
17
18 #include "event_handler_test_common.h"
19
20 #include <gtest/gtest.h>
21
22 using namespace testing::ext;
23 using namespace OHOS;
24 using namespace OHOS::AppExecFwk;
25
26 namespace {
27 const uint32_t TEST_TIMES = 10000;
28 uint32_t g_sendCount = 0;
29 uint32_t g_readDataSize = 0;
30 uint32_t g_writeDataSize = 0;
31
RandomInTen()32 inline uint32_t RandomInTen()
33 {
34 uint32_t remainderNum = 10;
35 return static_cast<uint32_t>(std::rand() % remainderNum);
36 }
37
CreateData(size_t dataSize)38 std::string CreateData(size_t dataSize)
39 {
40 std::string message = "";
41 for (size_t i = 0; i < dataSize; i++) {
42 message += std::to_string(RandomInTen());
43 }
44 return message;
45 }
46 } // unnamed namespace
47
48 class EventHandlerPressModuleTest : public testing::Test {
49 public:
50 static void SetUpTestCase(void);
51 static void TearDownTestCase(void);
52 void SetUp();
53 void TearDown();
54 };
55
SetUpTestCase(void)56 void EventHandlerPressModuleTest::SetUpTestCase(void)
57 {}
58
TearDownTestCase(void)59 void EventHandlerPressModuleTest::TearDownTestCase(void)
60 {}
61
SetUp(void)62 void EventHandlerPressModuleTest::SetUp(void)
63 {
64 /**
65 * @tc.setup: Set the value of test flags to the default.
66 */
67 CommonUtils::EventRunSet(false);
68 CommonUtils::EventRunCountReset();
69 g_sendCount = 0;
70 g_readDataSize = 0;
71 g_writeDataSize = 0;
72 }
73
TearDown(void)74 void EventHandlerPressModuleTest::TearDown(void)
75 {}
76
77 class MyFileDescriptorListener : public FileDescriptorListener {
78 public:
MyFileDescriptorListener()79 MyFileDescriptorListener()
80 {}
~MyFileDescriptorListener()81 ~MyFileDescriptorListener()
82 {}
83
OnReadable(int32_t fileDescriptor)84 void OnReadable(int32_t fileDescriptor) override
85 {
86 uint32_t messageSize = 1024;
87 char message[messageSize];
88 ssize_t retVal = read(fileDescriptor, message, sizeof(message) - 1);
89 if (retVal > 0) {
90 message[retVal] = '\0';
91 g_readDataSize += retVal;
92 }
93 }
94
OnWritable(int32_t fileDescriptor)95 void OnWritable(int32_t fileDescriptor) override
96 {
97 auto handler = GetOwner();
98 if (handler) {
99 handler->RemoveFileDescriptorListener(fileDescriptor);
100 size_t dataSize = 10;
101 int64_t delayTime = 10;
102 uint32_t writeTimes = 1000;
103 for (uint32_t i = 0; i < writeTimes; ++i) {
104 std::string message = CreateData(dataSize);
105 auto f = [fileDescriptor, message]() { write(fileDescriptor, message.c_str(), message.size()); };
106 handler->PostTask(f, delayTime * (i + 1));
107 g_writeDataSize += message.size();
108 }
109 }
110 }
111 };
112
113 /**
114 * @tc.name: SendPress001
115 * @tc.desc: Send the same event for 10000 times
116 * @tc.type: FUNC
117 * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
118 */
119 HWTEST_F(EventHandlerPressModuleTest, SendPress001, TestSize.Level3)
120 {
121 /**
122 * @tc.steps: step1. Send event for 10000 times.
123 * @tc.expected: step1. Send successfully and the event handled, and the succeeded times equals to test times.
124 */
125 auto myRunner = EventRunner::Create(false);
126 auto handler = std::make_shared<MyEventHandler>(myRunner);
127
128 for (uint32_t i = 0; i < TEST_TIMES; ++i) {
129 bool sentResult = handler->SendEvent(RUN_EVENT_ID);
130 handler->SendEvent(STOP_EVENT_ID);
131 myRunner->Run();
132 if (sentResult && CommonUtils::EventRunGet()) {
133 g_sendCount++;
134 }
135 }
136 EXPECT_EQ(TEST_TIMES, g_sendCount);
137 }
138
139 /**
140 * @tc.name: SendPress002
141 * @tc.desc: Send event by different handler for 10000 times
142 * @tc.type: FUNC
143 * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
144 */
145 HWTEST_F(EventHandlerPressModuleTest, SendPress002, TestSize.Level3)
146 {
147 /**
148 * @tc.steps: step1. Send event by different handler for 10000 times.
149 * @tc.expected: step1. Send successfully and the event handled,
150 * and the succeeded times equals to test times.
151 * the test result is right, equals to test times multiply by 2.
152 */
153 auto myRunner = EventRunner::Create(false);
154 auto handler1 = std::make_shared<MyEventHandler>(myRunner);
155 auto handler2 = std::make_shared<MyEventHandler>(myRunner);
156
157 for (uint32_t i = 0; i < TEST_TIMES; ++i) {
158 bool sentResult1 = handler1->SendEvent(RUN_EVENT_ID);
159 bool sentResult2 = handler2->SendEvent(RUN_EVENT_ID);
160 handler2->SendEvent(STOP_EVENT_ID);
161 myRunner->Run();
162 if (sentResult1 && sentResult2) {
163 g_sendCount++;
164 }
165 }
166 EXPECT_EQ(TEST_TIMES, g_sendCount);
167 EXPECT_EQ(TEST_TIMES * 2, CommonUtils::EventRunCount());
168 }
169
170 /**
171 * @tc.name: SendPress003
172 * @tc.desc: Send different event for 10000 times
173 * @tc.type: FUNC
174 * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
175 */
176 HWTEST_F(EventHandlerPressModuleTest, SendPress003, TestSize.Level3)
177 {
178 /**
179 * @tc.steps: step1. Send different event for 10000 times.
180 * @tc.expected: step1. Send successfully and the event handled, and the succeeded times equals to test times.
181 */
182 auto myRunner = EventRunner::Create(true);
183 auto handler = std::make_shared<MyEventHandler>(myRunner);
184 for (uint32_t i = 0; i < TEST_TIMES; ++i) {
185 bool sentResult = handler->SendEvent(Random() + 1);
186 if (sentResult) {
187 g_sendCount++;
188 }
189 }
190 handler->SendEvent(STOP_EVENT_ID);
191 EXPECT_EQ(TEST_TIMES, g_sendCount);
192 }
193
194 /**
195 * @tc.name: FdListenerPress001
196 * @tc.desc: Press test of listener via epoll
197 * @tc.type: FUNC
198 * @tc.require: SR000BTOPD SR000BTOPJ SR000BTOPM
199 */
200 HWTEST_F(EventHandlerPressModuleTest, FdListenerPress001, TestSize.Level3)
201 {
202 /**
203 * @tc.steps: step1. Add fd listener, write and read data for 1000 times.
204 * @tc.expected: step1. Add fd listener of input and output successfully, write and read the data correctly.
205 */
206 int32_t fds[] = {-1, -1};
207 int32_t pipe = pipe2(fds, O_NONBLOCK);
208 EXPECT_GE(pipe, 0);
209
210 auto listener = std::make_shared<MyFileDescriptorListener>();
211 auto myRunner = EventRunner::Create(false);
212 auto handler = std::make_shared<MyEventHandler>(myRunner);
213 auto inResult = handler->AddFileDescriptorListener(fds[0], FILE_DESCRIPTOR_INPUT_EVENT, listener);
214 EXPECT_EQ(inResult, ERR_OK);
215 auto outResult = handler->AddFileDescriptorListener(fds[1], FILE_DESCRIPTOR_OUTPUT_EVENT, listener);
216 EXPECT_EQ(outResult, ERR_OK);
217
218 int64_t delayTime = 20000;
219 int64_t param = 0;
220 handler->SendEvent(STOP_EVENT_ID, param, delayTime);
221 myRunner->Run();
222 EXPECT_EQ(g_writeDataSize, g_readDataSize);
223 }