HiCollieCppTest.cpp 9.6 KB
Newer Older
S
shuxiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <string>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <regex>
#include <gtest/gtest.h>

#include <unistd.h>
#include <sys/wait.h>

#include "xcollie/xcollie.h"
#include "xcollie/xcollie_checker.h"
#include "xcollie/xcollie_define.h"
#include "ctime"
#include "file_utils.h"

using namespace testing;
using namespace testing::ext;
using namespace std;
using namespace OHOS::HiviewDFX;

namespace {
int g_type = 1;
int g_blockTime;
int g_waitTime;
int g_setTimeout;
int g_updateTime = -1;
int g_cancelTimeout = -1;
}
class HiCollieCppTest : public testing::Test {
public:
    int callbackCnt_;
    static void SetUpTestCase();
    static void TearDownTestCase();
    void SetUp();
    void TearDown();
    string ReadFileList(string basePath, string param);
    bool ReadHilogcatredirectFile(string path, string context);
    void XcollieTestInstance(string testItem);
    void DoXCollieTest();
    void TimeCallback(void *data);
    void SetCallbackCnt(int cnt);
    HiCollieCppTest();
    ~HiCollieCppTest();
private:
};
HiCollieCppTest::HiCollieCppTest():callbackCnt_(0)
{
}
HiCollieCppTest::~HiCollieCppTest()
{
}
void HiCollieCppTest::SetUp()
{
    std::cout << "SetUp" << std::endl;
}
void HiCollieCppTest::TearDown()
{
    std::cout << "TearDown" << std::endl;
    g_updateTime = -1;
    g_cancelTimeout = -1;
}
void HiCollieCppTest::SetUpTestCase()
{
    std::cout << "SetUpTestCase" << std::endl;
}
void HiCollieCppTest::TearDownTestCase()
{
    std::cout << "TearDownTestCase" << std::endl;
}
void HiCollieCppTest::TimeCallback(void *data)
{
    callbackCnt_++;
}
void HiCollieCppTest::SetCallbackCnt(int cnt)
{
    callbackCnt_ = cnt;
}
class XCollieCheckerAssist : public XCollieChecker {
    using XCollieChecker::XCollieChecker;
    virtual void CheckLock()
    {
    // sleep 35 seconds
        sleep(35);
    }
};
void HiCollieCppTest::DoXCollieTest()
{
    pid_t pid;
    int status;
    // Fork a child process for leak injection
    pid = fork();
    if (pid == -1) {
        cout<<"fail to fork!"<<endl;
        exit(1);
    } else if (pid == 0) {
        XcollieTestInstance("WATCHDOG");
        sleep(g_blockTime);
        exit(0);
    } else {
        wait(&status);
    }
}

