34 #include <gtest/gtest.h> 35 #include <boost/thread.hpp> 44 const char LIBRARY_1[] =
"libclass_loader_TestPlugins1.so";
45 const char LIBRARY_2[] =
"libclass_loader_TestPlugins2.so";
50 TEST(ClassLoaderUniquePtrTest, basicLoad) {
53 loader1.createUniqueInstance<
Base>(
"Cat")->saySomething();
56 FAIL() <<
"ClassLoaderException: " << e.what() <<
"\n";
61 TEST(ClassLoaderUniquePtrTest, correctLazyLoadUnload) {
69 ClassLoader::UniquePtr<Base> obj = loader1.createUniqueInstance<
Base>(
"Cat");
78 FAIL() <<
"ClassLoaderException: " << e.what() <<
"\n";
80 FAIL() <<
"Unhandled exception";
86 TEST(ClassLoaderUniquePtrTest, nonExistentPlugin) {
90 ClassLoader::UniquePtr<Base> obj = loader1.createUniqueInstance<
Base>(
"Bear");
92 FAIL() <<
"Null object being returned instead of exception thrown.";
100 FAIL() <<
"Unknown exception caught.\n";
103 FAIL() <<
"Did not throw exception as expected.\n";
110 boost::this_thread::sleep(boost::posix_time::seconds(seconds));
116 for (
unsigned int c = 0; c < classes.size(); c++) {
117 loader->createUniqueInstance<
Base>(classes.at(c))->saySomething();
121 TEST(ClassLoaderUniquePtrTest, threadSafety) {
129 std::vector<boost::thread> client_threads;
131 for (
unsigned int c = 0; c < 1000; c++) {
132 client_threads.emplace_back(std::bind(&
run, &loader1));
135 for (
unsigned int c = 0; c < client_threads.size(); c++) {
136 client_threads.at(c).join();
142 FAIL() <<
"Unexpected ClassLoaderException.";
144 FAIL() <<
"Unknown exception.";
151 TEST(ClassLoaderUniquePtrTest, loadRefCountingLazy) {
157 ClassLoader::UniquePtr<Base> obj = loader1.createUniqueInstance<
Base>(
"Dog");
183 FAIL() <<
"Unexpected exception.\n";
185 FAIL() <<
"Unknown exception caught.\n";
188 FAIL() <<
"Did not throw exception as expected.\n";
200 for (
int i = 0; i < 2; ++i) {
201 loader.createUniqueInstance<
Base>(
"Cat")->saySomething();
202 loader.createUniqueInstance<
Base>(
"Dog")->saySomething();
203 loader.createUniqueInstance<
Base>(
"Robot")->saySomething();
206 FAIL() <<
"ClassLoaderException: " << e.what() <<
"\n";
212 TEST(MultiClassLoaderUniquePtrTest, lazyLoad) {
215 TEST(MultiClassLoaderUniquePtrTest, lazyLoadSecondTime) {
218 TEST(MultiClassLoaderUniquePtrTest, nonLazyLoad) {
221 TEST(MultiClassLoaderUniquePtrTest, noWarningOnLazyLoad) {
223 ClassLoader::UniquePtr<Base> cat =
nullptr, dog =
nullptr, rob =
nullptr;
229 cat = loader.createUniqueInstance<
Base>(
"Cat");
230 dog = loader.createUniqueInstance<
Base>(
"Dog");
231 rob = loader.createUniqueInstance<
Base>(
"Robot");
237 FAIL() <<
"ClassLoaderException: " << e.what() <<
"\n";
246 int main(
int argc,
char ** argv)
248 testing::InitGoogleTest(&argc, argv);
249 return RUN_ALL_TESTS();
std::vector< std::string > getAvailableClasses()
Indicates which classes (i.e. class_loader) that can be loaded by this object.
A ClassLoader that can bind more than one runtime library.
bool isLibraryLoaded()
Indicates if a library is loaded within the scope of this ClassLoader. Note that the library may alre...
An exception class thrown when class_loader is unable to create a plugin.
int main(int argc, char **argv)
This class allows loading and unloading of dynamically linked libraries which contain class definitio...
virtual void saySomething()=0
void loadLibrary(const std::string &library_path)
Loads a library into memory for this class loader.
int unloadLibrary()
Attempts to unload a library loaded within scope of the ClassLoader. If the library is not opened...
TEST(ClassLoaderUniquePtrTest, basicLoad)
void testMultiClassLoader(bool lazy)
bool isLibraryLoadedByAnybody(const std::string &library_path)
Indicates if passed library has been loaded by ANY ClassLoader.
void loadLibrary()
Attempts to load a library on behalf of the ClassLoader. If the library is already opened...
void run(ClassLoader *loader)