Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions be/test/io/cache/cache_lru_dumper_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include "io/cache/cache_lru_dumper.h"

#include <filesystem>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "io/cache/block_file_cache.h"
Expand All @@ -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,
Expand All @@ -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<NiceMock<MockBlockFileCache>>(&dst_queue);
recorder = std::make_unique<LRUQueueRecorder>(mock_cache.get());

Expand All @@ -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<NiceMock<MockBlockFileCache>> mock_cache;
Expand All @@ -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;

Expand All @@ -87,29 +95,26 @@ 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<std::string> 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
dumper->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)));
}
}

Expand Down Expand Up @@ -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
Loading