• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifdef HAS_HICHECKER_NATIVE_PART
17 #include <atomic>
18 #include <cerrno>
19 #include <thread>
20 
21 #include <sys/prctl.h>
22 
23 #include <gtest/gtest.h>
24 #include "event_handler.h"
25 #include "event_runner.h"
26 #include "hichecker.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS;
30 using namespace OHOS::AppExecFwk;
31 using namespace OHOS::HiviewDFX;
32 
33 bool isSetLogger = false;
34 
35 class LibEventHandlerCheckTest : public testing::Test {
36 public:
37     static void SetUpTestCase(void);
38     static void TearDownTestCase(void);
39     void SetUp();
40     void TearDown();
41 };
42 
43 class LoggerTest : public Logger {
44 public:
45     /**
46      * Processes the content of a specified string.
47      * @param message the content of a specified string.
48      */
Log(const std::string & line)49     void Log(const std::string &line)
50     {
51         isSetLogger = true;
52         GTEST_LOG_(INFO) << line;
53     };
~LoggerTest()54     virtual ~LoggerTest()
55     {}
56 };
57 
SetUpTestCase(void)58 void LibEventHandlerCheckTest::SetUpTestCase(void)
59 {}
60 
TearDownTestCase(void)61 void LibEventHandlerCheckTest::TearDownTestCase(void)
62 {}
63 
SetUp(void)64 void LibEventHandlerCheckTest::SetUp(void)
65 {}
66 
TearDown(void)67 void LibEventHandlerCheckTest::TearDown(void)
68 {}
69 
70 /*
71  * @tc.name: EventTimeout001
72  * @tc.desc: Do not invoke AddRule distribution execution times out.
73  * @tc.type: FUNC
74  */
75 HWTEST_F(LibEventHandlerCheckTest, EventTimeout001, TestSize.Level1)
76 {
77     /**
78      * @tc.setup: init handler and runner and deliveryTimeout and distributeTimeout.
79      */
80     auto runner = EventRunner::Create(true);
81     auto handler = std::make_shared<EventHandler>(runner);
__anoned5cf3ef0102() 82     auto fs = []() {
83         usleep(2000);
84     };
__anoned5cf3ef0202() 85     auto f = []() {
86         usleep(10000);
87     };
88     bool deliveryTimeout_ = false;
__anoned5cf3ef0302() 89     auto deliveryTimeoutThread = [&deliveryTimeout_]() {
90         deliveryTimeout_ = true;
91     };
92     bool distributeTimeout_ = false;
__anoned5cf3ef0402() 93     auto distributeTimeoutThread = [&distributeTimeout_]() {
94         distributeTimeout_ = true;
95     };
96     int64_t deliveryTimeout = 1;
97     int64_t distributeTimeout = 5;
98 
99     /**
100      * @tc.steps: step1. post task and run the runner.
101      * @tc.expected: step1. timeout success, but no hiCheck log.
102      */
103 
104     runner->SetDeliveryTimeout(deliveryTimeout);
105     runner->SetDistributeTimeout(distributeTimeout);
106     handler->PostTask(fs);
107     handler->PostTask(f);
108 
109     handler->SetDeliveryTimeoutCallback(deliveryTimeoutThread);
110     handler->SetDistributeTimeoutCallback(distributeTimeoutThread);
111 
112     usleep(100 * 1000);
113     EXPECT_EQ(false, deliveryTimeout_);
114     EXPECT_EQ(false, distributeTimeout_);
115 }
116 
117 /*
118  * @tc.name: EventTimeout002
119  * @tc.desc: Invocation AddRule distribution time out.
120  * @tc.type: FUNC
121  */
122 HWTEST_F(LibEventHandlerCheckTest, EventTimeout002, TestSize.Level1)
123 {
124     /**
125      * @tc.setup: begin check.
126      */
127     HiChecker::AddRule(Rule::RULE_CHECK_SLOW_EVENT);
128 
129     /**
130      * @tc.setup: init handler and runner and deliveryTimeout and distributeTimeout.
131      */
132     auto runner = EventRunner::Create(true);
133     auto handler = std::make_shared<EventHandler>(runner);
__anoned5cf3ef0502() 134     auto fs = []() {
135         usleep(2000);
136     };
__anoned5cf3ef0602() 137     auto f = []() {
138         usleep(10000);
139     };
140     bool deliveryTimeout_ = false;
__anoned5cf3ef0702() 141     auto deliveryTimeoutThread = [&deliveryTimeout_]() {
142         deliveryTimeout_ = true;
143     };
144     bool distributeTimeout_ = false;
__anoned5cf3ef0802() 145     auto distributeTimeoutThread = [&distributeTimeout_]() {
146         distributeTimeout_ = true;
147     };
148     int64_t deliveryTimeout = 1;
149     int64_t distributeTimeout = 100;
150 
151     /**
152      * @tc.steps: step1. post task and run the runner.
153      * @tc.expected: step1. timeout success, but no hiCheck log.
154      */
155 
156     runner->SetDeliveryTimeout(deliveryTimeout);
157     runner->SetDistributeTimeout(distributeTimeout);
158     handler->PostTask(fs);
159     handler->PostTask(f);
160 
161     handler->SetDeliveryTimeoutCallback(deliveryTimeoutThread);
162     handler->SetDistributeTimeoutCallback(distributeTimeoutThread);
163 
164     usleep(100 * 1000);
165     EXPECT_EQ(true, deliveryTimeout_);
166     EXPECT_EQ(false, distributeTimeout_);
167 }
168 /*
169  * @tc.name: EventTimeout003
170  * @tc.desc: Invocation AddRule execution time out.
171  * @tc.type: FUNC
172  */
173 HWTEST_F(LibEventHandlerCheckTest, EventTimeout003, TestSize.Level1)
174 {
175     /**
176      * @tc.setup: begin check.
177      */
178     HiChecker::AddRule(Rule::RULE_CHECK_SLOW_EVENT);
179 
180     /**
181      * @tc.setup: init handler and runner and deliveryTimeout and distributeTimeout.
182      */
183     auto runner = EventRunner::Create(true);
184     auto handler = std::make_shared<EventHandler>(runner);
__anoned5cf3ef0902() 185     auto fs = []() {
186         usleep(2000);
187     };
__anoned5cf3ef0a02() 188     auto f = []() {
189         usleep(10000);
190     };
191     bool deliveryTimeout_ = false;
__anoned5cf3ef0b02() 192     auto deliveryTimeoutThread = [&deliveryTimeout_]() {
193         deliveryTimeout_ = true;
194     };
195     bool distributeTimeout_ = false;
__anoned5cf3ef0c02() 196     auto distributeTimeoutThread = [&distributeTimeout_]() {
197         distributeTimeout_ = true;
198     };
199     int64_t deliveryTimeout = 4;
200     int64_t distributeTimeout = 6;
201 
202     /**
203      * @tc.steps: step1. post task and run the runner.
204      * @tc.expected: step1. timeout success, but no hiCheck log.
205      */
206 
207     runner->SetDeliveryTimeout(deliveryTimeout);
208     runner->SetDistributeTimeout(distributeTimeout);
209     handler->PostTask(fs);
210     handler->PostTask(f, "eventHandlerCheckTest");
211 
212     handler->SetDeliveryTimeoutCallback(deliveryTimeoutThread);
213     handler->SetDistributeTimeoutCallback(distributeTimeoutThread);
214 
215     usleep(100 * 1000);
216     EXPECT_EQ(false, deliveryTimeout_);
217     EXPECT_EQ(true, distributeTimeout_);
218 
219     int64_t deliveryTimeoutValue = runner->GetDeliveryTimeout();
220     int64_t distributeTimeoutValue = runner->GetDistributeTimeout();
221 
222     EXPECT_EQ(4, deliveryTimeoutValue);
223     EXPECT_EQ(6, distributeTimeoutValue);
224 }
225 #endif // HAS_HICHECKER_NATIVE_PART