facebook / CacheLib

Pluggable in-process caching engine to build and scale high performance services

Home Page:https://www.cachelib.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NavySetup should not involve the MockDevice

tang-hi opened this issue · comments

Describe the bug
After this commit was merged, the script ./contrib/build.sh -d -j -v no longer works because the CMakeLists.txt file in the cachebench directory does not add the dependencies for gtest and gmock. This results in numerous undefined reference errors when I compile the project:

/usr/bin/ld: ../allocator/libcachelib_allocator.a(NavySetup.cpp.o): in function `std::__detail::_MakeUniq<facebook::cachelib::navy::MockDevice>::__single_object std::make_unique<facebook::cachelib::navy::MockDevice, unsigned long, unsigned int>(unsigned long&&, unsigned int&&)':
/usr/include/c++/14.1.1/bits/unique_ptr.h:1076:

(.text._ZSt11make_uniqueIN8facebook8cachelib4navy10MockDeviceEJmjEENSt8__detail9_MakeUniqIT_E15__single_objectEDpOT0_[_ZSt11make_uniqueIN8facebook8cachelib4navy10MockDeviceEJmjEENSt8__detail9_MakeUniqIT_E15__single_objectEDpOT0_]+0x79): 


undefined reference to `facebook::cachelib::navy::MockDevice::MockDevice(unsigned long, unsigned int,std::shared_ptr<facebook::cachelib::navy::DeviceEncryptor>)'

Although you could add gtest and gmock to cachelib/cachebench/CMakeLists.txt and move the constructor from MockDevice.cpp to MockDevice.h to fix this, I don't think it is a good solution. We shouldn't include the test file in the source file.

target_link_libraries(cachelib_cachebench PUBLIC
   cachelib_datatype
   cachelib_allocator
   gflags
+  gtest
+  gmock
 )
//MockDevice.cpp
namespace facebook {
 namespace cachelib {
 namespace navy {
-MockDevice::MockDevice(uint64_t deviceSize,
-                       uint32_t ioAlignSize,
-                       std::shared_ptr<DeviceEncryptor> encryptor)
-    : Device{deviceSize, nullptr, ioAlignSize, 0, 0},
-      device_{deviceSize == 0
-                  ? nullptr
-                  : createMemoryDevice(
-                        deviceSize, std::move(encryptor), ioAlignSize)} {
-  ON_CALL(*this, readImpl(testing::_, testing::_, testing::_))
-      .WillByDefault(
-          testing::Invoke([this](uint64_t offset, uint32_t size, void* buffer) {
-            XDCHECK_EQ(size % getIOAlignmentSize(), 0u);
-            XDCHECK_EQ(offset % getIOAlignmentSize(), 0u);
-            return device_->read(offset, size, buffer);
-          }));
-
-  ON_CALL(*this, writeImpl(testing::_, testing::_, testing::_, testing::_))
-      .WillByDefault(testing::Invoke(
-          [this](uint64_t offset, uint32_t size, const void* data, int) {
-            XDCHECK_EQ(size % getIOAlignmentSize(), 0u);
-            XDCHECK_EQ(offset % getIOAlignmentSize(), 0u);
-            Buffer buffer = device_->makeIOBuffer(size);
-            std::memcpy(buffer.data(), data, size);
-            return device_->write(offset, std::move(buffer));
-          }));
-
-  ON_CALL(*this, flushImpl()).WillByDefault(testing::Invoke([this]() {
-    device_->flush();
-  }));
-
-  ON_CALL(*this, allocatePlacementHandle()).WillByDefault(testing::Invoke([]() {
-    return -1;
-  }));
-}
 } // namespace navy
 } // namespace cachelib
 } // namespace facebook
// MockDevice.h
   std::unique_ptr<Device> device_;
 };
 
+MockDevice::MockDevice(uint64_t deviceSize,
+                       uint32_t ioAlignSize,
+                       std::shared_ptr<DeviceEncryptor> encryptor)
+    : Device{deviceSize, nullptr, ioAlignSize, 0, 0},
+      device_{deviceSize == 0
+                  ? nullptr
+                  : createMemoryDevice(
+                        deviceSize, std::move(encryptor), ioAlignSize)} {
+  ON_CALL(*this, readImpl(testing::_, testing::_, testing::_))
+      .WillByDefault(
+          testing::Invoke([this](uint64_t offset, uint32_t size, void* buffer) {
+            XDCHECK_EQ(size % getIOAlignmentSize(), 0u);
+            XDCHECK_EQ(offset % getIOAlignmentSize(), 0u);
+            return device_->read(offset, size, buffer);
+          }));
+
+  ON_CALL(*this, writeImpl(testing::_, testing::_, testing::_, testing::_))
+      .WillByDefault(testing::Invoke(
+          [this](uint64_t offset, uint32_t size, const void* data, int) {
+            XDCHECK_EQ(size % getIOAlignmentSize(), 0u);
+            XDCHECK_EQ(offset % getIOAlignmentSize(), 0u);
+            Buffer buffer = device_->makeIOBuffer(size);
+            std::memcpy(buffer.data(), data, size);
+            return device_->write(offset, std::move(buffer));
+          }));
+
+  ON_CALL(*this, flushImpl()).WillByDefault(testing::Invoke([this]() {
+    device_->flush();
+  }));
+
+  ON_CALL(*this, allocatePlacementHandle()).WillByDefault(testing::Invoke([]() {
+    return -1;
+  }));
+}
+

To Reproduce
just run ./contrib/build.sh -d -j -v

Desktop (please complete the following information):

  • OS: arch-linux

I also encountered the same problem in ubuntu 20.04.

The build works now. The CMakeLists has been fixed along with the FDP tests. The FDP tests only run when FDP is enabled in the system.