Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,15 @@ if(TARGET ${PROJECT_NAME}_unmanaged_test)
${PROJECT_NAME}_TestPlugins3)
endif()

ament_add_gtest(${PROJECT_NAME}_external_load_test external_load_test.cpp
APPEND_LIBRARY_DIRS "${append_library_dirs}"
)
if(TARGET ${PROJECT_NAME}_external_load_test)
target_include_directories(${PROJECT_NAME}_external_load_test PUBLIC "../include")
target_link_libraries(${PROJECT_NAME}_external_load_test ${PROJECT_NAME} project_options)
add_dependencies(${PROJECT_NAME}_external_load_test
${PROJECT_NAME}_TestPlugins1
${PROJECT_NAME}_TestPlugins2)
endif()

add_subdirectory(fviz_case_study)
102 changes: 102 additions & 0 deletions test/external_load_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Software License Agreement (BSD License)
*
* Copyright (c) 2026, Open Source Robotics Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <algorithm>
#include <string>
#include <tuple>

#include "class_loader/class_loader.hpp"
#include "rcpputils/shared_library.hpp"

#include "gtest/gtest.h"

#include "./base.hpp"

// Plugin libraries opened by means other than class_loader, e.g. linked into the executable or
// opened with dlopen(). After that, class_loader never unloads any library in the process, so
// these tests run in their own executable.

const std::string LIBRARY_1 = class_loader::systemLibraryFormat("class_loader_TestPlugins1"); // NOLINT
const std::string LIBRARY_2 = class_loader::systemLibraryFormat("class_loader_TestPlugins2"); // NOLINT

// Opens LIBRARY_1 outside class_loader, once for the whole process. The handle is leaked on
// purpose: closing the library at exit would unmap the code that class_loader's global factory
// maps, destroyed later, still need to delete the library's MetaObjects.
void openLibrary1Externally()
{
static const auto * library = new rcpputils::SharedLibrary(LIBRARY_1);
(void)library;
ASSERT_TRUE(class_loader::impl::hasANonPurePluginLibraryBeenOpened());
}

// A ClassLoader with an empty path stands for libraries linked into the executable, and can use
// the classes of any library opened outside class_loader.
TEST(ClassLoaderExternalLoadTest, linkedClassLoader) {
ASSERT_NO_FATAL_FAILURE(openLibrary1Externally());
class_loader::ClassLoader loader("");
const auto classes = loader.getAvailableClasses<Base>();
EXPECT_NE(classes.end(), std::ranges::find(classes, "Dog"));
EXPECT_NE(classes.end(), std::ranges::find(classes, "Cat"));
EXPECT_TRUE(loader.isClassAvailable<Base>("Dog"));
EXPECT_EQ("Bark", loader.createInstance<Base>("Dog")->saySomething());
EXPECT_EQ("Meow", loader.createUniqueInstance<Base>("Cat")->saySomething());
EXPECT_THROW(
std::ignore = loader.createInstance<Base>("Robot"), class_loader::CreateClassException);
}

// A ClassLoader for the same library can still use its classes.
TEST(ClassLoaderExternalLoadTest, loaderForExternallyOpenedLibrary) {
ASSERT_NO_FATAL_FAILURE(openLibrary1Externally());
for (bool on_demand : {false, true}) {
SCOPED_TRACE(on_demand ? "on-demand" : "non-on-demand");
class_loader::ClassLoader loader(LIBRARY_1, on_demand);
EXPECT_TRUE(loader.isClassAvailable<Base>("Dog"));
EXPECT_EQ("Bark", loader.createInstance<Base>("Dog")->saySomething());
EXPECT_EQ("Meow", loader.createUniqueInstance<Base>("Cat")->saySomething());
}
}

// Other libraries still load and work, but are no longer unloaded.
TEST(ClassLoaderExternalLoadTest, otherLibrariesAreNotUnloaded) {
ASSERT_NO_FATAL_FAILURE(openLibrary1Externally());
// Robot's factory keeps this loader as its owner, since the library is never unloaded. The
// loader is leaked so that no other ClassLoader can reuse its address and take over Robot.
static auto * loader = new class_loader::ClassLoader(LIBRARY_2, false);
EXPECT_EQ("Beep boop", loader->createInstance<Base>("Robot")->saySomething());
loader->unloadLibrary();
EXPECT_TRUE(class_loader::impl::isLibraryLoadedByAnybody(LIBRARY_2));
}

