From 1b6b0d5a77ab49b63cb55f7423315f94e4031471 Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Mon, 19 Jan 2026 10:55:54 +0000 Subject: [PATCH 1/2] Extended capio_posix_unit_tests This commit extends the test suite by adding a new unit test to check the correct behaviour of the read and write cache components. --- capio/tests/unit/posix/CMakeLists.txt | 2 +- capio/tests/unit/posix/src/cache.hpp | 129 ++++++++++++++++++ capio/tests/unit/posix/src/main.cpp | 3 + .../posix/src/{realpath.cpp => realpath.hpp} | 1 - 4 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 capio/tests/unit/posix/src/cache.hpp create mode 100644 capio/tests/unit/posix/src/main.cpp rename capio/tests/unit/posix/src/{realpath.cpp => realpath.hpp} (98%) diff --git a/capio/tests/unit/posix/CMakeLists.txt b/capio/tests/unit/posix/CMakeLists.txt index 092bd62c9..cd075014d 100644 --- a/capio/tests/unit/posix/CMakeLists.txt +++ b/capio/tests/unit/posix/CMakeLists.txt @@ -4,7 +4,7 @@ set(TARGET_NAME capio_posix_unit_tests) set(TARGET_INCLUDE_FOLDER "${PROJECT_SOURCE_DIR}/capio/posix") set(TARGET_SOURCES - ${CMAKE_CURRENT_SOURCE_DIR}/src/realpath.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp ) ##################################### diff --git a/capio/tests/unit/posix/src/cache.hpp b/capio/tests/unit/posix/src/cache.hpp new file mode 100644 index 000000000..81ecf85d3 --- /dev/null +++ b/capio/tests/unit/posix/src/cache.hpp @@ -0,0 +1,129 @@ +#include + +#include "common/syscall.hpp" + +#include "utils/clone.hpp" +#include "utils/filesystem.hpp" +#include "utils/snapshot.hpp" + +#include "posix/handlers.hpp" + +inline int server_pid = -1; + +inline char **build_args() { + const auto args = static_cast(malloc(3 * sizeof(uintptr_t))); + + char const *command = std::getenv("CAPIO_SERVER_PATH"); + if (command == nullptr) { + command = "capio_server"; + } + + args[0] = strdup(command); + args[1] = strdup("--no-config"); + args[2] = static_cast(nullptr); + + return args; +} + +inline char **build_env() { + const char *home = getenv("HOME"); + + if (!home) { + if (passwd *pw = getpwuid(getuid())) { + home = pw->pw_dir; + } + } + + // 2 variables + NULL + auto env = static_cast(malloc(4 * sizeof(char *))); + + env[0] = strdup("CAPIO_DIR=/tmp"); + env[1] = strdup("CAPIO_LOG_LEVEL=-1"); + + if (home) { + std::string home_var = std::string("HOME=") + home; + env[2] = strdup(home_var.c_str()); + env[3] = nullptr; + } else { + // extremely unlikely, but keep env valid + env[2] = nullptr; + } + + return env; +} + +class CapioServerEnvironment : public testing::Test { + + protected: + static void SetUpTestSuite() { + + char **args = build_args(); + char **envp = build_env(); + if (server_pid < 0) { + const auto server_path = std::getenv("CAPIO_SERVER_PATH"); + ASSERT_GE(server_pid = fork(), 0); + ASSERT_NE(server_path, nullptr); + if (server_pid == 0) { + execvpe(server_path, args, envp); + _exit(127); + } + sleep(5); + } + } + + static void TearDownTestSuite() { + if (server_pid > 0) { + kill(server_pid, SIGTERM); + waitpid(server_pid, nullptr, 0); + server_pid = -1; + } + } +}; + +TEST_F(CapioServerEnvironment, testReadWrite1GBFileBy4kBuffer) { + + putenv("CAPIO_DIR=/tmp"); + + const auto tid = gettid(); + const auto wf_name = get_capio_workflow_name(); + const auto lines = CAPIO_CACHE_LINES_DEFAULT; + const auto line_size = CAPIO_CACHE_LINE_SIZE_DEFAULT; + long result; + + constexpr uint64_t FILE_SIZE = 1024 * 1024 * 1024; + + char pathname[] = "/tmp/data.dat"; + + const auto write_buffer = new char[FILE_SIZE]; // 1GB data + const auto read_buffer = new char[FILE_SIZE]; // 1GB data + + // fill write_buffer and read buffer + for (auto i = 0; i < FILE_SIZE; i++) { + write_buffer[i] = '1'; // init all data to 1 + read_buffer[i] = '0'; // init all data to 0, so that afterward it will become 1 + } + + init_client(tid); + init_filesystem(); + initialize_new_thread(); + + long fd; + + open_handler(reinterpret_cast(pathname), O_RDWR | O_CREAT, 0, 0, 0, 0, &fd); + EXPECT_GT(fd, 3); + write_handler(fd, reinterpret_cast(write_buffer), FILE_SIZE, 0, 0, 0, &result); + close_handler(fd, 0, 0, 0, 0, 0, &result); + + open_handler(reinterpret_cast(pathname), O_RDWR, 0, 0, 0, 0, &fd); + EXPECT_GT(fd, 3); + + read_handler(fd, reinterpret_cast(read_buffer), FILE_SIZE, 0, 0, 0, &result); + close_handler(fd, 0, 0, 0, 0, 0, &result); + + for (auto i = 0; i < FILE_SIZE; i++) { + EXPECT_EQ(read_buffer[i], write_buffer[i]); + } + + delete[] write_buffer; + delete[] read_buffer; +} \ No newline at end of file diff --git a/capio/tests/unit/posix/src/main.cpp b/capio/tests/unit/posix/src/main.cpp new file mode 100644 index 000000000..af55f8211 --- /dev/null +++ b/capio/tests/unit/posix/src/main.cpp @@ -0,0 +1,3 @@ +#include +#include "realpath.hpp" +#include "cache.hpp" \ No newline at end of file diff --git a/capio/tests/unit/posix/src/realpath.cpp b/capio/tests/unit/posix/src/realpath.hpp similarity index 98% rename from capio/tests/unit/posix/src/realpath.cpp rename to capio/tests/unit/posix/src/realpath.hpp index cfa38e8ae..f8c73656f 100644 --- a/capio/tests/unit/posix/src/realpath.cpp +++ b/capio/tests/unit/posix/src/realpath.hpp @@ -1,4 +1,3 @@ -#include #include From 8854441fdfad76971ea8dd5e4caa74cb895e7156 Mon Sep 17 00:00:00 2001 From: Marco Edoardo Santimaria Date: Mon, 19 Jan 2026 11:42:28 +0000 Subject: [PATCH 2/2] Investigating issue when reading big files at aroung 5kb offset from beginning of file --- capio/tests/unit/posix/src/cache.hpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/capio/tests/unit/posix/src/cache.hpp b/capio/tests/unit/posix/src/cache.hpp index 81ecf85d3..dc6ad4d5f 100644 --- a/capio/tests/unit/posix/src/cache.hpp +++ b/capio/tests/unit/posix/src/cache.hpp @@ -117,10 +117,22 @@ TEST_F(CapioServerEnvironment, testReadWrite1GBFileBy4kBuffer) { open_handler(reinterpret_cast(pathname), O_RDWR, 0, 0, 0, 0, &fd); EXPECT_GT(fd, 3); - read_handler(fd, reinterpret_cast(read_buffer), FILE_SIZE, 0, 0, 0, &result); + size_t total_read = 0; + const long read_size = 2 * 4096; + + while (total_read < FILE_SIZE) { + read_handler(fd, reinterpret_cast(read_buffer + total_read), read_size, 0, 0, 0, + &result); + total_read += read_size; + } + close_handler(fd, 0, 0, 0, 0, 0, &result); for (auto i = 0; i < FILE_SIZE; i++) { + if (read_buffer[i] != write_buffer[i]) { + std::cout << "i=" << i << " read_buffer[i]=" << read_buffer[i] + << " write_buffer[i]=" << write_buffer[i] << std::endl; + } EXPECT_EQ(read_buffer[i], write_buffer[i]); }