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