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