|
Boost-Commit : |
From: mconsoni_at_[hidden]
Date: 2007-06-14 18:42:39
Author: mconsoni
Date: 2007-06-14 18:42:38 EDT (Thu, 14 Jun 2007)
New Revision: 7047
URL: http://svn.boost.org/trac/boost/changeset/7047
Log:
Two unit tests suggested by Janek Kozicki.
- First, we test what happens if we load two times a library with the same name but with different implementation.
We load a first version named libHelloWorldCache.extension, we use it, and then we replace this file
with another library (but leaving the same name, libHelloWorldCache.extension) and check if the returned values
are the same (cached). In my tests on Linux it worked correctly (giving two diferent results).
- Second, we test what happens if we remove a shared object before unloading the plugin. It works well on Linux
too.
Added:
sandbox/libs/extension/test/lib_caching_test.cpp
Text files modified:
sandbox/libs/extension/test/Jamfile.v2 | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
Modified: sandbox/libs/extension/test/Jamfile.v2
==============================================================================
--- sandbox/libs/extension/test/Jamfile.v2 (original)
+++ sandbox/libs/extension/test/Jamfile.v2 2007-06-14 18:42:38 EDT (Thu, 14 Jun 2007)
@@ -19,6 +19,7 @@
[ run zone_test.cpp ]
[ run construction.cpp ]
[ run hello_world_test.cpp ]
+ [ run lib_caching_test.cpp ]
[ run parameters_test.cpp ]
[ run multiple_inheritance_test.cpp ]
[ run extension_test.cpp ]
Added: sandbox/libs/extension/test/lib_caching_test.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/test/lib_caching_test.cpp 2007-06-14 18:42:38 EDT (Thu, 14 Jun 2007)
@@ -0,0 +1,151 @@
+/* (C) Copyright Mariano G. Consoni 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+#include <boost/extension/factory_map.hpp>
+#include <boost/extension/factory.hpp>
+#include <boost/extension/shared_library.hpp>
+#define BOOST_TEST_MAIN
+#define BOOST_TEST_DYN_LINK 1
+
+#include <boost/test/unit_test.hpp>
+#include <boost/filesystem/operations.hpp>
+
+#include <iostream>
+#include <string>
+
+#include "../examples/word.hpp"
+
+#define BOOST_EXTENSION_LIBS_DIRECTORY "../bin/"
+
+using namespace boost::extensions;
+
+/// tests if the SO caches libraries and if we can remove .extensions files before unloading
+
+BOOST_AUTO_TEST_CASE(lib_caching_test)
+{
+ if(boost::filesystem::exists(BOOST_EXTENSION_LIBS_DIRECTORY "libHelloWorldCache.extension")) {
+ boost::filesystem::remove(BOOST_EXTENSION_LIBS_DIRECTORY "libHelloWorldCache.extension");
+ }
+ boost::filesystem::copy_file(BOOST_EXTENSION_LIBS_DIRECTORY "libHelloWorldLib.extension",
+ BOOST_EXTENSION_LIBS_DIRECTORY "libHelloWorldCache.extension");
+
+ {
+ // load the first version
+ shared_library l((std::string("libHelloWorldCache") + ".extension").c_str());
+ BOOST_CHECK_EQUAL( l.open(), true );
+ {
+
+ // check if the factory can return the functor
+ factory_map fm;
+ functor<void, factory_map &> load_func = l.get_functor<void, factory_map &>("extension_export_word");
+ BOOST_CHECK_EQUAL( load_func.is_valid(), true );
+
+ load_func(fm);
+
+ std::list<factory<word, int> > & factory_list = fm.get<word, int>();
+ BOOST_CHECK_EQUAL( factory_list.size(), 2 );
+
+ std::list<factory<word, int> >::iterator current_word = factory_list.begin();
+
+ std::auto_ptr<word> hello_word_ptr(current_word->create());
+ BOOST_CHECK_EQUAL( !hello_word_ptr.get(), 0 );
+
+ BOOST_CHECK_EQUAL( hello_word_ptr->get_val(), "hello");
+ std::cerr << hello_word_ptr->get_val() << " " << std::endl;
+
+ ++current_word;
+
+ std::auto_ptr<word> world_word_ptr(current_word->create());
+ BOOST_CHECK_EQUAL( !world_word_ptr.get(), 0 );
+
+ BOOST_CHECK_EQUAL( world_word_ptr->get_val(), "world!");
+ std::cerr << world_word_ptr->get_val() << std::endl << std::endl;
+ }
+ l.close();
+ }
+
+ // replace the loaded library and try to reload
+ boost::filesystem::remove(BOOST_EXTENSION_LIBS_DIRECTORY "libHelloWorldCache.extension");
+ boost::filesystem::copy_file(BOOST_EXTENSION_LIBS_DIRECTORY "libHelloWorldLibv2.extension",
+ BOOST_EXTENSION_LIBS_DIRECTORY "libHelloWorldCache.extension");
+
+ {
+ // load the second version
+ shared_library l((std::string("libHelloWorldCache") + ".extension").c_str());
+ BOOST_CHECK_EQUAL( l.open(), true );
+
+ {
+ // check if the factory can return the functor
+ factory_map fm;
+ functor<void, factory_map &> load_func = l.get_functor<void, factory_map &>("extension_export_word");
+ BOOST_CHECK_EQUAL( load_func.is_valid(), true );
+
+ load_func(fm);
+
+ // check if we can get the word list
+ std::list<factory<word, int> > & factory_list = fm.get<word, int>();
+ BOOST_CHECK_EQUAL( factory_list.size(), 2 );
+
+ // iterate trough the classes and execute get_val method to obtain the correct words
+ std::list<factory<word, int> >::iterator current_word = factory_list.begin();
+
+ std::auto_ptr<word> hello_word_ptr(current_word->create());
+ BOOST_CHECK_EQUAL( !hello_word_ptr.get(), 0 );
+
+ BOOST_CHECK_EQUAL( hello_word_ptr->get_val(), "| v2 hello");
+ std::cerr << hello_word_ptr->get_val() << " " << std::endl;
+
+ ++current_word;
+
+ std::auto_ptr<word> world_word_ptr(current_word->create());
+ BOOST_CHECK_EQUAL( !world_word_ptr.get(), 0 );
+
+ BOOST_CHECK_EQUAL( world_word_ptr->get_val(), "world! v2");
+ std::cerr << world_word_ptr->get_val() << " " << std::endl;
+ }
+ l.close();
+ }
+
+ {
+ // load the library again and remove it when loaded
+ shared_library l((std::string("libHelloWorldCache") + ".extension").c_str());
+ BOOST_CHECK_EQUAL( l.open(), true );
+
+ {
+ // check if the factory can return the functor
+ factory_map fm;
+ functor<void, factory_map &> load_func = l.get_functor<void, factory_map &>("extension_export_word");
+ BOOST_CHECK_EQUAL( load_func.is_valid(), true );
+
+ load_func(fm);
+
+ // check if we can get the word list
+ std::list<factory<word, int> > & factory_list = fm.get<word, int>();
+ BOOST_CHECK_EQUAL( factory_list.size(), 2 );
+
+ // iterate trough the classes and execute get_val method to obtain the correct words
+ std::list<factory<word, int> >::iterator current_word = factory_list.begin();
+
+ std::auto_ptr<word> hello_word_ptr(current_word->create());
+ BOOST_CHECK_EQUAL( !hello_word_ptr.get(), 0 );
+
+ BOOST_CHECK_EQUAL( hello_word_ptr->get_val(), "| v2 hello");
+ std::cerr << hello_word_ptr->get_val() << " " << std::endl;
+
+ ++current_word;
+
+ std::auto_ptr<word> world_word_ptr(current_word->create());
+ BOOST_CHECK_EQUAL( !world_word_ptr.get(), 0 );
+
+ BOOST_CHECK_EQUAL( world_word_ptr->get_val(), "world! v2");
+ std::cerr << world_word_ptr->get_val() << " " << std::endl;
+
+ boost::filesystem::remove(BOOST_EXTENSION_LIBS_DIRECTORY "libHelloWorldCache.extension");
+ }
+ l.close();
+ }
+}
+
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk