• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "watchdog_task_test.h"
16 
17 #include <gtest/gtest.h>
18 #include <string>
19 #include <thread>
20 
21 #include "watchdog_task.h"
22 #include "xcollie_utils.h"
23 #include "directory_ex.h"
24 #include "file_ex.h"
25 #include "event_handler.h"
26 #include "ffrt_inner.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS::AppExecFwk;
30 namespace OHOS {
31 namespace HiviewDFX {
SetUpTestCase(void)32 void WatchdogTaskTest::SetUpTestCase(void)
33 {
34 }
35 
TearDownTestCase(void)36 void WatchdogTaskTest::TearDownTestCase(void)
37 {
38 }
39 
SetUp(void)40 void WatchdogTaskTest::SetUp(void)
41 {
42 }
43 
TearDown(void)44 void WatchdogTaskTest::TearDown(void)
45 {
46 }
47 
48 /**
49  * @tc.name: WatchdogTaskTest_001
50  * @tc.desc: add testcase code coverage
51  * @tc.type: FUNC
52  */
53 HWTEST_F(WatchdogTaskTest, WatchdogTaskTest_001, TestSize.Level1)
54 {
55     int taskResult = 0;
__anond178ecb90102() 56     auto taskFunc = [&taskResult]() { taskResult = 1; };
57     WatchdogTask task("WatchdogTaskTest_001", taskFunc, 0, 0, true);
58     task.DoCallback();
59     EXPECT_EQ(task.flag, 0);
60     task.flag = 1;
61     task.DoCallback();
62     task.flag = 2;
63     task.DoCallback();
64 }
65 
66 /**
67  * @tc.name: WatchdogTaskTest_002
68  * @tc.desc: add testcase code coverage
69  * @tc.type: FUNC
70  */
71 HWTEST_F(WatchdogTaskTest, WatchdogTaskTest_002, TestSize.Level1)
72 {
73     uint64_t now = GetCurrentTickMillseconds() + 1000;
74     int taskResult = 0;
__anond178ecb90202() 75     auto taskFunc = [&taskResult]() { taskResult = 1; };
76     WatchdogTask task("WatchdogTaskTest_002", taskFunc, 0, 0, true);
77     task.Run(now);
78     EXPECT_EQ(task.GetBlockDescription(1), "Watchdog: thread(WatchdogTaskTest_002) blocked 1s");
79 }
80 
81 /**
82  * @tc.name: WatchdogTaskTest_003
83  * @tc.desc: add testcase code coverage
84  * @tc.type: FUNC
85  */
86 HWTEST_F(WatchdogTaskTest, WatchdogTaskTest_003, TestSize.Level1)
87 {
88     int taskResult = 0;
__anond178ecb90302() 89     auto taskFunc = [&taskResult]() { taskResult = 1; };
90     WatchdogTask task("WatchdogTaskTest_003", taskFunc, 0, 0, true);
91     task.RunHandlerCheckerTask();
92     EXPECT_TRUE(task.checker == nullptr);
93 
94     auto runner = EventRunner::Create(true);
95     auto handler = std::make_shared<EventHandler>(runner);
96     WatchdogTask task1("WatchdogTaskTest_003", handler, nullptr, 5);
97     task1.RunHandlerCheckerTask();
98     int ret = task1.EvaluateCheckerState();
99     EXPECT_TRUE(ret >= 0);
100 }
101 
102 /**
103  * @tc.name: WatchdogTaskTest_004
104  * @tc.desc: add testcase code coverage
105  * @tc.type: FUNC
106  */
107 HWTEST_F(WatchdogTaskTest, WatchdogTaskTest_004, TestSize.Level1)
108 {
109     WatchdogTask task("WatchdogTaskTest_004", 0, 1);
110     uint64_t now = GetCurrentTickMillseconds();
111     task.triggerTimes.push_back(now);
112     task.triggerTimes.push_back(now + 1000);
113     task.triggerTimes.push_back(now + 2000);
114     task.TimerCountTask();
115     EXPECT_TRUE(task.countLimit != 0);
116 }
117 
118 #ifdef SUSPEND_CHECK_ENABLE
119 /**
120  * @tc.name: WatchdogTask_GetSuspendTimeTask_001
121  * @tc.desc: verify wherther the time is read successfully from the file
122  * @tc.type: FUNC
123  */
124 HWTEST_F(WatchdogTaskTest, WatchdogTask_GetSuspendTimeTask_001, TestSize.Level1)
125 {
126     uint64_t now = GetCurrentTickMillseconds();
127     const char *LAST_SUSPEND_TIME_PATH = "/sys/power/last_sr";
128     auto [lastSuspendStartTime, lastSuspendEndTime] = GetSuspendTime(LAST_SUSPEND_TIME_PATH, now);
129     ASSERT_GT(lastSuspendStartTime, -1);
130     ASSERT_GT(lastSuspendEndTime, -1);
131 }
132 
133 /**
134  * @tc.name: WatchdogTask_WatchdogSkipCheckWhenSuspend_001
135  * @tc.desc: verify wherther the task skipped successfully
136  * @tc.type: FUNC
137  */
138 HWTEST_F(WatchdogTaskTest, WatchdogTask_WatchdogSkipCheckWhenSuspend_001, TestSize.Level1)
139 {
140     int taskResult = 0;
__anond178ecb90402() 141     auto taskFunc = [&taskResult]() { taskResult = 1; };
142     const std::string name = "SkipCheckWhenSuspend_001";
143     uint64_t delay = 0;
144     uint64_t interval = 5000;
145     bool isOneshot = true;
146     WatchdogTask task(name, taskFunc, delay, interval, isOneshot);
147     double testLastSuspendStartTime = 1500.238412;
148     double testLastSuspendEndTime = 900.391023;
149     uint64_t testNow = 1600;
150     ASSERT_EQ(task.ShouldSkipCheckForSuspend(testNow, testLastSuspendStartTime, testLastSuspendEndTime), true);
151 }
152 
153 /**
154  * @tc.name: WatchdogTask_WatchdogSkipCheckWhenSuspend_002
155  * @tc.desc: verify wherther the task skipped successfully
156  * @tc.type: FUNC
157  */
158 HWTEST_F(WatchdogTaskTest, WatchdogTask_WatchdogSkipCheckWhenSuspend_002, TestSize.Level1)
159 {
160     int taskResult = 0;
__anond178ecb90502() 161     auto taskFunc = [&taskResult]() { taskResult = 1; };
162     const std::string name = "SkipCheckWhenSuspend_002";
163     uint64_t delay = 0;
164     uint64_t interval = 10000;
165     bool isOneshot = true;
166     WatchdogTask task(name, taskFunc, delay, interval, isOneshot);
167     double testLastSuspendStartTime = 9000.391023;
168     double testLastSuspendEndTime = 15000.238412;
169     uint64_t testNow = 16000;
170     ASSERT_EQ(task.ShouldSkipCheckForSuspend(testNow, testLastSuspendStartTime, testLastSuspendEndTime), true);
171 }
172 
173 /**
174  * @tc.name: WatchdogTask_WatchdogSkipCheckWhenSuspend_003
175  * @tc.desc: verify wherther the task skipped successfully
176  * @tc.type: FUNC
177  */
178 HWTEST_F(WatchdogTaskTest, WatchdogTask_WatchdogSkipCheckWhenSuspend_003, TestSize.Level1)
179 {
180     int taskResult = 0;
__anond178ecb90602() 181     auto taskFunc = [&taskResult]() { taskResult = 1; };
182     const std::string name = "SkipCheckWhenSuspend_003";
183     uint64_t delay = 0;
184     uint64_t interval = 3000;
185     bool isOneshot = true;
186     WatchdogTask task(name, taskFunc, delay, interval, isOneshot);
187     double testLastSuspendStartTime = 9000.391023;
188     double testLastSuspendEndTime = 15000.238412;
189     uint64_t testNow = 16000;
190     ASSERT_EQ(task.ShouldSkipCheckForSuspend(testNow, testLastSuspendStartTime, testLastSuspendEndTime), false);
191 }
192 
193 /**
194  * @tc.name: WatchdogTask_WatchdogSkipCheckWhenSuspend_004
195  * @tc.desc: verify wherther the task skipped successfully
196  * @tc.type: FUNC
197  */
198 HWTEST_F(WatchdogTaskTest, WatchdogTask_WatchdogSkipCheckWhenSuspend_004, TestSize.Level1)
199 {
200     int taskResult = 0;
__anond178ecb90702() 201     auto taskFunc = [&taskResult]() { taskResult = 1; };
202     const std::string name = "SkipCheckWhenSuspend_004";
203     uint64_t delay = 0;
204     uint64_t interval = 3000;
205     bool isOneshot = true;
206     WatchdogTask task(name, taskFunc, delay, interval, isOneshot);
207     double testLastSuspendStartTime = -1;
208     double testLastSuspendEndTime = 15000.238412;
209     uint64_t testNow = 16000;
210     ASSERT_EQ(task.ShouldSkipCheckForSuspend(testNow, testLastSuspendStartTime, testLastSuspendEndTime), false);
211 }
212 
213 /**
214  * @tc.name: WatchdogTask_WatchdogSkipCheckWhenSuspend_005
215  * @tc.desc: verify wherther the task skipped successfully
216  * @tc.type: FUNC
217  */
218 HWTEST_F(WatchdogTaskTest, WatchdogTask_WatchdogSkipCheckWhenSuspend_005, TestSize.Level1)
219 {
220     int taskResult = 0;
__anond178ecb90802() 221     auto taskFunc = [&taskResult]() { taskResult = 1; };
222     const std::string name = "SkipCheckWhenSuspend_005";
223     uint64_t delay = 0;
224     uint64_t interval = 3000;
225     bool isOneshot = true;
226     WatchdogTask task(name, taskFunc, delay, interval, isOneshot);
227     double testLastSuspendStartTime = -1;
228     double testLastSuspendEndTime = -1;
229     uint64_t testNow = -1;
230     ASSERT_EQ(task.ShouldSkipCheckForSuspend(testNow, testLastSuspendStartTime, testLastSuspendEndTime), false);
231 }
232 
233 #endif
234 } // namespace HiviewDFX
235 } // namespace OHOS
236