提交 a89a4f3a 编写于 作者: Z zhuqingxi

add hitrace and hicollie testcases

Signed-off-by: Nzhuqingxi <zhuqingxi@huawei.com>
上级 9c89e346
......@@ -17,8 +17,11 @@ group("hiviewdfxtestacts") {
deps = [
"faultloggertest/faultloggercpptest:faultloggertest",
"hiappeventtest/hiappeventjstest:hiappeventjstest",
"hicollietest/hicolliecpptest:HiCollieCppTest",
"hilogtest/libhilogtest:libhilogtestacts",
"hisyseventtest/hisyseventcpptest:HiSysEventCPPTest",
"hitracetest/hitracecpptest:HitraceCPPtest",
"hitracetest/hitracectest:HitraceCtest",
]
}
}
# 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.
import("//test/xts/tools/build/suite.gni")
module_output_path = "hits/HiCollieCppTest"
###############################################################################
config("xcollie_config") {
visibility = [ ":*" ]
include_dirs = [
"../../utils/native",
"//utils/native/base/include/",
"//base/hiviewdfx/hilog/interfaces/native/innerkits/include",
"//base/hiviewdfx/hicollie/interfaces/native/innerkits/include",
]
}
ohos_moduletest_suite("HiCollieCppTest") {
module_out_path = module_output_path
sources = [ "HiCollieCppTest.cpp" ]
deps = [
"../../utils/native:utilskit",
"//base/hiviewdfx/hicollie/interfaces/native/innerkits:libhicollie",
"//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog",
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
]
configs = [ ":xcollie_config" ]
}
/*
* 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);
}
\ No newline at end of file
{
"kits": [
{
"push": [
"HiCollieCppTest->/data/local/tmp/HiCollieCppTest"
],
"type": "PushKit",
"post-push": [
"chmod -R 777 /data/local/tmp/*"
]
}
],
"driver": {
"native-test-timeout": "120000",
"type": "CppTest",
"module-name": "HiCollieCppTest",
"runtime-hint": "1s",
"native-test-device-path": "/data/local/tmp"
},
"description": "Configuration for HiCollieCppTest Tests"
}
\ No newline at end of file
# 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.
import("//test/xts/tools/build/suite.gni")
module_output_path = "hits/HitraceCPPtest"
###############################################################################
config("hitrace_config") {
visibility = [ ":*" ]
include_dirs = [
"../../utils/native",
"//utils/native/base/include/",
"//base/hiviewdfx/hilog/interfaces/native/innerkits/include",
"//base/hiviewdfx/hitrace/interfaces/native/innerkits/include",
"//base/hiviewdfx/hisysevent/interfaces/native/innerkits/include",
]
}
ohos_moduletest_suite("HitraceCPPtest") {
module_out_path = module_output_path
sources = [ "HitraceCPPtest.cpp" ]
deps = [
"../../utils/native:utilskit",
"//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog",
"//base/hiviewdfx/hisysevent/interfaces/native/innerkits:libhisysevent",
"//base/hiviewdfx/hitrace/interfaces/native/innerkits:libhitrace",
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
]
configs = [ ":hitrace_config" ]
}
/*
* 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 <cstdarg>
#include <ctime>
#include <gtest/gtest.h>
#include <pthread.h>
#include "hitrace/hitrace.h"
#include "hitrace/hitraceid.h"
#include "hitrace/trace.h"
#include "file_utils.h"
using namespace OHOS;
using namespace HiviewDFX;
using namespace testing;
using namespace testing::ext;
using namespace std;
class HitraceCPPtest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp();
void TearDown();
private:
};
void HitraceCPPtest::SetUp()
{
}
void HitraceCPPtest::TearDown()
{
}
void HitraceCPPtest::SetUpTestCase()
{
}
void HitraceCPPtest::TearDownTestCase()
{
}
/**
* @tc.name HiTrace C++ interface test
* @tc.number DFX_DFT_HiTrace_CPP_0001
* @tc.desc HiTrace C++ interface test
*/
HWTEST_F(HitraceCPPtest, Hitrace_interface, Function|MediumTest|Level1)
{
GTEST_LOG_(INFO) << "Hitrace_interface start" << endl;
HiTraceFlag hitraceflag = HITRACE_FLAG_INCLUDE_ASYNC;
HiTraceId hiTraceId = HiTrace::Begin("test", HITRACE_FLAG_INCLUDE_ASYNC);
HiTrace::SetId(hiTraceId);
hiTraceId = HiTrace::GetId();
ASSERT_TRUE(hiTraceId.IsValid());
hiTraceId.EnableFlag(hitraceflag);
ASSERT_TRUE(hiTraceId.IsFlagEnabled(hitraceflag));
int hitraceflags = HITRACE_FLAG_FAULT_TRIGGER;
hiTraceId.SetFlags(hitraceflags);
hitraceflags = hiTraceId.GetFlags();
ASSERT_TRUE(hitraceflags == HITRACE_FLAG_FAULT_TRIGGER);
uint64_t chainId = 10000;
hiTraceId.SetChainId(chainId);
chainId = hiTraceId.GetChainId();
ASSERT_TRUE(chainId == 10000);
uint64_t spanId = 12345678;
hiTraceId.SetSpanId(spanId);
spanId = hiTraceId.GetSpanId();
ASSERT_TRUE(spanId == 12345678);
HiTraceId childId = HiTrace::CreateSpan();
EXPECT_EQ(1, childId.IsValid());
/* set child id to thread id */
HiTrace::SetId(childId);
/* continue to create child span */
HiTraceId grandChildId = HiTrace::CreateSpan();
EXPECT_EQ(1, grandChildId.IsValid());
EXPECT_EQ(grandChildId.GetFlags(), childId.GetFlags());
EXPECT_EQ(grandChildId.GetChainId(), childId.GetChainId());
EXPECT_EQ(grandChildId.GetParentSpanId(), childId.GetSpanId());
grandChildId.SetChainId(chainId);
chainId = grandChildId.GetChainId();
ASSERT_TRUE(chainId == 10000);
grandChildId.SetSpanId(spanId);
spanId = grandChildId.GetSpanId();
ASSERT_TRUE(spanId == 12345678);
HiTrace::ClearId();
HiTraceId clearId = HiTrace::GetId();
EXPECT_EQ(0, clearId.IsValid());
HiTrace::End(hiTraceId);
GTEST_LOG_(INFO) << "Hitrace_interface end" << endl;
}
/**
* @tc.name HiTrace C++ Begin interface, when flag is invalid.
* @tc.number DFX_DFT_HiTrace_CPP_0002
* @tc.desc HiTrace C++ interface test
*/
HWTEST_F(HitraceCPPtest, Hitrace_interface1, Function|MediumTest|Level1)
{
GTEST_LOG_(INFO) << "Hitrace_interface1 start" << endl;
/* begin with invalid flag */
HiTraceId invalidFlagId = HiTrace::Begin("invalid param", HITRACE_FLAG_MAX+1);
EXPECT_EQ(0, invalidFlagId.IsValid());
HiTrace::End(invalidFlagId);
invalidFlagId = HiTrace::Begin("invalid param", -1);
EXPECT_EQ(0, invalidFlagId.IsValid());
HiTrace::End(invalidFlagId);
GTEST_LOG_(INFO) << "Hitrace_interface1 end" << endl;
}
/**
* @tc.name HiTrace C++ Begin interface, when name is invalid.
* @tc.number DFX_DFT_HiTrace_CPP_0003
* @tc.desc HiTrace C++ interface test
*/
HWTEST_F(HitraceCPPtest, Hitrace_interface2, Function|MediumTest|Level1)
{
GTEST_LOG_(INFO) << "Hitrace_interface2 start" << endl;
/* begin with invalid name */
HiTraceId invalidNameId = HiTrace::Begin("", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, invalidNameId.IsValid());
HiTrace::End(invalidNameId);
GTEST_LOG_(INFO) << "Hitrace_interface2 end" << endl;
}
/**
* @tc.name HiTrace C++ Begin interface, when name is very long.
* @tc.number DFX_DFT_HiTrace_CPP_0004
* @tc.desc HiTrace C++ interface test
*/
HWTEST_F(HitraceCPPtest, Hitrace_interface3, Function|MediumTest|Level1)
{
GTEST_LOG_(INFO) << "Hitrace_interface3 start" << endl;
std::string name = "test";
for (int i = 0; i < 1000; i++) {
name += "test";
}
HiTraceId traceId = HiTrace::Begin(name, HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, traceId.IsValid());
HiTrace::End(traceId);
GTEST_LOG_(INFO) << "Hitrace_interface3 end" << endl;
}
/**
* @tc.name Repeated call Begin interface.
* @tc.number DFX_DFT_HiTrace_CPP_0005
* @tc.desc HiTrace C++ interface test
*/
HWTEST_F(HitraceCPPtest, Hitrace_interface4, Function|MediumTest|Level1)
{
GTEST_LOG_(INFO) << "Hitrace_interface4 start" << endl;
HiTraceId traceId = HiTrace::Begin("test", HITRACE_FLAG_TP_INFO);
HiTraceId traceId1 = HiTrace::Begin("test1", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, traceId.IsValid());
EXPECT_EQ(0, traceId1.IsValid());
HiTrace::End(traceId);
HiTrace::End(traceId1);
GTEST_LOG_(INFO) << "Hitrace_interface4 end" << endl;
}
/**
* @tc.name End invalid traceid
* @tc.number DFX_DFT_HiTrace_CPP_0006
* @tc.desc HiTrace C++ interface test
*/
HWTEST_F(HitraceCPPtest, Hitrace_interface5, Function|MediumTest|Level1)
{
GTEST_LOG_(INFO) << "Hitrace_interface5 start" << endl;
HiTraceId traceId = HiTrace::Begin("test", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, traceId.IsValid());
HiTraceId invalidId;
invalidId.SetChainId(1000);
invalidId.SetSpanId(1000);
HiTrace::End(invalidId);
HiTraceId wrongEndId = HiTrace::GetId();
EXPECT_EQ(1, wrongEndId.IsValid());
ASSERT_TRUE(wrongEndId.GetChainId() == traceId.GetChainId());
EXPECT_EQ(1, wrongEndId.IsFlagEnabled(HITRACE_FLAG_TP_INFO));
HiTrace::End(traceId);
GTEST_LOG_(INFO) << "Hitrace_interface5 end" << endl;
}
/**
* @tc.name End traceId that has ended.
* @tc.number DFX_DFT_HiTrace_CPP_0007
* @tc.desc HiTrace C++ interface test
*/
HWTEST_F(HitraceCPPtest, Hitrace_interface6, Function|MediumTest|Level1)
{
GTEST_LOG_(INFO) << "Hitrace_interface6 start" << endl;
HiTraceId traceId = HiTrace::Begin("invalid param1", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, traceId.IsValid());
HiTrace::End(traceId);
traceId = HiTrace::GetId();
EXPECT_EQ(0, traceId.IsValid());
HiTrace::End(traceId);
traceId = HiTrace::GetId();
EXPECT_EQ(0, traceId.IsValid());
GTEST_LOG_(INFO) << "Hitrace_interface6 end" << endl;
}
\ No newline at end of file
{
"kits": [
{
"push": [
"HitraceCPPtest->/data/local/tmp/HitraceCPPtest"
],
"type": "PushKit",
"post-push": [
"chmod -R 777 /data/local/tmp/*"
]
}
],
"driver": {
"native-test-timeout": "120000",
"type": "CppTest",
"module-name": "HitraceCPPtest",
"runtime-hint": "1s",
"native-test-device-path": "/data/local/tmp"
},
"description": "Configuration for HitraceCPPtest Tests"
}
\ No newline at end of file
# 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.
import("//test/xts/tools/build/suite.gni")
module_output_path = "hits/HitraceCtest"
###############################################################################
config("hitrace_config") {
visibility = [ ":*" ]
include_dirs = [
"../../utils/native",
"//utils/native/base/include/",
"//base/hiviewdfx/hilog/interfaces/native/innerkits/include",
"//base/hiviewdfx/hitrace/interfaces/native/innerkits/include",
]
}
ohos_moduletest_suite("HitraceCtest") {
module_out_path = module_output_path
sources = [ "HitraceCtest.cpp" ]
deps = [
"../../utils/native:utilskit",
"//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog",
"//base/hiviewdfx/hitrace/interfaces/native/innerkits:libhitrace",
"//third_party/googletest:gtest_main",
"//utils/native/base:utils",
]
configs = [ ":hitrace_config" ]
}
/*
* 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 <cstdarg>
#include <gtest/gtest.h>
#include "hitrace/hitracec.h"
#include "file_utils.h"
using namespace testing::ext;
using namespace std;
class HitraceCtest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp();
void TearDown();
private:
};
void HitraceCtest::SetUp()
{
}
void HitraceCtest::TearDown()
{
}
void HitraceCtest::SetUpTestCase()
{
}
void HitraceCtest::TearDownTestCase()
{
}
/**
* @tc.name HiTrace C interface test
* @tc.number DFX_DFT_HiTrace_0001
* @tc.desc HiTrace C interface test
*/
HWTEST_F(HitraceCtest, HitraceC_interface, Function|MediumTest|Level1)
{
GTEST_LOG_(INFO) << "HitraceC_interface start" << endl;
HiTraceIdStruct hiTraceId;
HiTraceFlag hitraceflag = HITRACE_FLAG_INCLUDE_ASYNC;
hiTraceId = HiTraceBegin("test", HITRACE_FLAG_INCLUDE_ASYNC);
HiTraceSetId(&hiTraceId);
hiTraceId = HiTraceGetId();
ASSERT_TRUE(HiTraceIsValid(&hiTraceId));
HiTraceEnableFlag(&hiTraceId, hitraceflag);
ASSERT_TRUE(HiTraceIsFlagEnabled(&hiTraceId, hitraceflag));
int hitraceflags = HITRACE_FLAG_FAULT_TRIGGER;
HiTraceSetFlags(&hiTraceId, hitraceflags);
hitraceflags = HiTraceGetFlags(&hiTraceId);
ASSERT_TRUE(hitraceflags == HITRACE_FLAG_FAULT_TRIGGER);
uint64_t chainId = 10000;
HiTraceSetChainId(&hiTraceId, chainId);
chainId = HiTraceGetChainId(&hiTraceId);
ASSERT_TRUE(chainId==10000);
uint64_t spanId = 12345678;
HiTraceSetSpanId(&hiTraceId, spanId);
spanId = HiTraceGetSpanId(&hiTraceId);
ASSERT_TRUE(spanId == 12345678);
HiTraceIdStruct childId = HiTraceCreateSpan();
ASSERT_TRUE(HiTraceIsValid(&childId));
/* set child id to thread id */
HiTraceSetId(&childId);
/* continue to create child span */
HiTraceIdStruct grandChildId = HiTraceCreateSpan();
ASSERT_TRUE(HiTraceIsValid(&grandChildId));
EXPECT_EQ(HiTraceGetFlags(&grandChildId), HiTraceGetFlags(&childId));
EXPECT_EQ(HiTraceGetChainId(&grandChildId), HiTraceGetChainId(&childId));
EXPECT_EQ(HiTraceGetParentSpanId(&grandChildId), HiTraceGetSpanId(&childId));
HiTraceSetChainId(&grandChildId, chainId);
chainId = HiTraceGetChainId(&grandChildId);
ASSERT_TRUE(chainId==10000);
HiTraceSetSpanId(&grandChildId, spanId);
spanId = HiTraceGetSpanId(&grandChildId);
ASSERT_TRUE(spanId==12345678);
HiTraceClearId();
hiTraceId = HiTraceGetId();
ASSERT_FALSE(HiTraceIsValid(&hiTraceId));
HiTraceEnd(&hiTraceId);
GTEST_LOG_(INFO) << "HitraceC_interface end" << endl;
}
/**
* @tc.name HiTraceBegin interface test, when flag is invalid
* @tc.number DFX_DFT_HiTrace_0002
* @tc.desc HiTrace C interface test
*/
HWTEST_F(HitraceCtest, HitraceC_interface1, Function|MediumTest|Level1)
{
GTEST_LOG_(INFO) << "HitraceC_interface1 start" << endl;
/* begin with invalid flag */
HiTraceIdStruct invalidFlagId = HiTraceBegin("invalid param", HITRACE_FLAG_MAX+1);
EXPECT_EQ(0, HiTraceIsValid(&invalidFlagId));
HiTraceEnd(&invalidFlagId);
invalidFlagId = HiTraceBegin("invalid param1", -1);
EXPECT_EQ(0, HiTraceIsValid(&invalidFlagId));
HiTraceEnd(&invalidFlagId);
GTEST_LOG_(INFO) << "HitraceC_interface1 end" << endl;
}
/**
* @tc.name HiTraceBegin interface test, when name is invalid.
* @tc.number DFX_DFT_HiTrace_0003
* @tc.desc HiTrace C interface test
*/
HWTEST_F(HitraceCtest, HitraceC_interface2, Function|MediumTest|Level1)
{
GTEST_LOG_(INFO) << "HitraceC_interface2 start" << endl;
/* begin with invalid name */
HiTraceIdStruct invalidNameId = HiTraceBegin("", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, HiTraceIsValid(&invalidNameId));
HiTraceEnd(&invalidNameId);
GTEST_LOG_(INFO) << "HitraceC_interface2 end" << endl;
}
/**
* @tc.name HiTraceBegin interface test, when name is very long.
* @tc.number DFX_DFT_HiTrace_0004
* @tc.desc HiTrace C++ interface test
*/
HWTEST_F(HitraceCtest, Hitrace_interface3, Function|MediumTest|Level1)
{
GTEST_LOG_(INFO) << "Hitrace_interface3 start" << endl;
char name[4096] = "test";
for (int i = 0; i < 1000; i++)
{
strcat(name, "test");
}
HiTraceIdStruct traceId = HiTraceBegin(name, HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, HiTraceIsValid(&traceId));
HiTraceEnd(&traceId);
GTEST_LOG_(INFO) << "Hitrace_interface3 end" << endl;
}
/**
* @tc.name Repeated call HiTraceBegin interface test.
* @tc.number DFX_DFT_HiTrace_0005
* @tc.desc HiTrace C interface test
*/
HWTEST_F(HitraceCtest, Hitrace_interface4, Function|MediumTest|Level1)
{
GTEST_LOG_(INFO) << "Hitrace_interface4 start" << endl;
HiTraceIdStruct traceId = HiTraceBegin("test", HITRACE_FLAG_TP_INFO);
HiTraceIdStruct traceId1 = HiTraceBegin("test1", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, HiTraceIsValid(&traceId));
EXPECT_EQ(0, HiTraceIsValid(&traceId1));
HiTraceEnd(&traceId);
HiTraceEnd(&traceId1);
GTEST_LOG_(INFO) << "Hitrace_interface4 end" << endl;
}
/**
* @tc.name End invalid traceid test.
* @tc.number DFX_DFT_HiTrace_0006
* @tc.desc HiTrace C interface test
*/
HWTEST_F(HitraceCtest, HitraceC_interface5, Function|MediumTest|Level1)
{
GTEST_LOG_(INFO) << "HitraceC_interface5 start" << endl;
HiTraceIdStruct traceId = HiTraceBegin("test", HITRACE_FLAG_TP_INFO);
HiTraceIdStruct invalidId = {0};
EXPECT_EQ(1, HiTraceIsValid(&traceId));
HiTraceEnd(&invalidId);
HiTraceIdStruct wrongEndId = HiTraceGetId();
EXPECT_EQ(1, HiTraceIsValid(&wrongEndId));
EXPECT_EQ(HiTraceGetChainId(&wrongEndId), HiTraceGetChainId(&traceId));
EXPECT_EQ(1, HiTraceIsFlagEnabled(&wrongEndId, HITRACE_FLAG_TP_INFO));
HiTraceEnd(&traceId);
GTEST_LOG_(INFO) << "HitraceC_interface5 end" << endl;
}
/**
* @tc.name End traceId that has ended.
* @tc.number DFX_DFT_HiTrace_0007
* @tc.desc HiTrace C interface test
*/
HWTEST_F(HitraceCtest, HitraceC_interface6, Function|MediumTest|Level1)
{
GTEST_LOG_(INFO) << "HitraceC_interface6 start" << endl;
HiTraceIdStruct traceId = HiTraceBegin("test", HITRACE_FLAG_TP_INFO);
EXPECT_EQ(1, HiTraceIsValid(&traceId));
HiTraceEnd(&traceId);
traceId = HiTraceGetId();
EXPECT_EQ(0, HiTraceIsValid(&traceId));
HiTraceEnd(&traceId);
traceId = HiTraceGetId();
EXPECT_EQ(0, HiTraceIsValid(&traceId));
GTEST_LOG_(INFO) << "HitraceC_interface6 end" << endl;
}
\ No newline at end of file
{
"kits": [
{
"push": [
"HitraceCtest->/data/local/tmp/HitraceCtest"
],
"type": "PushKit",
"post-push": [
"chmod -R 777 /data/local/tmp/*"
]
}
],
"driver": {
"native-test-timeout": "120000",
"type": "CppTest",
"module-name": "HitraceCtest",
"runtime-hint": "1s",
"native-test-device-path": "/data/local/tmp"
},
"description": "Configuration for HitraceCtest Tests"
}
\ No newline at end of file
......@@ -41,7 +41,7 @@ int ExecCmdWithRet(std::string cmd, std::vector<std::string> &resvec)
std::cout<< "cmd is " + cmd <<std::endl;
if ((cmd.find("hilog") == std::string::npos) && (cmd.find("hidumper") == std::string::npos)
&& (cmd.find("ps") == std::string::npos)) {
&& (cmd.find("ps") == std::string::npos) && (cmd.find("rm") == std::string::npos)) {
std::cout<<"unsupport cmd!" + cmd <<std::endl;
return 0;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册