unique_ptr_test.cpp
Go to the documentation of this file.
00001 #include "base.h"
00002 #include <class_loader/class_loader.h>
00003 #include <class_loader/multi_library_class_loader.h>
00004 
00005 #include <gtest/gtest.h>
00006 #include <boost/thread.hpp>
00007 
00008 #include <functional>
00009 #include <iostream>
00010 
00011 const std::string LIBRARY_1 = "libclass_loader_TestPlugins1.so";
00012 const std::string LIBRARY_2 = "libclass_loader_TestPlugins2.so";
00013 
00014 using class_loader::ClassLoader;
00015 
00016 /*****************************************************************************/
00017 TEST(ClassLoaderUniquePtrTest, basicLoad)
00018 {
00019   try
00020   {
00021     ClassLoader loader1(LIBRARY_1, false);
00022     loader1.createUniqueInstance<Base>("Cat")->saySomething(); //See if lazy load works
00023     SUCCEED();
00024   }
00025   catch(class_loader::ClassLoaderException& e)
00026   {
00027     FAIL() << "ClassLoaderException: " << e.what() << "\n";
00028   }
00029 }
00030 
00031 /*****************************************************************************/
00032 TEST(ClassLoaderUniquePtrTest, correctLazyLoadUnload)
00033 {
00034   try
00035   {
00036     ASSERT_FALSE(class_loader::class_loader_private::isLibraryLoadedByAnybody(LIBRARY_1));
00037     ClassLoader loader1(LIBRARY_1, true);
00038     ASSERT_FALSE(class_loader::class_loader_private::isLibraryLoadedByAnybody(LIBRARY_1));
00039     ASSERT_FALSE(loader1.isLibraryLoaded());
00040 
00041     {
00042       ClassLoader::UniquePtr<Base> obj = loader1.createUniqueInstance<Base>("Cat");
00043       ASSERT_TRUE(class_loader::class_loader_private::isLibraryLoadedByAnybody(LIBRARY_1));
00044       ASSERT_TRUE(loader1.isLibraryLoaded());
00045     }
00046 
00047     //The library will unload automatically when the only plugin object left is destroyed
00048     ASSERT_FALSE(class_loader::class_loader_private::isLibraryLoadedByAnybody(LIBRARY_1));
00049     return;
00050   }
00051   catch(class_loader::ClassLoaderException& e)
00052   {
00053     FAIL() << "ClassLoaderException: " << e.what() << "\n";
00054   }
00055   catch(...)
00056   {
00057     FAIL() << "Unhandled exception";
00058   }
00059 }
00060 
00061 /*****************************************************************************/
00062 
00063 TEST(ClassLoaderUniquePtrTest, nonExistentPlugin)
00064 {
00065   ClassLoader loader1(LIBRARY_1, false);
00066 
00067   try
00068   {
00069     ClassLoader::UniquePtr<Base> obj = loader1.createUniqueInstance<Base>("Bear");
00070     if(obj == NULL)
00071       FAIL() << "Null object being returned instead of exception thrown.";
00072 
00073     obj->saySomething();
00074   }
00075   catch(const class_loader::CreateClassException& e)
00076   {
00077     SUCCEED();
00078     return;
00079   }
00080   catch(...)
00081   {
00082     FAIL() << "Unknown exception caught.\n";
00083   }
00084 
00085   FAIL() << "Did not throw exception as expected.\n";
00086 }
00087 
00088 /*****************************************************************************/
00089 
00090 void wait(int seconds)
00091 {
00092   boost::this_thread::sleep(boost::posix_time::seconds(seconds));
00093 }
00094 
00095 void run(ClassLoader* loader)
00096 {
00097   std::vector<std::string> classes = loader->getAvailableClasses<Base>();
00098   for(unsigned int c = 0; c < classes.size(); c++)
00099   {
00100     loader->createUniqueInstance<Base>(classes.at(c))->saySomething();
00101   }
00102 }
00103 
00104 TEST(ClassLoaderUniquePtrTest, threadSafety)
00105 {
00106   ClassLoader loader1(LIBRARY_1);
00107   ASSERT_TRUE(loader1.isLibraryLoaded());
00108 
00109   //Note: Hard to test thread safety to make sure memory isn't corrupted.
00110   //The hope is this test is hard enough that once in a while it'll segfault
00111   //or something if there's some implementation error.
00112   try
00113   {
00114     std::vector<boost::thread> client_threads;
00115 
00116     for(unsigned int c = 0; c < 1000; c++)
00117       client_threads.emplace_back(std::bind(&run, &loader1));
00118 
00119     for(unsigned int c = 0; c < client_threads.size(); c++)
00120       client_threads.at(c).join();
00121 
00122     loader1.unloadLibrary();
00123     ASSERT_FALSE(loader1.isLibraryLoaded());
00124 
00125   }
00126   catch(const class_loader::ClassLoaderException& ex)
00127   {
00128     FAIL() << "Unexpected ClassLoaderException.";
00129   }
00130   catch(...)
00131   {
00132     FAIL() << "Unknown exception.";
00133   }
00134 }
00135 
00136 
00137 /*****************************************************************************/
00138 
00139 TEST(ClassLoaderUniquePtrTest, loadRefCountingLazy)
00140 {
00141   try
00142   {
00143     ClassLoader loader1(LIBRARY_1, true);
00144     ASSERT_FALSE(loader1.isLibraryLoaded());
00145 
00146     {
00147       ClassLoader::UniquePtr<Base> obj = loader1.createUniqueInstance<Base>("Dog");
00148       ASSERT_TRUE(loader1.isLibraryLoaded());
00149     }
00150 
00151     ASSERT_FALSE(loader1.isLibraryLoaded());
00152 
00153     loader1.loadLibrary();
00154     ASSERT_TRUE(loader1.isLibraryLoaded());
00155 
00156     loader1.loadLibrary();
00157     ASSERT_TRUE(loader1.isLibraryLoaded());
00158 
00159     loader1.unloadLibrary();
00160     ASSERT_TRUE(loader1.isLibraryLoaded());
00161 
00162     loader1.unloadLibrary();
00163     ASSERT_FALSE(loader1.isLibraryLoaded());
00164 
00165     loader1.unloadLibrary();
00166     ASSERT_FALSE(loader1.isLibraryLoaded());
00167 
00168     loader1.loadLibrary();
00169     ASSERT_TRUE(loader1.isLibraryLoaded());
00170 
00171     return;
00172   }
00173   catch(const class_loader::ClassLoaderException& e)
00174   {
00175     FAIL() << "Unexpected exception.\n";
00176   }
00177   catch(...)
00178   {
00179     FAIL() << "Unknown exception caught.\n";
00180   }
00181 
00182   FAIL() << "Did not throw exception as expected.\n";
00183 }
00184 
00185 
00186 /*****************************************************************************/
00187 
00188 void testMultiClassLoader(bool lazy)
00189 {
00190   try
00191   {
00192     class_loader::MultiLibraryClassLoader loader(lazy);
00193     loader.loadLibrary(LIBRARY_1);
00194     loader.loadLibrary(LIBRARY_2);
00195     for (int i=0; i < 2; ++i) {
00196       loader.createUniqueInstance<Base>("Cat")->saySomething();
00197       loader.createUniqueInstance<Base>("Dog")->saySomething();
00198       loader.createUniqueInstance<Base>("Robot")->saySomething();
00199     }
00200   }
00201   catch(class_loader::ClassLoaderException& e)
00202   {
00203     FAIL() << "ClassLoaderException: " << e.what() << "\n";
00204   }
00205 
00206   SUCCEED();
00207 }
00208 
00209 TEST(MultiClassLoaderUniquePtrTest, lazyLoad)
00210 {
00211   testMultiClassLoader(true);
00212 }
00213 TEST(MultiClassLoaderUniquePtrTest, lazyLoadSecondTime)
00214 {
00215   testMultiClassLoader(true);
00216 }
00217 TEST(MultiClassLoaderUniquePtrTest, nonLazyLoad)
00218 {
00219   testMultiClassLoader(false);
00220 }
00221 TEST(MultiClassLoaderUniquePtrTest, noWarningOnLazyLoad)
00222 {
00223   try
00224   {
00225     ClassLoader::UniquePtr<Base> cat = nullptr, dog = nullptr, rob = nullptr;
00226     {
00227       class_loader::MultiLibraryClassLoader loader(true);
00228       loader.loadLibrary(LIBRARY_1);
00229       loader.loadLibrary(LIBRARY_2);
00230 
00231       cat = loader.createUniqueInstance<Base>("Cat");
00232       dog = loader.createUniqueInstance<Base>("Dog");
00233       rob = loader.createUniqueInstance<Base>("Robot");
00234     }
00235     cat->saySomething();
00236     dog->saySomething();
00237     rob->saySomething();
00238   }
00239   catch(class_loader::ClassLoaderException& e)
00240   {
00241     FAIL() << "ClassLoaderException: " << e.what() << "\n";
00242   }
00243 
00244   SUCCEED();
00245 }
00246 
00247 /*****************************************************************************/
00248 
00249 // Run all the tests that were declared with TEST()
00250 int main(int argc, char **argv){
00251   testing::InitGoogleTest(&argc, argv);
00252   return RUN_ALL_TESTS();
00253 }
00254 
00255 


class_loader
Author(s): Mirza Shah
autogenerated on Fri Jul 28 2017 05:48:18