From e1b5439ee2c3f4420149ecaa911766fde6d9fc9c Mon Sep 17 00:00:00 2001 From: Andreas Joachim Peters Date: Thu, 6 Jun 2024 17:24:28 +0200 Subject: [PATCH] TESTS: add XrdClVectorCache.cc test --- tests/XrdCl/CMakeLists.txt | 4 +++ tests/XrdCl/XrdClVectorCache.cc | 47 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 tests/XrdCl/XrdClVectorCache.cc diff --git a/tests/XrdCl/CMakeLists.txt b/tests/XrdCl/CMakeLists.txt index 015f4c92501..90e37eae0b9 100644 --- a/tests/XrdCl/CMakeLists.txt +++ b/tests/XrdCl/CMakeLists.txt @@ -3,6 +3,8 @@ add_executable(xrdcl-unit-tests XrdClPoller.cc XrdClSocket.cc XrdClUtilsTest.cc + XrdClVectorCache.cc + ../../src/XrdApps/XrdClJCachePlugin/XrdClVectorCache.cc ../common/Server.cc ../common/Utils.cc ../common/TestEnv.cc @@ -12,9 +14,11 @@ target_link_libraries(xrdcl-unit-tests XrdCl XrdXml XrdUtils + stdc++fs ZLIB::ZLIB GTest::GTest GTest::Main + OpenSSL::Crypto ) target_include_directories(xrdcl-unit-tests diff --git a/tests/XrdCl/XrdClVectorCache.cc b/tests/XrdCl/XrdClVectorCache.cc new file mode 100644 index 00000000000..8eb1aa087c7 --- /dev/null +++ b/tests/XrdCl/XrdClVectorCache.cc @@ -0,0 +1,47 @@ +#undef NDEBUG + +#include "XrdApps/XrdClJCachePlugin/XrdClVectorCache.hh" +#include "XrdSys/XrdSysPlatform.hh" + +#include + +#include +#include + +using namespace testing; + +// Test the JCAche VectorCache class + +class VectorCacheTest : public ::testing::Test {}; + +TEST(VectorCacheTest, Store) +{ + XrdCl::ChunkList chunks; + std::string name = "root://localhost//dummy"; + std::string prefix = "/tmp/"; + + char data[100]; // 100 bytes of fake data + char cdata[100]; // 100 bytes of zeroed data + + for (auto i=0; i< 100; i++) { + XrdCl::ChunkInfo s(i,1); + chunks.push_back(s); + data[i] = i; + cdata[i] = 0; + } + + EXPECT_TRUE(bcmp(data, cdata, 100) != 0) << "Data not zeroed" << std::endl; + VectorCache cacheout ( chunks, name, data, prefix); + EXPECT_TRUE(cacheout.store()) << "Failed to store vector read into cache" << std::endl; + VectorCache cachein ( chunks, name, cdata, prefix); + EXPECT_TRUE(cachein.retrieve()) << "Failed to retrieve vector read from cache" << std::endl; + EXPECT_TRUE(bcmp(data, cdata, 100) == 0) << "Cached data is wrong" << std::endl; + EXPECT_TRUE(truncate("/tmp/d1a4e9081bd37839e4b4f486ed8b13397ce9ffa0198edb586208d6b73e15b19a/3ec7dea73b7880fdce09e1c8f804054ae685e3dbda4d467c71ab2327ea5ad93e",99)==0) << "Failed to truncate cached file" << std::endl; + EXPECT_TRUE(!cachein.retrieve()) << "Truncate cache entry was not seen" << std::endl; + EXPECT_TRUE(unlink("/tmp/d1a4e9081bd37839e4b4f486ed8b13397ce9ffa0198edb586208d6b73e15b19a/3ec7dea73b7880fdce09e1c8f804054ae685e3dbda4d467c71ab2327ea5ad93e") == 0) << "Failed to unlink cached file" << std::endl; + EXPECT_TRUE(!cachein.retrieve()) << "Unlinked cache entry was not seen" << std::endl; + // re-cache the block an check if it is ok + EXPECT_TRUE(cacheout.store()) << "Failed to store vector read into cache" << std::endl; + EXPECT_TRUE(cachein.retrieve()) << "Failed to retrieve vector read from cache" << std::endl; +} +