diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8c72c68..1e5ffea 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -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) diff --git a/test/external_load_test.cpp b/test/external_load_test.cpp new file mode 100644 index 0000000..984c2b0 --- /dev/null +++ b/test/external_load_test.cpp @@ -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 +#include +#include + +#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(); + EXPECT_NE(classes.end(), std::ranges::find(classes, "Dog")); + EXPECT_NE(classes.end(), std::ranges::find(classes, "Cat")); + EXPECT_TRUE(loader.isClassAvailable("Dog")); + EXPECT_EQ("Bark", loader.createInstance("Dog")->saySomething()); + EXPECT_EQ("Meow", loader.createUniqueInstance("Cat")->saySomething()); + EXPECT_THROW( + std::ignore = loader.createInstance("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("Dog")); + EXPECT_EQ("Bark", loader.createInstance("Dog")->saySomething()); + EXPECT_EQ("Meow", loader.createUniqueInstance("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("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(); +} diff --git a/test/unmanaged_test.cpp b/test/unmanaged_test.cpp index 3eeb96b..99e8bb8 100644 --- a/test/unmanaged_test.cpp +++ b/test/unmanaged_test.cpp @@ -29,8 +29,10 @@ #include #include +#include #include "class_loader/class_loader.hpp" +#include "class_loader/multi_library_class_loader.hpp" #include "gtest/gtest.h" @@ -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 dog(loader.createUnmanagedInstance("Dog")); + EXPECT_EQ("Bark", dog->saySomething()); + std::unique_ptr cat(loader.createUnmanagedInstance("Cat", LIBRARY_1)); + EXPECT_EQ("Meow", cat->saySomething()); + std::unique_ptr doubled( + loader.createUnmanagedInstance( + "Double", LIBRARY_3, "double", std::make_unique(4))); + EXPECT_EQ(8, doubled->get_number()); + + EXPECT_THROW( + std::ignore = loader.createUnmanagedInstance("Dog2"), + class_loader::CreateClassException); + EXPECT_THROW( + std::ignore = loader.createUnmanagedInstance("Dog", "not_a_loaded_library"), + class_loader::NoClassLoaderExistsException); +} + int main(int argc, char ** argv) { testing::InitGoogleTest(&argc, argv); diff --git a/test/utest.cpp b/test/utest.cpp index e9c3da1..77662e2 100644 --- a/test/utest.cpp +++ b/test/utest.cpp @@ -33,6 +33,7 @@ #include #endif +#include #include #include #include @@ -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(loader, "Dog", LIBRARY_1)->saySomething()); + EXPECT_EQ( + "Beep boop", TypeParam::template create(loader, "Robot", LIBRARY_2)->saySomething()); + // The class exists, but not in that library. + EXPECT_THROW( + std::ignore = TypeParam::template create(loader, "Dog", LIBRARY_2), + class_loader::CreateClassException); + // The library was never loaded through this loader. + EXPECT_THROW( + std::ignore = TypeParam::template create(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( + loader, "Identity", "identity", std::make_unique(3))->get_number()); + // The string argument must not be taken for a library path, nor the reverse. + EXPECT_EQ( + 6, TypeParam::template create( + loader, "Double", LIBRARY_3, "double", std::make_unique(3))->get_number()); + EXPECT_THROW( + std::ignore = TypeParam::template create( + loader, "Double", LIBRARY_1, "double", std::make_unique(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(LIBRARY_1), + class_loader::NoClassLoaderExistsException); + + loader.loadLibrary(LIBRARY_1); + loader.loadLibrary(LIBRARY_2); + auto libraries = loader.getRegisteredLibraries(); + std::ranges::sort(libraries); + std::vector 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(LIBRARY_2); + EXPECT_NE(classes.end(), std::ranges::find(classes, "Robot")); + EXPECT_EQ(classes.end(), std::ranges::find(classes, "Dog")); + EXPECT_TRUE(loader.isClassAvailable("Dog")); + EXPECT_FALSE(loader.isClassAvailable("Dog2")); + + loader.unloadLibrary(LIBRARY_1); + EXPECT_FALSE(loader.isLibraryAvailable(LIBRARY_1)); + EXPECT_FALSE(loader.isClassAvailable("Dog")); + EXPECT_EQ(std::vector{LIBRARY_2}, loader.getRegisteredLibraries()); +} + // Tests that don't depend on how instances are managed. TEST(ClassLoaderBasicTest, basicLoadFailures) { @@ -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("Dog")->saySomething()); +} + TEST(ClassLoaderBasicTest, correctNonLazyLoadUnload) { ASSERT_FALSE(class_loader::impl::isLibraryLoadedByAnybody(LIBRARY_1)); class_loader::ClassLoader loader1(LIBRARY_1, false);