* This file is part of the MindStudio project.
* Copyright (c) 2025 Huawei Technologies Co.,Ltd.
*
* MindStudio is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* -------------------------------------------------------------------------
*/
#define ENABLE_SERVICE_PROF_UNIT_TEST
#include <gtest/gtest.h>
#include <string>
#include <dlfcn.h>
#include <unistd.h>
#include "msServiceProfiler/msServiceProfiler.h"
#include "msServiceProfiler/ServiceProfilerDbWriter.h"
using namespace msServiceProfiler;
int mock_gethostname_return_value = 0;
static int (*real_gethostname)(char *, size_t) = nullptr;
extern "C" {
int gethostname(char *name, size_t len)
{
if (!real_gethostname) {
real_gethostname = (int (*)(char *, size_t))dlsym(RTLD_NEXT, "gethostname");
}
std::cerr << "nihao" << mock_gethostname_return_value << std::endl;
if (mock_gethostname_return_value != 0) {
return mock_gethostname_return_value;
}
return real_gethostname(name, len);
}
void SetMockGetHostNameReturnValue(int value)
{
mock_gethostname_return_value = value;
}
}
TEST(GetHostNameTest, GetHostName_Success_ReturnsValidHostname)
{
SetMockGetHostNameReturnValue(0);
std::string result = MsUtils::GetHostName();
EXPECT_FALSE(result.empty());
}
TEST(WaitForAllDumpTest, WaitForAllDump_Success)
{
try {
auto start = std::chrono::steady_clock::now();
msServiceProfiler::ServiceProfilerThreadWriter<DBFile::SERVICE>::GetWriter().WaitForAllDump();
auto duration = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - start);
EXPECT_LT(duration.count(), 2) << "WaitForAllDump() took too long, possibly stuck or not configured properly.";
std::cout << "Test passed, execution time: " << duration.count() << "s" << std::endl;
} catch (const std::exception &e) {
FAIL() << "Exception during WaitForAllDump(): " << e.what();
} catch (...) {
FAIL() << "Unknown exception during WaitForAllDump().";
}
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}