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 
18 #include <string>
19 #include "dfx_util.h"
20 #include "fault_logger_pipe.h"
21 
22 using namespace OHOS::HiviewDFX;
23 using namespace testing::ext;
24 using namespace std;
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 class FaultLoggerPipeTest : public testing::Test {
29 public:
SetUpTestCase(void)30     static void SetUpTestCase(void) {}
TearDownTestCase(void)31     static void TearDownTestCase(void) {}
SetUp()32     void SetUp() {}
TearDown()33     void TearDown() {}
34 };
35 } // namespace HiviewDFX
36 } // namespace OHOS
37 
38 namespace {
39 /**
40  * @tc.name: FaultLoggerPipeTest001
41  * @tc.desc: test FaultLoggerPipeMap Check Set Get func
42  * @tc.type: FUNC
43  */
44 HWTEST_F (FaultLoggerPipeTest, FaultLoggerPipeTest001, TestSize.Level2)
45 {
46     GTEST_LOG_(INFO) << "FaultLoggerPipeTest001: start.";
47     std::shared_ptr<FaultLoggerPipeMap> ptr = std::make_shared<FaultLoggerPipeMap>();
48     uint64_t time = OHOS::HiviewDFX::GetTimeMilliSeconds();
49     int pid = 100;
50     bool check = ptr->Check(pid, time);
51     EXPECT_EQ(check, false) << "FaultLoggerPipeTest001 Check failed";
52 
53     ptr->Set(pid, time);
54     auto ret = ptr->Get(pid);
55     EXPECT_EQ(true, ret != nullptr) << "FaultLoggerPipeTest001 Get failed";
56 
57     sleep(11); // sleep 11 seconds
58     time = OHOS::HiviewDFX::GetTimeMilliSeconds();
59     check = ptr->Check(pid, time);
60     EXPECT_EQ(check, false) << "FaultLoggerPipeTest001 Check failed";
61 
62     GTEST_LOG_(INFO) << "FaultLoggerPipeTest001: end.";
63 }
64 
65 /**
66  * @tc.name: FaultLoggerPipeTest002
67  * @tc.desc: test FaultLoggerPipeMap Del func
68  * @tc.type: FUNC
69  */
70 HWTEST_F (FaultLoggerPipeTest, FaultLoggerPipeTest002, TestSize.Level2)
71 {
72     GTEST_LOG_(INFO) << "FaultLoggerPipeTest002: start.";
73     std::shared_ptr<FaultLoggerPipeMap> ptr = std::make_shared<FaultLoggerPipeMap>();
74     uint64_t time = OHOS::HiviewDFX::GetTimeMilliSeconds();
75     int pid = 100;
76     ptr->Set(pid, time);
77     auto ret = ptr->Get(pid);
78     EXPECT_EQ(true, ret != nullptr) << "FaultLoggerPipeTest002 Get failed";
79     ptr->Del(pid);
80     ret = ptr->Get(pid);
81     EXPECT_EQ(true, ret == nullptr) << "FaultLoggerPipeTest002 Del failed";
82     GTEST_LOG_(INFO) << "FaultLoggerPipeTest002: end.";
83 }
84 }