void HiCollieCppTest::XcollieTestInstance(string testItem)
{
    cout<<"XcollieTestInstance start"<<endl;
    if (testItem == "TIMEOUT") {
        int id = XCollie::GetInstance().SetTimer("TimeoutTimer", g_setTimeout, nullptr, nullptr, g_type);
        if (g_updateTime != -1) {
            bool ret = XCollie::GetInstance().UpdateTimer(id, g_updateTime);
            if (ret) {
                cout<<"Update timer succeed,reset as "<<g_updateTime<<endl;
            } else {
                cout<<"Update timer failed!"<<endl;
            }
        }
        if (g_cancelTimeout != -1) {
            XCollie::GetInstance().CancelTimer(id);
        }
        sleep(g_blockTime);
        XCollie::GetInstance().CancelTimer(id);
    } else if (testItem == "WATCHDOG") {
        const OHOS::sptr<XCollieChecker> checker = new XCollieCheckerAssist("checker");
        XCollie::GetInstance().RegisterXCollieChecker(checker, g_type);
        sleep(g_waitTime);
    }
}
/**
 * @tc.name Verify that when the native layer is blocked by the main thread of
 *   the monitored service, the service restarts after 60 seconds
 * @tc.number DFX_DFR_Xcollie_Watchdog_0001
 * @tc.desc Verify that the native layer is blocked by the main thread of the monitored service
*/
HWTEST_F(HiCollieCppTest, Xcollie_watchdog_test, Function|MediumTest|Level1) {
    GTEST_LOG_(INFO) << "Xcollie_watchdog_test start" << endl;
    g_type = XCOLLIE_THREAD;
    g_blockTime = 61;
    g_waitTime = 20;
    DoXCollieTest();
    string cmd = "";
    bool result = false;
    sleep(90);
    std::vector<std::string> cmdret;
    cmd = "ps -A |grep HiCollieCppTest";
    unsigned long cmdretlen;
    cmdretlen = ExecCmdWithRet(cmd, cmdret);
    if (cmdretlen == 1) {
        result = true;
    } else {
        GTEST_LOG_(INFO) << "failed to get xcollie log." << endl;
    }
    ASSERT_TRUE(result);
    GTEST_LOG_(INFO) << "Xcollie_watchdog_test end" << endl;
}
/**
 * @tc.name Verify that the native layer service deadlock and
 *   main thread blocking are monitored at the same time
 * @tc.number DFX_DFR_Xcollie_Watchdog_0002
 * @tc.desc Verify that the native layer service deadlock and main thread blocking
*/
HWTEST_F(HiCollieCppTest, Xcollie_watchdog_test1, Function|MediumTest|Level1) {
    GTEST_LOG_(INFO) << "Xcollie_watchdog_test1 start" << endl;
    string cmd = "";
    bool result = false;
    g_type = XCOLLIE_LOCK|XCOLLIE_THREAD;
    g_blockTime = 61;
    g_waitTime = 20;
    DoXCollieTest();
    sleep(90);
    std::vector<std::string> cmdret;
    cmd = "ps -A |grep HiCollieCppTest";
    unsigned long cmdretlen;
    cmdretlen = ExecCmdWithRet(cmd, cmdret);
    if (cmdretlen == 1) {
        result = true;
    } else {
        GTEST_LOG_(INFO) << "failed to get xcollie log." << endl;
    }
    ASSERT_TRUE(result);
    GTEST_LOG_(INFO) << "Xcollie_watchdog_test1 end" << endl;
}
/**
 * @tc.name When a deadlock occurs in the monitored service of the native layer,
 *   the service restarts after 60 seconds
 * @tc.number DFX_DFR_Xcollie_Watchdog_0003
 * @tc.desc Verify that deadlock occurs in the monitored service of the native layer
*/
HWTEST_F(HiCollieCppTest, Xcollie_watchdog_test2, Function|MediumTest|Level1) {
    GTEST_LOG_(INFO) << "Xcollie_watchdog_test2 start" << endl;
    g_type = XCOLLIE_LOCK;
    g_blockTime = 30;
    g_waitTime = 75;
    DoXCollieTest();
    string cmd = "";
    bool result = false;
    sleep(110);
    std::vector<std::string> cmdret;
    cmd = "ps -A |grep HiCollieCppTest";
    unsigned long cmdretlen;
    cmdretlen = ExecCmdWithRet(cmd, cmdret);
    if (cmdretlen == 1) {
        result = true;
    } else {
        GTEST_LOG_(INFO) << "failed to get xcollie log." << endl;
    }
    ASSERT_TRUE(result);
    GTEST_LOG_(INFO) << "Xcollie_watchdog_test2 end" << endl;
}
/**
 * @tc.name verification of callback funcitons
 * @tc.number DFX_DFR_Xcollie_Timeout_0001
 * @tc.desc callback funcitons
 */