int main(int argc, char ** argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
24 changes: 24 additions & 0 deletions test/unmanaged_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@

#include <memory>
#include <string>
#include <tuple>

#include "class_loader/class_loader.hpp"
#include "class_loader/multi_library_class_loader.hpp"

#include "gtest/gtest.h"

Expand Down Expand Up @@ -60,6 +62,28 @@ TEST(ClassLoaderUnmanagedTest, constructorLoadUnmanaged) {
delete unmanaged_instance;
}

TEST(ClassLoaderUnmanagedTest, multiLibraryClassLoaderUnmanaged) {
class_loader::MultiLibraryClassLoader loader(false);
loader.loadLibrary(LIBRARY_1);
loader.loadLibrary(LIBRARY_3);

std::unique_ptr<Base> dog(loader.createUnmanagedInstance<Base>("Dog"));
EXPECT_EQ("Bark", dog->saySomething());
std::unique_ptr<Base> cat(loader.createUnmanagedInstance<Base>("Cat", LIBRARY_1));
EXPECT_EQ("Meow", cat->saySomething());
std::unique_ptr<BaseWithInterfaceCtor> doubled(
loader.createUnmanagedInstance<BaseWithInterfaceCtor>(
"Double", LIBRARY_3, "double", std::make_unique<int>(4)));
EXPECT_EQ(8, doubled->get_number());

EXPECT_THROW(
std::ignore = loader.createUnmanagedInstance<Base>("Dog2"),
class_loader::CreateClassException);
EXPECT_THROW(
std::ignore = loader.createUnmanagedInstance<Base>("Dog", "not_a_loaded_library"),
class_loader::NoClassLoaderExistsException);
}

int main(int argc, char ** argv)
{
testing::InitGoogleTest(&argc, argv);
Expand Down
103 changes: 103 additions & 0 deletions test/utest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <dlfcn.h>
#endif

#include <algorithm>
#include <memory>
#include <string>
#include <thread>
Expand Down Expand Up @@ -300,6 +301,73 @@ TYPED_TEST(MultiClassLoaderTest, nonExistentPlugin) {
class_loader::ClassLoaderException);
}

TYPED_TEST(MultiClassLoaderTest, createFromSpecificLibrary) {
for (bool on_demand : {false, true}) {
SCOPED_TRACE(on_demand ? "on-demand" : "non-on-demand");
class_loader::MultiLibraryClassLoader loader(on_demand);
loader.loadLibrary(LIBRARY_1);
loader.loadLibrary(LIBRARY_2);
EXPECT_EQ("Bark", TypeParam::template create<Base>(loader, "Dog", LIBRARY_1)->saySomething());
EXPECT_EQ(
"Beep boop", TypeParam::template create<Base>(loader, "Robot", LIBRARY_2)->saySomething());
// The class exists, but not in that library.
EXPECT_THROW(
std::ignore = TypeParam::template create<Base>(loader, "Dog", LIBRARY_2),
class_loader::CreateClassException);
// The library was never loaded through this loader.
EXPECT_THROW(
std::ignore = TypeParam::template create<Base>(loader, "Dog", LIBRARY_3),
class_loader::NoClassLoaderExistsException);
}
}

TYPED_TEST(MultiClassLoaderTest, constructorArguments) {
class_loader::MultiLibraryClassLoader loader(false);
loader.loadLibrary(LIBRARY_3);
EXPECT_EQ(
3, TypeParam::template create<BaseWithInterfaceCtor>(
loader, "Identity", "identity", std::make_unique<int>(3))->get_number());
// The string argument must not be taken for a library path, nor the reverse.
EXPECT_EQ(
6, TypeParam::template create<BaseWithInterfaceCtor>(
loader, "Double", LIBRARY_3, "double", std::make_unique<int>(3))->get_number());
EXPECT_THROW(
std::ignore = TypeParam::template create<BaseWithInterfaceCtor>(
loader, "Double", LIBRARY_1, "double", std::make_unique<int>(3)),
class_loader::NoClassLoaderExistsException);
}

TEST(MultiClassLoaderBasicTest, libraryQueries) {
class_loader::MultiLibraryClassLoader loader(false);
EXPECT_TRUE(loader.getRegisteredLibraries().empty());
EXPECT_FALSE(loader.isLibraryAvailable(LIBRARY_1));
EXPECT_THROW(
std::ignore = loader.getAvailableClassesForLibrary<Base>(LIBRARY_1),
class_loader::NoClassLoaderExistsException);

loader.loadLibrary(LIBRARY_1);
loader.loadLibrary(LIBRARY_2);
auto libraries = loader.getRegisteredLibraries();
std::ranges::sort(libraries);
std::vector<std::string> expected{LIBRARY_1, LIBRARY_2};
std::ranges::sort(expected);
EXPECT_EQ(expected, libraries);
EXPECT_TRUE(loader.isLibraryAvailable(LIBRARY_1));
EXPECT_TRUE(loader.isLibraryAvailable(LIBRARY_2));
EXPECT_FALSE(loader.isLibraryAvailable(LIBRARY_3));

const auto classes = loader.getAvailableClassesForLibrary<Base>(LIBRARY_2);
EXPECT_NE(classes.end(), std::ranges::find(classes, "Robot"));
EXPECT_EQ(classes.end(), std::ranges::find(classes, "Dog"));
EXPECT_TRUE(loader.isClassAvailable<Base>("Dog"));
EXPECT_FALSE(loader.isClassAvailable<Base>("Dog2"));

loader.unloadLibrary(LIBRARY_1);
EXPECT_FALSE(loader.isLibraryAvailable(LIBRARY_1));
EXPECT_FALSE(loader.isClassAvailable<Base>("Dog"));
EXPECT_EQ(std::vector<std::string>{LIBRARY_2}, loader.getRegisteredLibraries());
}

// Tests that don't depend on how instances are managed.

TEST(ClassLoaderBasicTest, basicLoadFailures) {
Expand All @@ -317,6 +385,41 @@ TEST(ClassLoaderBasicTest, librariesUsedByClassLoader) {
ASSERT_EQ(v.size(), 1u);
}

TEST(ClassLoaderBasicTest, isLibraryLoadedByAnyClassloader) {
if (class_loader::impl::hasANonPurePluginLibraryBeenOpened()) {
GTEST_SKIP() << "Libraries are never unloaded once loadUnloadLoadFromGraveyard has run";
}
class_loader::ClassLoader loader1(LIBRARY_1, false);
class_loader::ClassLoader loader2(LIBRARY_1, true);
EXPECT_TRUE(loader1.isLibraryLoaded());
EXPECT_FALSE(loader2.isLibraryLoaded());
EXPECT_TRUE(loader2.isLibraryLoadedByAnyClassloader());
loader1.unloadLibrary();
EXPECT_FALSE(loader1.isLibraryLoadedByAnyClassloader());
EXPECT_FALSE(loader2.isLibraryLoadedByAnyClassloader());
}

// An empty path stands for libraries linked into the executable: nothing to load or unload.
TEST(ClassLoaderBasicTest, emptyPathLoadsNothing) {
class_loader::ClassLoader loader("", false);
loader.loadLibrary();
EXPECT_EQ(0, loader.unloadLibrary());
EXPECT_EQ(0, loader.unloadLibrary());
EXPECT_TRUE(class_loader::impl::getAllLibrariesUsedByClassLoader(&loader).empty());
}

// Unloading a library that class_loader doesn't have loaded does nothing.
TEST(ClassLoaderBasicTest, unloadUnknownLibrary) {
if (class_loader::impl::hasANonPurePluginLibraryBeenOpened()) {
GTEST_SKIP() << "Libraries are never unloaded once loadUnloadLoadFromGraveyard has run";
}
class_loader::ClassLoader loader(LIBRARY_1, true);
EXPECT_NO_THROW(class_loader::impl::unloadLibrary(LIBRARY_1, &loader));
EXPECT_NO_THROW(class_loader::impl::unloadLibrary("not_a_library", &loader));
EXPECT_FALSE(class_loader::impl::isLibraryLoadedByAnybody(LIBRARY_1));
EXPECT_EQ("Bark", loader.createInstance<Base>("Dog")->saySomething());
}

TEST(ClassLoaderBasicTest, correctNonLazyLoadUnload) {
ASSERT_FALSE(class_loader::impl::isLibraryLoadedByAnybody(LIBRARY_1));
class_loader::ClassLoader loader1(LIBRARY_1, false);
Expand Down
Loading