• 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 -PKG ohos.samples.ecg -threads";
56     std::string result = "";
57     bool flag = false;
58     bool ret = SPUtils::LoadCmd(cmd, result);
59     std::string::size_type strOne = result.find("command exec finished!");
60     if ((strOne != result.npos)) {
61         flag = true;
62     }
63     EXPECT_EQ(ret, true);
64     EXPECT_TRUE(flag);
65 }
66 
67 /**
68  * @tc.name: ThreadsTestCase03
69  * @tc.desc: Test Threads by pid not exit
70  * @tc.type: FUNC
71  */
72 HWTEST_F(SPdaemonThreadsTest, ThreadsTestCase03, TestSize.Level1)
73 {
74     std::string cmd = "SP_daemon -N 1 -threads -PID 88888888"; // 88888888 is not exit
75     std::string result = "";
76     bool ret = SPUtils::LoadCmd(cmd, result);
77     EXPECT_EQ(ret, true);
78     ret = VerifResult(result);
79     EXPECT_EQ(ret, false);
80 }
81 
82 /**
83  * @tc.name: ThreadsTestCase04
84  * @tc.desc: Test Threads
85  * @tc.type: FUNC
86  */
87 HWTEST_F(SPdaemonThreadsTest, ThreadsTestCase04, TestSize.Level1)
88 {
89     Threads &ths = Threads::GetInstance();
90     std::string packName = "init";
91     ths.SetPackageName(packName);
92     ths.SetProcessId("1");
93     std::map<std::string, std::string> thsItemData = ths.ItemData();
94     std::string threadsNum = thsItemData["threadsNum"];
95     EXPECT_EQ(threadsNum.empty(), false);
96 }
97 } // namespace OHOS
98 } // namespace SmartPerf