HWTEST_F(HiCollieCppTest, Xcollie_Timeout_test, Function|MediumTest|Level1) {
    GTEST_LOG_(INFO) << "Xcollie_Timeout_test start" << endl;
    g_type = XCOLLIE_FLAG_NOOP;
    SetCallbackCnt(0);
    auto func = [this](void *data) {
        this->TimeCallback(data);
    };
    int last = XCollie::GetInstance().SetTimer("TimeoutTimer", 61, func, nullptr, g_type);
    ASSERT_TRUE(last != INVALID_ID);
    GTEST_LOG_(INFO) << "Xcollie_Timeout_test end" << endl;
}
/**
 * @tc.name UpdateTimer interface test
 * @tc.number DFX_DFR_Xcollie_interface_0001
 * @tc.desc interface test
 */
HWTEST_F(HiCollieCppTest, Xcollie_interface_test, Function|MediumTest|Level1)
{
    int id = XCollie::GetInstance().SetTimer("TimeoutTimerTest", 1, nullptr, nullptr, XCOLLIE_FLAG_NOOP);
    ASSERT_NE(id, INVALID_ID);
    bool result = XCollie::GetInstance().UpdateTimer(id, 1);
    ASSERT_TRUE(result);
}
/**
 * @tc.name UpdateTimer interface test, id is invalid
 * @tc.number DFX_DFR_Xcollie_interface_0002
 * @tc.desc interface test
 */
HWTEST_F(HiCollieCppTest, Xcollie_interface_test1, Function|MediumTest|Level1)
{
    int id = -1;
    int timeout = 100;
    bool result = XCollie::GetInstance().UpdateTimer(id, timeout);
    ASSERT_FALSE(result);
}
/**
 * @tc.name UpdateTimer interface test, timeout is 0
 * @tc.number DFX_DFR_Xcollie_interface_0003
 * @tc.desc interface test
 */
HWTEST_F(HiCollieCppTest, Xcollie_interface_test2, Function|MediumTest|Level1)
{
    bool result = XCollie::GetInstance().UpdateTimer(10, 0);
    ASSERT_FALSE(result);
}
/**
 * @tc.name SetTimer interface test, timeout is 0
 * @tc.number DFX_DFR_Xcollie_interface_0004
 * @tc.desc interface test
 */
HWTEST_F(HiCollieCppTest, Xcollie_interface_test3, Function|MediumTest|Level1)
{
    int id = XCollie::GetInstance().SetTimer("Timer", 0, nullptr, nullptr, XCOLLIE_FLAG_NOOP);
    ASSERT_EQ(id, INVALID_ID);
}
/**
 * @tc.name SetTimer interface test, timeout is negative.
 * @tc.number DFX_DFR_Xcollie_interface_0005
 * @tc.desc interface test
 */
HWTEST_F(HiCollieCppTest, Xcollie_interface_test4, Function|MediumTest|Level1)
{
    int id = XCollie::GetInstance().SetTimer("Timer", -2, nullptr, nullptr, XCOLLIE_FLAG_NOOP);
    ASSERT_TRUE(id != INVALID_ID);
}
/**
 * @tc.name SetTimer interface test, service name is very long.
 * @tc.number DFX_DFR_Xcollie_interface_0006
 * @tc.desc interface test
 */
HWTEST_F(HiCollieCppTest, Xcollie_interface_test5, Function|MediumTest|Level1)
{
    std::string name = "abcd";
    for (int i = 0; i < 1000; i++) {
        name += "abcd";
    }
    int id = XCollie::GetInstance().SetTimer(name, 5, nullptr, nullptr, XCOLLIE_FLAG_NOOP);
    ASSERT_TRUE(id != INVALID_ID);
}
/**
 * @tc.name SetTimer interface test, when type is invalid.
 * @tc.number DFX_DFR_Xcollie_interface_0007
 * @tc.desc interface test
 */
HWTEST_F(HiCollieCppTest, Xcollie_interface_test6, Function|MediumTest|Level1)
{
    int id = XCollie::GetInstance().SetTimer("Timer", 5, nullptr, nullptr, XCOLLIE_FLAG_NOOP + 100);
    ASSERT_TRUE(id != INVALID_ID);
}