• 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 
16 #include <gtest/gtest.h>
17 #include <string>
18 #include <thread>
19 #include "watchdog_inner_test.h"
20 
21 #define private public
22 #define protected public
23 #include "watchdog_inner.h"
24 #undef private
25 #undef protected
26 
27 #include "xcollie_utils.h"
28 #include "directory_ex.h"
29 #include "file_ex.h"
30 #include "event_handler.h"
31 #include "ffrt_inner.h"
32 
33 using namespace testing::ext;
34 using namespace OHOS::AppExecFwk;
35 namespace OHOS {
36 namespace HiviewDFX {
SetUpTestCase(void)37 void WatchdogInnerTest::SetUpTestCase(void)
38 {
39 }
40 
TearDownTestCase(void)41 void WatchdogInnerTest::TearDownTestCase(void)
42 {
43 }
44 
SetUp(void)45 void WatchdogInnerTest::SetUp(void)
46 {
47 }
48 
TearDown(void)49 void WatchdogInnerTest::TearDown(void)
50 {
51 }
52 
53 /**
54  * @tc.name: WatchdogInner thread run a oneshot task
55  * @tc.desc: Verify whether the task has been executed failed
56  * @tc.type: FUNC
57  */
58 HWTEST_F(WatchdogInnerTest, WatchdogInnerTest_RunOneShotTask_001, TestSize.Level1)
59 {
60     int taskResult = 0;
__anon71a615610102() 61     auto taskFunc = [&taskResult]() { taskResult = 1; };
62     WatchdogInner::GetInstance().RunOneShotTask("", taskFunc, 0);
63     ASSERT_EQ(taskResult, 0);
64 }
65 
66 /**
67  * @tc.name: WatchdogInner thread run a periodical task
68  * @tc.desc: Verify whether the task has been executed failed
69  * @tc.type: FUNC
70  */
71 HWTEST_F(WatchdogInnerTest, WatchdogInnerTest_RunPeriodicalTask_001, TestSize.Level1)
72 {
73     int taskResult = 0;
__anon71a615610202() 74     auto taskFunc = [&taskResult]() { taskResult = 1; };
75     WatchdogInner::GetInstance().RunPeriodicalTask("", taskFunc, 2000, 0);
76     ASSERT_EQ(taskResult, 0);
77 }
78 
79 /**
80  * @tc.name: WatchdogInner fetch next task;
81  * @tc.desc: Verify whether the task acquisition failed
82  * @tc.type: FUNC
83  */
84 HWTEST_F(WatchdogInnerTest, WatchdogInnerTest_FetchNextTask_001, TestSize.Level1)
85 {
86     uint64_t now = GetCurrentTickMillseconds();
87     int taskResult = 0;
__anon71a615610302() 88     auto taskFunc = [&taskResult]() { taskResult = 1; };
89     const std::string name = "task1";
90     uint64_t delay = 0;
91     uint64_t interval = 0;
92     bool isOneshot = true;
93     WatchdogTask task(name, taskFunc, delay, interval, isOneshot);
94     int id = WatchdogInner::GetInstance().InsertWatchdogTaskLocked(name, WatchdogTask(name, taskFunc,
95         delay, interval, isOneshot));
96     ASSERT_GT(id, 0);
97     WatchdogInner::GetInstance().isNeedStop_.store(true);
98     ASSERT_EQ(WatchdogInner::GetInstance().FetchNextTask(now, task), 60000);
99     WatchdogInner::GetInstance().isNeedStop_.store(false);
100     WatchdogTask task1;
101     ASSERT_EQ(WatchdogInner::GetInstance().FetchNextTask(now, task1), 60000);
102 }
103 
104 /**
105  * @tc.name: WatchdogInner fetch next task;
106  * @tc.desc: Verify whether the task acquisition successfully
107  * @tc.type: FUNC
108  */
109 HWTEST_F(WatchdogInnerTest, WatchdogInnerTest_FetchNextTask_002, TestSize.Level1)
110 {
111     uint64_t now = GetCurrentTickMillseconds() + 6500;
112     int taskResult = 0;
__anon71a615610402() 113     auto taskFunc = [&taskResult]() { taskResult = 1; };
114     const std::string name = "IPC_FULL";
115     uint64_t delay = 0;
116     uint64_t interval = 0;
117     bool isOneshot = true;
118     WatchdogTask task(name, taskFunc, delay, interval, isOneshot);
119     int id = WatchdogInner::GetInstance().InsertWatchdogTaskLocked(name, WatchdogTask(name, taskFunc,
120         delay, interval, isOneshot));
121     ASSERT_GT(id, 0);
122     ASSERT_EQ(WatchdogInner::GetInstance().FetchNextTask(now, task), 0);
123 }
124 
125 /**
126  * @tc.name: WatchdogInner FfrtCallback;
127  * @tc.desc: Verify FfrtCallback
128  * @tc.type: FUNC
129  */
130 HWTEST_F(WatchdogInnerTest, WatchdogInnerTest_FfrtCallback_001, TestSize.Level1)
131 {
132     uint64_t taskId = 1;
133     const char *taskInfo = "task";
134     uint32_t delayedTaskCount = 0;
135     WatchdogInner::GetInstance().FfrtCallback(taskId, taskInfo, delayedTaskCount);
136 }
137 
138 /**
139  * @tc.name: WatchdogInner kick watchdog;
140  * @tc.desc: Verify whether the kick watchdog failed
141  * @tc.type: FUNC
142  */
143 HWTEST_F(WatchdogInnerTest, WatchdogInnerTest_KickWatchdog_001, TestSize.Level1)
144 {
145     bool ret = WatchdogInner::GetInstance().KickWatchdog();
146     ASSERT_EQ(ret, true);
147 }
148 
149 /**
150  * @tc.name: WatchdogInner IpcCheck;
151  * @tc.desc: Verify IpcCheck
152  * @tc.type: FUNC
153  */
154 HWTEST_F(WatchdogInnerTest, WatchdogInnerTest_IpcCheck_001, TestSize.Level1)
155 {
156     WatchdogInner::GetInstance().IpcCheck();
157 }
158 
159 /**
160  * @tc.name: WatchdogInner is exceedMaxTaskLocked;
161  * @tc.desc: Verify whether checkerQueue_ is over MAX_WATCH_NUM;
162  * @tc.type: FUNC
163  */
164 HWTEST_F(WatchdogInnerTest, WatchdogInnerTest_IsExceedMaxTaskLocked_001, TestSize.Level1)
165 {
166     bool ret = WatchdogInner::GetInstance().IsExceedMaxTaskLocked();
167     ASSERT_EQ(ret, false);
168 }
169 } // namespace HiviewDFX
170 } // namespace OHOS