1 Star 0 Fork 1

SmartSmallBoy/loongcollector_cpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
CheckpointManagerUnittest.cpp 3.61 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2022 iLogtail Authors
//
// 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 "checkpoint/CheckPointManager.h"
#include "common/FileSystemUtil.h"
#include "common/Flags.h"
#include "unittest/Unittest.h"
DECLARE_FLAG_INT32(checkpoint_find_max_file_count);
namespace logtail {
std::string kTestRootDir;
class CheckpointManagerUnittest : public ::testing::Test {
public:
static void SetUpTestCase() {
kTestRootDir = (bfs::path(GetProcessExecutionDir()) / "CheckpointManagerUnittest").string();
bfs::remove_all(kTestRootDir);
bfs::create_directories(kTestRootDir);
AppConfig::GetInstance()->SetLoongcollectorConfDir(kTestRootDir);
}
static void TearDownTestCase() { bfs::remove_all(kTestRootDir); }
void TestSearchFilePathByDevInodeInDirectory();
};
UNIT_TEST_CASE(CheckpointManagerUnittest, TestSearchFilePathByDevInodeInDirectory);
void CheckpointManagerUnittest::TestSearchFilePathByDevInodeInDirectory() {
const std::string kRotateFileName = "test.log.5";
const std::string kFileName = "test.log";
const std::string kFilePath = (bfs::path(kTestRootDir) / kFileName).string();
const std::string kRotateFilePath = (bfs::path(kTestRootDir) / kRotateFileName).string();
const std::string kTempPath = (bfs::path(kTestRootDir) / ".." / kFileName).string();
std::ofstream(kFilePath) << "";
fsutil::PathStat ps;
EXPECT_TRUE(fsutil::PathStat::stat(kFilePath, ps));
auto devInode = ps.GetDevInode();
// Rotate file in current directory.
bfs::rename(kFilePath, kRotateFilePath);
// Normal search.
{
auto const filePath = SearchFilePathByDevInodeInDirectory(kTestRootDir, 0, devInode, nullptr);
EXPECT_TRUE(filePath);
EXPECT_EQ(filePath.value(), kRotateFilePath);
}
// Exceed limit when search.
{
bfs::rename(kRotateFilePath, kTempPath);
auto bakLimit = INT32_FLAG(checkpoint_find_max_file_count);
INT32_FLAG(checkpoint_find_max_file_count) = 2;
for (size_t idx = 1; idx < 5; ++idx) {
std::ofstream(kFilePath + "." + std::to_string(idx)) << "";
}
std::map<DevInode, SplitedFilePath> cache;
auto const filePath = SearchFilePathByDevInodeInDirectory(kTestRootDir, 0, devInode, &cache);
EXPECT_FALSE(filePath);
EXPECT_EQ(cache.size(), INT32_FLAG(checkpoint_find_max_file_count) + 1);
INT32_FLAG(checkpoint_find_max_file_count) = bakLimit;
bfs::rename(kTempPath, kRotateFilePath);
}
// File is moved to sub-directory.
const auto kSubDir = bfs::path(kTestRootDir) / "sub" / "sub";
bfs::create_directories(kSubDir);
const auto kSubDirFilePath = (kSubDir / kRotateFileName).string();
bfs::rename(kRotateFilePath, kSubDirFilePath);
// Search with depth.
{
auto filePath = SearchFilePathByDevInodeInDirectory(kTestRootDir, 0, devInode, nullptr);
EXPECT_FALSE(filePath);
filePath = SearchFilePathByDevInodeInDirectory(kTestRootDir, 2, devInode, nullptr);
EXPECT_TRUE(filePath);
EXPECT_EQ(filePath.value(), kSubDirFilePath);
}
}
} // namespace logtail
UNIT_TEST_MAIN
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/SmartSmallBoy/loongcollector_cpp.git
git@gitee.com:SmartSmallBoy/loongcollector_cpp.git
SmartSmallBoy
loongcollector_cpp
loongcollector_cpp
master

搜索帮助