platform/include/pion/platform/PionPlatformUnitTest.hpp

00001 // ------------------------------------------------------------------------
00002 // Pion is a development platform for building Reactors that process Events
00003 // ------------------------------------------------------------------------
00004 // Copyright (C) 2009 Atomic Labs, Inc.  (http://www.atomiclabs.com)
00005 //
00006 // Pion is free software: you can redistribute it and/or modify it under the
00007 // terms of the GNU Affero General Public License as published by the Free
00008 // Software Foundation, either version 3 of the License, or (at your option)
00009 // any later version.
00010 //
00011 // Pion is distributed in the hope that it will be useful, but WITHOUT ANY
00012 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013 // FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for
00014 // more details.
00015 //
00016 // You should have received a copy of the GNU Affero General Public License
00017 // along with Pion.  If not, see <http://www.gnu.org/licenses/>.
00018 //
00019 
00020 #ifndef __PION_PIONPLATFORMUNITTEST_HEADER__
00021 #define __PION_PIONPLATFORMUNITTEST_HEADER__
00022 
00023 #include <fstream>
00024 #include <libxml/tree.h>
00025 #include <boost/function.hpp>
00026 #include <pion/PionScheduler.hpp>
00027 #include <pion/platform/Event.hpp>
00028 #include <pion/platform/Codec.hpp>
00029 #include <pion/platform/ReactionEngine.hpp>
00030 #include <pion/platform/ConfigManager.hpp>
00031 
00033 const std::string& get_config_file_dir(void);
00034 
00036 const std::string& get_log_file_dir(void);
00037 
00038 const std::string CONFIG_FILE_DIR(get_config_file_dir());
00039 const std::string LOG_FILE_DIR(get_log_file_dir());
00040 
00041 const std::string VOCABS_CONFIG_FILE(CONFIG_FILE_DIR + "vocabularies.xml");
00042 const std::string VOCABS_TEMPLATE_FILE(CONFIG_FILE_DIR + "vocabularies.tmpl");
00043 const std::string CODECS_CONFIG_FILE(CONFIG_FILE_DIR + "codecs.xml");
00044 #ifdef PION_HAVE_JSON
00045 const std::string CODECS_TEMPLATE_FILE(CONFIG_FILE_DIR + "codecs.tmpl");
00046 #else
00047 const std::string CODECS_TEMPLATE_FILE(CONFIG_FILE_DIR + "codecs-no-json.tmpl");
00048 #endif
00049 const std::string DATABASES_CONFIG_FILE(CONFIG_FILE_DIR + "databases.xml");
00050 const std::string DATABASES_TEMPLATE_FILE(CONFIG_FILE_DIR + "databases.tmpl");
00051 const std::string REACTORS_CONFIG_FILE(CONFIG_FILE_DIR + "reactors.xml");
00052 const std::string REACTORS_TEMPLATE_FILE(CONFIG_FILE_DIR + "reactors.tmpl");
00053 const std::string PROTOCOLS_CONFIG_FILE(CONFIG_FILE_DIR + "protocols.xml");
00054 const std::string PROTOCOLS_TEMPLATE_FILE(CONFIG_FILE_DIR + "protocols.tmpl");
00055 const std::string USERS_CONFIG_FILE(CONFIG_FILE_DIR + "users.xml");
00056 const std::string USERS_TEMPLATE_FILE(CONFIG_FILE_DIR + "users.tmpl");
00057 const std::string SERVICES_CONFIG_FILE(CONFIG_FILE_DIR + "services.xml");
00058 const std::string SERVICES_TEMPLATE_FILE(CONFIG_FILE_DIR + "services.tmpl");
00059 const std::string PLATFORM_CONFIG_FILE(CONFIG_FILE_DIR + "platform.xml");
00060 const std::string PLATFORM_TEMPLATE_FILE(CONFIG_FILE_DIR + "platform.tmpl");
00061 const std::string DBENGINES_CONFIG_FILE(CONFIG_FILE_DIR + "dbengines.xml");
00062 const std::string DBENGINES_TEMPLATE_FILE(CONFIG_FILE_DIR + "dbengines.tmpl");
00063 
00064 
00065 struct PionPlatformUnitTest {
00066 
00067     static void checkReactorEventsIn(pion::platform::ReactionEngine& reaction_engine,
00068         const std::string& reactor_id, const boost::uint64_t expected_value,
00069         const boost::uint32_t wait_seconds = 1)
00070     {
00071         // wait up to one second for the number to exceed the expected value
00072         const int num_checks_allowed = 10 * wait_seconds;
00073         for (int i = 0; i < num_checks_allowed; ++i) {
00074             pion::PionScheduler::sleep(0, 100000000); // 0.1 seconds
00075             if (reaction_engine.getEventsIn(reactor_id) >= expected_value) break;
00076         }
00077         BOOST_REQUIRE_GE( reaction_engine.getEventsIn(reactor_id), expected_value );
00078     }
00079 
00080     static void checkReactorEventsOut(pion::platform::ReactionEngine& reaction_engine,
00081         const std::string& reactor_id, const boost::uint64_t expected_value,
00082         const boost::uint32_t wait_seconds = 1)
00083     {
00084         // wait up to one second for the number to exceed the expected value
00085         const int num_checks_allowed = 10 * wait_seconds;
00086         for (int i = 0; i < num_checks_allowed; ++i) {
00087             pion::PionScheduler::sleep(0, 100000000); // 0.1 seconds
00088             if (reaction_engine.getEventsOut(reactor_id) >= expected_value) break;
00089         }
00090         BOOST_REQUIRE_GE( reaction_engine.getEventsOut(reactor_id), expected_value );
00091     }
00092     
00093     static boost::uint64_t feedFileToReactor(pion::platform::ReactionEngine& reaction_engine,
00094         const std::string& reactor_id, pion::platform::Codec& codec_ref, const std::string& log_file)
00095     {
00096         std::ifstream in(log_file.c_str(), std::ios::in);
00097         BOOST_REQUIRE(in.is_open());
00098     
00099         boost::uint64_t events_read = 0;
00100         pion::platform::EventPtr event_ptr;
00101         pion::platform::EventFactory event_factory;
00102 
00103         // push events from the log file into the clickstream sessionizer reactor
00104         event_factory.create(event_ptr, codec_ref.getEventType());
00105         while (codec_ref.read(in, *event_ptr)) {
00106             ++events_read;
00107             reaction_engine.send(reactor_id, event_ptr);
00108             event_factory.create(event_ptr, codec_ref.getEventType());
00109         }
00110         
00111         in.close();
00112     
00113         return events_read;
00114     }
00115 
00116     // From a string representation of a Reactor configuration, obtain an xmlNodePtr that
00117     // points to a list of all the child nodes needed by Reactor::setConfig().
00118     static xmlNodePtr makeReactorConfigFromString(const std::string& inner_config_str) {
00119         std::string config_str = std::string("<PionConfig><Reactor>") + inner_config_str + "</Reactor></PionConfig>";
00120         xmlNodePtr config_ptr = pion::platform::ConfigManager::createResourceConfig("Reactor", config_str.c_str(), config_str.size());
00121         BOOST_REQUIRE(config_ptr);
00122         return config_ptr;
00123     }
00124 };
00125 
00126 #endif

Generated on Wed Apr 13 16:38:34 2011 for pion-platform by  doxygen 1.4.7