• 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 <fstream>
20 #include <fcntl.h>
21 #include <sys/prctl.h>
22 #include <unistd.h>
23 #include <dlfcn.h>
24 #include <chrono>
25 #include <cstdio>
26 
27 #define private public
28 #define protected public
29 #include "watchdog_inner.h"
30 #undef private
31 #undef protected
32 
33 #include "xcollie_define.h"
34 #include "xcollie_utils.h"
35 #include "directory_ex.h"
36 #include "file_ex.h"
37 #include "parameters.h"
38 #include "watchdog_inner_util_test.h"
39 
40 using namespace testing::ext;
41 using namespace OHOS::AppExecFwk;
42 namespace OHOS {
43 namespace HiviewDFX {
44 
45 class WatchdogInnerTaskTest : public testing::Test {
46 public:
WatchdogInnerTaskTest()47     WatchdogInnerTaskTest()
48     {}
~WatchdogInnerTaskTest()49     ~WatchdogInnerTaskTest()
50     {}
51     static void SetUpTestCase(void);
52     static void TearDownTestCase(void);
53     void SetUp();
54     void TearDown();
55 };
56 
SetUpTestCase(void)57 void WatchdogInnerTaskTest::SetUpTestCase(void)
58 {
59 }
60 
TearDownTestCase(void)61 void WatchdogInnerTaskTest::TearDownTestCase(void)
62 {
63 }
64 
SetUp(void)65 void WatchdogInnerTaskTest::SetUp(void)
66 {
67     InitSeLinuxEnabled();
68 }
69 
TearDown(void)70 void WatchdogInnerTaskTest::TearDown(void)
71 {
72     CancelSeLinuxEnabled();
73 }
74 
InitBeginFuncTest(const char * name)75 static void InitBeginFuncTest(const char* name)
76 {
77     std::string nameStr(name);
78 }
79 
InitEndFuncTest(const char * name)80 static void InitEndFuncTest(const char* name)
81 {
82     std::string nameStr(name);
83 }
84 
85 /**
86  * @tc.name: WatchdogInner thread RemoveInnerTask test
87  * @tc.desc: add test case
88  * @tc.type: FUNC
89  */
90 HWTEST_F(WatchdogInnerTaskTest, WatchdogInnerTaskTest_TaskRemoveInnerTask_001, TestSize.Level1)
91 {
92     WatchdogInner::GetInstance().RemoveInnerTask("WatchdogInnerTaskTest_RemoveInnerTask_001");
93     ASSERT_EQ(WatchdogInner::GetInstance().checkerQueue_.size(), 0);
94 }
95 
96 /**
97  * @tc.name: WatchdogInner thread run a oneshot task
98  * @tc.desc: Verify whether the task has been executed failed
99  * @tc.type: FUNC
100  */
101 HWTEST_F(WatchdogInnerTaskTest, WatchdogInnerTaskTest_RunOneShotTask_001, TestSize.Level1)
102 {
103     int taskResult = 0;
__anon401660f40102() 104     auto taskFunc = [&taskResult]() { taskResult = 1; };
105     WatchdogInner::GetInstance().RunOneShotTask("", taskFunc, 0);
106     ASSERT_EQ(taskResult, 0);
107 }
108 
109 /**
110  * @tc.name: WatchdogInner thread run a periodical task
111  * @tc.desc: Verify whether the task has been executed failed
112  * @tc.type: FUNC
113  */
114 HWTEST_F(WatchdogInnerTaskTest, WatchdogInnerTaskTest_RunPeriodicalTask_001, TestSize.Level1)
115 {
116     int taskResult = 0;
__anon401660f40202() 117     auto taskFunc = [&taskResult]() { taskResult = 1; };
118     std::string name = "RunPeriodicalTask_001";
119     WatchdogInner::GetInstance().RunPeriodicalTask(name, taskFunc, 2000, 0);
120     ASSERT_TRUE(WatchdogInner::GetInstance().checkerQueue_.size() > 0);
121     WatchdogInner::GetInstance().TriggerTimerCountTask(name, false, "test");
122     WatchdogInner::GetInstance().TriggerTimerCountTask(name, true, "test");
123     ASSERT_EQ(taskResult, 0);
124     size_t beforeSize = WatchdogInner::GetInstance().taskNameSet_.size();
125     WatchdogInner::GetInstance().RemoveInnerTask(name);
126     size_t afterSize = WatchdogInner::GetInstance().taskNameSet_.size();
127     ASSERT_EQ(beforeSize, (afterSize + 1));
128     WatchdogInner::GetInstance().RunPeriodicalTask("", taskFunc, 2000, 0);
129     WatchdogInner::GetInstance().SetTimerCountTask("", 1, 1);
130     WatchdogInner::GetInstance().RemoveInnerTask("");
131 }
132 
133 /**
134  * @tc.name: WatchdogInner fetch next task;
135  * @tc.desc: Verify whether the task acquisition failed
136  * @tc.type: FUNC
137  */
138 HWTEST_F(WatchdogInnerTaskTest, WatchdogInnerTaskTest_FetchNextTask_001, TestSize.Level1)
139 {
140     uint64_t now = GetCurrentTickMillseconds();
141     int taskResult = 0;
__anon401660f40302() 142     auto taskFunc = [&taskResult]() { taskResult = 1; };
143     const std::string name = "task1";
144     uint64_t delay = 0;
145     uint64_t interval = 0;
146     bool isOneshot = true;
147     WatchdogTask task(name, taskFunc, delay, interval, isOneshot);
148     int id = WatchdogInner::GetInstance().InsertWatchdogTaskLocked(name, WatchdogTask(name, taskFunc,
149         delay, interval, isOneshot));
150     ASSERT_GT(id, 0);
151     WatchdogInner::GetInstance().isNeedStop_.store(true);
152     ASSERT_EQ(WatchdogInner::GetInstance().FetchNextTask(now, task), 60000);
153     WatchdogInner::GetInstance().isNeedStop_.store(false);
154     WatchdogTask task1;
155     ASSERT_EQ(WatchdogInner::GetInstance().FetchNextTask(now, task1), 60000);
156     WatchdogTask task2("", taskFunc, delay, interval, isOneshot);
157     ASSERT_EQ(WatchdogInner::GetInstance().FetchNextTask(now, task1), 60000);
158 }
159 } // namespace HiviewDFX
160 } // namespace OHOS
161