Add GoogleTest with CTest integration + some tests

Also rename addSetup to onSetup to unify naming.
This commit is contained in:
Olli Saarikivi
2023-05-10 18:46:55 +00:00
parent 4045323aa2
commit 75a2af8de2
8 changed files with 70 additions and 9 deletions

View File

@@ -20,6 +20,12 @@ if(ALLOW_GDRCOPY)
find_package(GDRCopy)
endif()
include(CTest)
include(FetchContent)
FetchContent_Declare(googletest URL https://github.com/google/googletest/archive/b796f7d44681514f58a683a3a71ff17c94edb0c1.zip)
FetchContent_MakeAvailable(googletest)
include(GoogleTest)
set(CLANG_FORMAT_SOURCE_DIRS src tests)
include(${PROJECT_SOURCE_DIR}/cmake/AddClangFormatTargets.cmake)

View File

@@ -75,7 +75,7 @@ struct MemorySender : public Setuppable
MSCCLPP_API_CPP void Communicator::sendMemoryOnSetup(RegisteredMemory memory, int remoteRank, int tag)
{
addSetup(std::make_shared<MemorySender>(memory, remoteRank, tag));
onSetup(std::make_shared<MemorySender>(memory, remoteRank, tag));
}
struct MemoryReceiver : public Setuppable
@@ -99,7 +99,7 @@ struct MemoryReceiver : public Setuppable
MSCCLPP_API_CPP NonblockingFuture<RegisteredMemory> Communicator::recvMemoryOnSetup(int remoteRank, int tag)
{
auto memoryReceiver = std::make_shared<MemoryReceiver>(remoteRank, tag);
addSetup(memoryReceiver);
onSetup(memoryReceiver);
return NonblockingFuture<RegisteredMemory>(memoryReceiver->memoryPromise_.get_future());
}
@@ -131,11 +131,11 @@ MSCCLPP_API_CPP std::shared_ptr<Connection> Communicator::connectOnSetup(int rem
throw mscclpp::Error("Unsupported transport", ErrorCode::InternalError);
}
pimpl->connections_.push_back(conn);
addSetup(conn);
onSetup(conn);
return conn;
}
MSCCLPP_API_CPP void Communicator::addSetup(std::shared_ptr<Setuppable> setuppable)
MSCCLPP_API_CPP void Communicator::onSetup(std::shared_ptr<Setuppable> setuppable)
{
pimpl->toSetup_.push_back(setuppable);
}

View File

@@ -361,7 +361,7 @@ public:
std::shared_ptr<Connection> connectOnSetup(int remoteRank, int tag, Transport transport);
/* Add a custom Setuppable object to a list of objects to be setup later, when setup() is called. */
void addSetup(std::shared_ptr<Setuppable> setuppable);
void onSetup(std::shared_ptr<Setuppable> setuppable);
/* Setup all objects that have registered for setup. This includes any connections created by connect(). */
void setup();

View File

@@ -1,6 +1,6 @@
function(add_test_executable name sources)
add_executable(${name} ${sources})
target_link_libraries(${name} mscclpp)
target_link_libraries(${name} mscclpp CUDA::cudart CUDA::cuda_driver)
if(USE_MPI_FOR_TESTS)
target_link_libraries(${name} MPI::MPI_CXX)
target_compile_definitions(${name} PRIVATE MSCCLPP_USE_MPI_FOR_TESTS)
@@ -10,5 +10,10 @@ endfunction()
add_test_executable(bootstrap_test_cpp bootstrap_test_cpp.cc)
add_test_executable(communicator_test_cpp communicator_test_cpp.cu)
add_test_executable(allgather_test_cpp allgather_test_cpp.cu)
add_test_executable(ib_test ib_test.cc)
add_subdirectory(unittests)
# Unit tests
add_executable(unit_tests)
target_link_libraries(unit_tests GTest::gtest_main GTest::gmock_main mscclpp)
add_subdirectory(unit) # This adds the sources to the mscclpp target
gtest_discover_tests(unit_tests DISCOVERY_MODE PRE_TEST)

View File

@@ -0,0 +1,3 @@
target_sources(unit_tests PRIVATE
core_tests.cc
)

49
tests/unit/core_tests.cc Normal file
View File

@@ -0,0 +1,49 @@
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "mscclpp.hpp"
class LocalCommunicatorTest : public ::testing::Test {
protected:
void SetUp() override {
bootstrap = std::make_shared<mscclpp::Bootstrap>(0, 1);
comm = std::make_shared<mscclpp::Communicator>(bootstrap);
}
std::shared_ptr<mscclpp::Bootstrap> bootstrap;
std::shared_ptr<mscclpp::Communicator> comm;
};
class MockSetuppable : public mscclpp::Setuppable {
public:
MOCK_METHOD(void, beginSetup, (std::shared_ptr<mscclpp::BaseBootstrap> bootstrap), (override));
MOCK_METHOD(void, endSetup, (std::shared_ptr<mscclpp::BaseBootstrap> bootstrap), (override));
};
TEST_F(LocalCommunicatorTest, OnSetup) {
auto mockSetuppable = std::make_shared<MockSetuppable>();
comm->onSetup(mockSetuppable);
EXPECT_CALL(*mockSetuppable, beginSetup(std::dynamic_pointer_cast<mscclpp::BaseBootstrap>(bootstrap)));
EXPECT_CALL(*mockSetuppable, endSetup(std::dynamic_pointer_cast<mscclpp::BaseBootstrap>(bootstrap)));
comm->setup();
}
TEST_F(LocalCommunicatorTest, RegisterMemory) {
int dummy[42];
auto memory = comm->registerMemory(&dummy, sizeof(dummy), mscclpp::NoTransports);
EXPECT_EQ(memory.data(), &dummy);
EXPECT_EQ(memory.size(), sizeof(dummy));
EXPECT_EQ(memory.rank(), 0);
EXPECT_EQ(memory.transports(), mscclpp::NoTransports);
}
TEST_F(LocalCommunicatorTest, SendMemoryToSelf) {
int dummy[42];
auto memory = comm->registerMemory(&dummy, sizeof(dummy), mscclpp::NoTransports);
comm->sendMemoryOnSetup(memory, 0, 0);
auto memoryFuture = comm->recvMemoryOnSetup(0, 0);
comm->setup();
auto sameMemory = memoryFuture.get();
EXPECT_EQ(sameMemory.size(), memory.size());
EXPECT_EQ(sameMemory.rank(), memory.rank());
EXPECT_EQ(sameMemory.transports(), memory.transports());
}

View File

@@ -1,2 +0,0 @@
add_test_executable(ib_test ib_test.cc)
target_link_libraries(ib_test CUDA::cudart)