• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 <regex>
18 #include <string>
19 #include "sp_utils.h"
20 #include "Threads.h"
21 using namespace testing::ext;
22 using namespace std;
23 
24 namespace OHOS {
25 namespace SmartPerf {
26 class SPdaemonThreadsTest : public testing::Test {
27 public:
SetUpTestCase()28     static void SetUpTestCase() {}
TearDownTestCase()29     static void TearDownTestCase() {}
30 
SetUp()31     void SetUp() {}
TearDown()32     void TearDown() {}
33 };
34 
VerifResult(const std::string & input)35 static bool VerifResult(const std::string &input)
36 {
37     std::regex pattern(R"(threadsNum=([\d:|]+))");
38     std::smatch match;
39     if (std::regex_search(input, match, pattern)) {
40         if (match.size() > 1) {
41             std::cout << match[1].str() << std::endl;
42             return !match[1].str().empty();
43         }
44     }
45 
46     return false;
47 }
48 /**
49  * @tc.name: ThreadsTestCase01
50  * @tc.desc: Test Threads by packagename
51  * @tc.type: FUNC
52  */
53 HWTEST_F(SPdaemonThreadsTest, ThreadsTestCase01, TestSize.Level1)
54 {
55     std::string cmd = "SP_daemon -N 1 -threads -PKG sh";
56     std::string result = "";
57     bool ret = SPUtils::LoadCmd(cmd, result);
58     EXPECT_EQ(ret, true);
59     ret = VerifResult(result);
60     EXPECT_EQ(ret, true);
61 }
62 
63 /**
64  * @tc.name: ThreadsTestCase02
65  * @tc.desc: Test Threads by pid
66  * @tc.type: FUNC
67  */
68 HWTEST_F(SPdaemonThreadsTest, ThreadsTestCase02, TestSize.Level1)
69 {
70     std::string cmd = "SP_daemon -N 1 -threads -PID 1";
71     std::string result = "";
72     bool ret = SPUtils::LoadCmd(cmd, result);
73     EXPECT_EQ(ret, true);
74     ret = VerifResult(result);
75     EXPECT_EQ(ret, true);
76 }
77 
78 /**
79  * @tc.name: ThreadsTestCase03
80  * @tc.desc: Test Threads by pid not exit
81  * @tc.type: FUNC
82  */
83 HWTEST_F(SPdaemonThreadsTest, ThreadsTestCase03, TestSize.Level1)
84 {
85     std::string cmd = "SP_daemon -N 1 -threads -PID 88888888"; // 88888888 is not exit
86     std::string result = "";
87     bool ret = SPUtils::LoadCmd(cmd, result);
88     EXPECT_EQ(ret, true);
89     ret = VerifResult(result);
90     EXPECT_EQ(ret, false);
91 }
92 
93 /**
94  * @tc.name: ThreadsTestCase04
95  * @tc.desc: Test Threads
96  * @tc.type: FUNC
97  */
98 HWTEST_F(SPdaemonThreadsTest, ThreadsTestCase04, TestSize.Level1)
99 {
100     Threads &ths = Threads::GetInstance();
101     std::string packName = "init";
102     ths.SetPackageName(packName);
103     ths.SetProcessId("1");
104     std::map<std::string, std::string> thsItemData = ths.ItemData();
105     std::string threadsNum = thsItemData["threadsNum"];
106     EXPECT_EQ(threadsNum.empty(), false);
107 }
108 } // namespace OHOS
109 } // namespace SmartPerf