1 /* 2 * Copyright (c) 2025 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 <gtest/hwext/gtest-multithread.h> 18 19 #include <string> 20 #include <thread> 21 #include <unistd.h> 22 23 #include "dfx_define.h" 24 #include "dfx_test_util.h" 25 #include "dfx_dump_catcher_slow_policy.h" 26 27 using namespace testing; 28 using namespace testing::ext; 29 using namespace testing::mt; 30 31 namespace OHOS { 32 namespace HiviewDFX { 33 class DfxDumpCatcherSlowPolicyTest : public testing::Test { 34 public: SetUpTestCase()35 static void SetUpTestCase() {}; TearDownTestCase()36 static void TearDownTestCase() {}; SetUp()37 void SetUp() {}; TearDown()38 void TearDown() {}; 39 }; 40 41 /** 42 * @tc.name: DfxDumpCatcherSlowPolicyTest001 43 * @tc.desc: test DfxDumpCatcherSlowPolicy Set And Get Same Pid 44 * @tc.type: FUNC 45 */ 46 HWTEST_F(DfxDumpCatcherSlowPolicyTest, DfxDumpCatcherSlowPolicyTest001, TestSize.Level2) 47 { 48 GTEST_LOG_(INFO) << "DfxDumpCatcherSlowPolicyTest001: start."; 49 const int pid = 12345; 50 const int waitSec = 5; // 5 : 5s for slow period 51 DfxDumpCatcherSlowPolicy::GetInstance().SetDumpCatcherSlowStat(pid); 52 sleep(1); // 1 : 1s 53 ASSERT_TRUE(DfxDumpCatcherSlowPolicy::GetInstance().IsDumpCatcherInSlowPeriod(pid)); 54 sleep(waitSec); 55 ASSERT_FALSE(DfxDumpCatcherSlowPolicy::GetInstance().IsDumpCatcherInSlowPeriod(pid)); 56 GTEST_LOG_(INFO) << "DfxDumpCatcherSlowPolicyTest001: end."; 57 } 58 59 /** 60 * @tc.name: DfxDumpCatcherSlowPolicyTest002 61 * @tc.desc: test DfxDumpCatcherSlowPolicy Set And Get diff Pid 62 * @tc.type: FUNC 63 */ 64 HWTEST_F(DfxDumpCatcherSlowPolicyTest, DfxDumpCatcherSlowPolicyTest002, TestSize.Level2) 65 { 66 GTEST_LOG_(INFO) << "DfxDumpCatcherSlowPolicyTest002: start."; 67 const int pid = 12345; 68 const int otherPid = 54321; 69 const int waitSec = 5; // 5 : 5s for slow period 70 DfxDumpCatcherSlowPolicy::GetInstance().SetDumpCatcherSlowStat(pid); 71 sleep(1); // 1 : 1s 72 ASSERT_FALSE(DfxDumpCatcherSlowPolicy::GetInstance().IsDumpCatcherInSlowPeriod(otherPid)); 73 sleep(waitSec); 74 ASSERT_FALSE(DfxDumpCatcherSlowPolicy::GetInstance().IsDumpCatcherInSlowPeriod(pid)); 75 GTEST_LOG_(INFO) << "DfxDumpCatcherSlowPolicyTest002: end."; 76 } 77 78 /** 79 * @tc.name: DfxDumpCatcherSlowPolicyTest003 80 * @tc.desc: test DfxDumpCatcherSlowPolicy Set And Get muti Pid 81 * @tc.type: FUNC 82 */ 83 HWTEST_F(DfxDumpCatcherSlowPolicyTest, DfxDumpCatcherSlowPolicyTest003, TestSize.Level2) 84 { 85 GTEST_LOG_(INFO) << "DfxDumpCatcherSlowPolicyTest003: start."; 86 const int waitSec = 5; // 5 : 5s for slow period 87 const int pidNum = 50; // 50 : save 50 record 88 89 for (int pid = 0; pid < pidNum; pid++) { 90 DfxDumpCatcherSlowPolicy::GetInstance().SetDumpCatcherSlowStat(pid); 91 usleep(100000); // 100000 : 100ms 92 ASSERT_TRUE(DfxDumpCatcherSlowPolicy::GetInstance().IsDumpCatcherInSlowPeriod(pid)); 93 } 94 95 sleep(waitSec); 96 for (int pid = 0; pid < pidNum; pid++) { 97 ASSERT_FALSE(DfxDumpCatcherSlowPolicy::GetInstance().IsDumpCatcherInSlowPeriod(pid)); 98 } 99 100 for (int pid = 0; pid < pidNum + 1; pid++) { 101 DfxDumpCatcherSlowPolicy::GetInstance().SetDumpCatcherSlowStat(pid); 102 usleep(1000); // 1000 : 1ms 103 } 104 ASSERT_FALSE(DfxDumpCatcherSlowPolicy::GetInstance().IsDumpCatcherInSlowPeriod(1)); 105 GTEST_LOG_(INFO) << "DfxDumpCatcherSlowPolicyTest003: end."; 106 } 107 } // namespace HiviewDFX 108 } // namepsace OHOS 109