Savarese Software Research Corporation
ServiceTestFixture.h
Go to the documentation of this file.
00001 /* Copyright 2006-2011 Savarese Software Research Corporation
00002  *
00003  * Licensed under the Apache License, Version 2.0 (the "License");
00004  * you may not use this file except in compliance with the License.
00005  * You may obtain a copy of the License at
00006  *
00007  *     http://www.savarese.com/software/ApacheLicense-2.0
00008  *
00009  * Unless required by applicable law or agreed to in writing, software
00010  * distributed under the License is distributed on an "AS IS" BASIS,
00011  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012  * See the License for the specific language governing permissions and
00013  * limitations under the License.
00014  */
00015 
00020 #ifndef __SSRC_WISP_TEST_SERVICE_TEST_FIXTURE_H
00021 #define __SSRC_WISP_TEST_SERVICE_TEST_FIXTURE_H
00022 
00023 #include <ssrc/wisp/service/service.h>
00024 #include <ssrc/wisp/utility/service_main.h>
00025 
00026 #include <boost/bind.hpp>
00027 #include <boost/thread/thread.hpp>
00028 
00029 __BEGIN_NS_SSRC_WISP_TEST
00030 
00031 using namespace NS_SSRC_WISP;
00032 using NS_SSRC_WISP_SERVICE::ServiceEventHandler;
00033 using NS_SSRC_WISP_SERVICE::Service;
00034 
00035 template<typename ProtocolProcessor,
00036          template <typename PP> class EH = ServiceEventHandler>
00037 class TestService : public Service<EH<ProtocolProcessor> > {
00038   typedef Service<EH<ProtocolProcessor> > super;
00039 
00040 public:
00041 
00042   explicit TestService(const std::string & connection = "",
00043                        const std::string & name = "", 
00044                        const unsigned int message_capacity =
00045                        NS_SSRC_SPREAD::Message::DefaultCapacity) :
00046     super(connection, name, message_capacity)
00047   { }
00048 
00049   template<typename Initializer>
00050   explicit TestService(const Initializer & initializer,
00051                        const std::string & connection = "",
00052                        const std::string & name = "", 
00053                        const unsigned int message_capacity =
00054                        NS_SSRC_SPREAD::Message::DefaultCapacity) :
00055     super(initializer, connection, name, message_capacity)
00056   { }
00057 
00058   using super::protocol;
00059 };
00060 
00061 // Destructor fires on exception to terminate thread.
00062 template<typename service_type, typename CallStop>
00063 struct ServiceThread : public service_type::caller_type {
00064   typedef typename service_type::caller_type super;
00065   typedef NS_SSRC_WISP_SERVICE::EventLoop loop_type;
00066   typedef boost::shared_ptr<loop_type> loop_ptr;
00067   typedef boost::shared_ptr<boost::thread> thread_ptr;
00068 
00069   WISP_IMPORT_T(service_type, protocol_processor);
00070 
00071   service_type & serv;
00072   thread_ptr thread;
00073   loop_ptr loop;
00074 
00075   ServiceThread(service_type & serv) :
00076     super(WISP_SPREAD_DAEMON),
00077     serv(serv), thread(), loop(new loop_type)
00078   {
00079     serv.start(*loop, 300);
00080     thread =
00081       thread_ptr(new boost::thread(boost::bind(&loop_type::start, loop)));
00082     while(serv.state() != protocol_processor::Started)
00083       boost::thread::yield();    
00084   }
00085 
00086   ~ServiceThread() {
00087     super::template send<CallStop>(serv.name());
00088     thread->join();
00089   }
00090 };
00091 
00092 struct TestOptions {
00093   const std::string connection;
00094   const std::string name;
00095   const unsigned int capacity;
00096 
00097   TestOptions() :
00098     connection(WISP_SPREAD_DAEMON), name(),
00099     capacity(NS_SSRC_SPREAD::Message::DefaultCapacity)
00100   { }
00101 };
00102 
00103 template<typename ProtocolProcessor,
00104          typename CallStop,
00105          template <typename PP> class EH = ServiceEventHandler,
00106          typename Options = TestOptions,
00107          bool has_initializer = false>
00108 class ServiceTestFixture {
00109 protected:
00110   typedef ProtocolProcessor protocol_processor;
00111   //typedef EH<protocol_processor> event_handler;
00112   typedef TestService<protocol_processor, EH> service_type;
00113   typedef ServiceThread<service_type, CallStop> service_thread;
00114   typedef
00115   utility::ServiceFactory<service_type, Options, has_initializer>
00116   service_factory;
00117 
00118   Options options;
00119   service_factory new_service;
00120   typename service_factory::service_ptr serv;
00121 
00122 public:
00123 
00124   ServiceTestFixture() {
00125     serv = new_service(options);
00126   }
00127 
00128   ~ServiceTestFixture() {
00129     serv.reset();
00130   }
00131 };
00132 
00133 __END_NS_SSRC_WISP_TEST
00134 
00135 #endif

Savarese Software Research Corporation
Copyright © 2006-2010 Savarese Software Research Corporation. All rights reserved.