Wisp 1.2.0 C++ Unit Test Coverage
Current view: top level - ssrc/wisp/test - ServiceTestCase.h (source / functions) Hit Total Coverage
Test: Wisp 1.2.0 C++ Unit Tests Lines: 25 26 96.2 %
Date: 2010-05-26 Functions: 18 20 90.0 %
Branches: 40 80 50.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2006-2008 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     http://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : #ifndef __SSRC_WISP_TEST_SERVICE_TEST_CASE_H
      18                 :            : #define __SSRC_WISP_TEST_SERVICE_TEST_CASE_H
      19                 :            : 
      20                 :            : #include <ssrc/wisp/service/service.h>
      21                 :            : #include <ssrc/wisp/utility/service_main.h>
      22                 :            : #include <ssrc/wisp/test/test_main.h>
      23                 :            : 
      24                 :            : #include <boost/bind.hpp>
      25                 :            : #include <boost/thread/thread.hpp>
      26                 :            : 
      27                 :            : __BEGIN_NS_SSRC_WISP_TEST
      28                 :            : 
      29                 :            : using namespace NS_SSRC_WISP;
      30                 :            : using NS_SSRC_WISP_SERVICE::ServiceEventHandler;
      31                 :            : using NS_SSRC_WISP_SERVICE::Service;
      32                 :            : 
      33                 :            : template<typename ProtocolProcessor,
      34                 :            :          template <typename PP> class EH = ServiceEventHandler>
      35                 :          3 : class TestService : public Service<EH<ProtocolProcessor> > {
      36                 :            :   typedef Service<EH<ProtocolProcessor> > super;
      37                 :            : 
      38                 :            : public:
      39                 :            : 
      40                 :          3 :   explicit TestService(const string & connection = "",
      41                 :            :                        const string & name = "", 
      42                 :            :                        const unsigned int message_capacity =
      43                 :            :                        NS_SSRC_SPREAD::Message::DefaultCapacity) :
      44                 :          3 :     super(connection, name, message_capacity)
      45                 :          3 :   { }
      46                 :            : 
      47                 :            :   template<typename Initializer>
      48                 :            :   explicit TestService(const Initializer & initializer,
      49                 :            :                        const string & connection = "",
      50                 :            :                        const string & name = "", 
      51                 :            :                        const unsigned int message_capacity =
      52                 :            :                        NS_SSRC_SPREAD::Message::DefaultCapacity) :
      53                 :            :     super(initializer, connection, name, message_capacity)
      54                 :            :   { }
      55                 :            : 
      56                 :            :   using super::protocol;
      57                 :            : };
      58                 :            : 
      59                 :            : // Destructor fires on cppunit exception to terminate thread.
      60                 :            : template<typename service_type, typename CallStop>
      61                 :            : struct ServiceThread : public service_type::caller_type {
      62                 :            :   typedef typename service_type::caller_type super;
      63                 :            :   typedef NS_SSRC_WISP_SERVICE::EventLoop loop_type;
      64                 :            :   typedef boost::shared_ptr<loop_type> loop_ptr;
      65                 :            :   typedef boost::shared_ptr<boost::thread> thread_ptr;
      66                 :            : 
      67                 :            :   WISP_IMPORT_T(service_type, protocol_processor);
      68                 :            : 
      69                 :            :   service_type & serv;
      70                 :            :   thread_ptr thread;
      71                 :            :   loop_ptr loop;
      72                 :            : 
      73                 :          3 :   ServiceThread(service_type & serv) :
      74                 :            :     super(WISP_SPREAD_DAEMON),
      75 [ +  - ][ +  - ]:          3 :     serv(serv), thread(), loop(new loop_type)
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
      76                 :            :   {
      77         [ +  - ]:          3 :     serv.start(*loop, 300);
      78 [ +  - ][ +  - ]:          3 :     thread =
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
                 [ +  - ]
      79                 :            :       thread_ptr(new boost::thread(boost::bind(&loop_type::start, loop)));
      80 [ +  - ][ -  + ]:          3 :     while(serv.state() != protocol_processor::Started)
      81         [ #  # ]:          0 :       boost::thread::yield();    
      82                 :          3 :   }
      83                 :            : 
      84                 :          3 :   ~ServiceThread() {
      85 [ +  - ][ +  - ]:          3 :     super::template send<CallStop>(serv.name());
      86         [ +  - ]:          3 :     thread->join();
      87 [ +  - ][ +  - ]:          3 :   }
      88                 :            : };
      89                 :            : 
      90         [ +  - ]:          3 : struct TestOptions {
      91                 :            :   string connection;
      92                 :            :   string name;
      93                 :            :   unsigned int capacity;
      94                 :            : 
      95                 :          3 :   TestOptions() :
      96                 :            :     connection(WISP_SPREAD_DAEMON), name(),
      97 [ +  - ][ +  - ]:          3 :     capacity(NS_SSRC_SPREAD::Message::DefaultCapacity)
      98                 :          3 :   { }
      99                 :            : };
     100                 :            : 
     101                 :            : template<typename ProtocolProcessor,
     102                 :            :          typename CallStop,
     103                 :            :          template <typename PP> class EH = ServiceEventHandler,
     104                 :            :          typename Options = TestOptions,
     105                 :            :          bool has_initializer = false>
     106 [ +  + ][ +  + ]:          6 : class ServiceTestCase : public TestCase {
         [ -  + ][ +  - ]
            [ +  - ][ - ]
            [ - ][ +  - ]
         [ +  - ][ -  + ]
     107                 :            : protected:
     108                 :            :   typedef ProtocolProcessor protocol_processor;
     109                 :            :   //typedef EH<protocol_processor> event_handler;
     110                 :            :   typedef TestService<protocol_processor, EH> service_type;
     111                 :            :   typedef ServiceThread<service_type, CallStop> service_thread;
     112                 :            :   typedef
     113                 :            :   utility::ServiceFactory<service_type, Options, has_initializer>
     114                 :            :   service_factory;
     115                 :            : 
     116                 :            :   Options options;
     117                 :            :   service_factory new_service;
     118                 :            :   typename service_factory::service_ptr serv;
     119                 :            : 
     120                 :            : public:
     121                 :            : 
     122                 :          3 :   virtual void setUp() {
     123         [ +  - ]:          3 :     serv = new_service(options);
     124                 :          3 :   }
     125                 :            : 
     126                 :          3 :   virtual void tearDown() {
     127                 :          3 :     serv.reset();
     128                 :          3 :   }
     129                 :            : };
     130                 :            : 
     131                 :            : __END_NS_SSRC_WISP_TEST
     132                 :            : 
     133                 :            : #endif