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;
__anondf92b98f0102() 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;
__anondf92b98f0202() 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;
__anondf92b98f0302() 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 } // namespace HiviewDFX
102 } // namespace OHOS
103