From 8d6b06214840d91eaae9bc2e54d814bc63eef801 Mon Sep 17 00:00:00 2001 From: zhengyu Date: Sun, 10 Aug 2025 22:46:40 +0800 Subject: [PATCH] [test](filecache) fix lru dumper test (#54456) fix incorrect dumper file name and use directory to contain all the test dump files to make test clean. Signed-off-by: zhengyu --- be/test/io/cache/cache_lru_dumper_test.cpp | 28 ++++++++++++---------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/be/test/io/cache/cache_lru_dumper_test.cpp b/be/test/io/cache/cache_lru_dumper_test.cpp index fd4260b012b2ab..cf51fef4c0a6a7 100644 --- a/be/test/io/cache/cache_lru_dumper_test.cpp +++ b/be/test/io/cache/cache_lru_dumper_test.cpp @@ -17,6 +17,8 @@ #include "io/cache/cache_lru_dumper.h" +#include + #include "gmock/gmock.h" #include "gtest/gtest.h" #include "io/cache/block_file_cache.h" @@ -30,12 +32,14 @@ using ::testing::NiceMock; namespace doris::io { std::mutex _mutex; +static const std::string test_dir = "./cache_lru_dumper_test_dir/"; + class MockBlockFileCache : public BlockFileCache { public: LRUQueue* dst_queue; // Pointer to the destination queue MockBlockFileCache(LRUQueue* queue) : BlockFileCache("", {}), dst_queue(queue) { - _cache_base_path = "./"; + _cache_base_path = test_dir; } FileBlockCell* add_cell(const UInt128Wrapper& hash, const CacheContext& ctx, size_t offset, @@ -59,6 +63,9 @@ class CacheLRUDumperTest : public ::testing::Test { LRUQueue dst_queue; // Member variable for destination queue void SetUp() override { + std::filesystem::remove_all(test_dir); + std::filesystem::create_directory(test_dir); + mock_cache = std::make_unique>(&dst_queue); recorder = std::make_unique(mock_cache.get()); @@ -68,6 +75,7 @@ class CacheLRUDumperTest : public ::testing::Test { void TearDown() override { dumper.reset(); mock_cache.reset(); + std::filesystem::remove_all(test_dir); } std::unique_ptr> mock_cache; @@ -76,9 +84,9 @@ class CacheLRUDumperTest : public ::testing::Test { }; TEST_F(CacheLRUDumperTest, test_finalize_dump_and_parse_dump_footer) { - std::ofstream out("test_finalize.bin", std::ios::binary); - std::string tmp_filename = "test_finalize.bin.tmp"; - std::string final_filename = "test_finalize.bin"; + std::string tmp_filename = test_dir + "test_finalize.bin.tmp"; + std::string final_filename = test_dir + "test_finalize.bin"; + std::ofstream out(tmp_filename, std::ios::binary); size_t file_size = 0; size_t entry_num = 10; @@ -87,21 +95,18 @@ TEST_F(CacheLRUDumperTest, test_finalize_dump_and_parse_dump_footer) { dumper->finalize_dump(out, entry_num, tmp_filename, final_filename, file_size).ok()); // Test parse footer - std::ifstream in("test_finalize.bin", std::ios::binary); + std::ifstream in(final_filename, std::ios::binary); size_t parsed_entry_num = 0; EXPECT_TRUE(dumper->parse_dump_footer(in, final_filename, parsed_entry_num).ok()); EXPECT_EQ(entry_num, parsed_entry_num); - - out.close(); in.close(); - std::remove("test_finalize.bin"); } TEST_F(CacheLRUDumperTest, test_remove_lru_dump_files) { // Create test files std::vector queue_names = {"disposable", "index", "normal", "ttl"}; for (const auto& name : queue_names) { - std::ofstream(fmt::format("lru_dump_{}.tail", name)); + std::ofstream(fmt::format("{}lru_dump_{}.tail", test_dir, name)); } // Test remove @@ -109,7 +114,7 @@ TEST_F(CacheLRUDumperTest, test_remove_lru_dump_files) { // Verify files are removed for (const auto& name : queue_names) { - EXPECT_FALSE(std::filesystem::exists(fmt::format("lru_dump_{}.tail", name))); + EXPECT_FALSE(std::filesystem::exists(fmt::format("{}lru_dump_{}.tail", test_dir, name))); } } @@ -141,9 +146,6 @@ TEST_F(CacheLRUDumperTest, test_dump_and_restore_queue) { ++src_it; ++dst_it; } - - // Clean up - std::remove(fmt::format("lru_dump_{}.tail", queue_name).c_str()); } } // namespace doris::io \ No newline at end of file