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 <thread>
27 : :
28 : : __BEGIN_NS_SSRC_WISP_TEST
29 : :
30 : : using namespace NS_SSRC_WISP;
31 : : using NS_SSRC_WISP_SERVICE::ServiceEventHandler;
32 : : using NS_SSRC_WISP_SERVICE::Service;
33 : : using NS_SSRC_WISP_UTILITY::make_smart_ptr;
34 : :
35 : : template<typename ProtocolProcessor,
36 : : template <typename PP, typename...> 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 std::shared_ptr<loop_type> loop_ptr;
67 : : typedef std::shared_ptr<std::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(make_smart_ptr<loop_ptr>())
+ - + - ]
78 : : {
79 [ + - ]: 3 : serv.start(*loop, 300);
80 [ + - + - ]: 3 : thread = make_smart_ptr<thread_ptr>(std::bind(&loop_type::start, loop));
81 : :
82 [ + - - + ]: 3 : while(serv.state() != protocol_processor::Started) {
83 : 0 : std::this_thread::yield();
84 : : }
85 : 3 : }
86 : :
87 : 3 : ~ServiceThread() {
88 : 3 : super::template send<CallStop>(serv.name());
89 : 3 : thread->join();
90 : 3 : }
91 : : };
92 : :
93 : 3 : struct TestOptions {
94 : : const std::string connection;
95 : : const std::string name;
96 : : const unsigned int capacity;
97 : :
98 : 3 : TestOptions() :
99 : : connection(WISP_SPREAD_DAEMON), name(),
100 [ + - ]: 3 : capacity(NS_SSRC_SPREAD::Message::DefaultCapacity)
101 : 3 : { }
102 : : };
103 : :
104 : : template<typename ProtocolProcessor,
105 : : typename CallStop,
106 : : template <typename PP, typename...> class EH = ServiceEventHandler,
107 : : typename Options = TestOptions,
108 : : bool has_initializer = false>
109 : : class ServiceTestFixture {
110 : : protected:
111 : : typedef ProtocolProcessor protocol_processor;
112 : : //typedef EH<protocol_processor> event_handler;
113 : : typedef TestService<protocol_processor, EH> service_type;
114 : : typedef ServiceThread<service_type, CallStop> service_thread;
115 : : typedef
116 : : utility::ServiceFactory<service_type, Options, has_initializer>
117 : : service_factory;
118 : :
119 : : Options options;
120 : : service_factory new_service;
121 : : typename service_factory::service_ptr serv;
122 : :
123 : : public:
124 : :
125 : 3 : ServiceTestFixture() {
126 [ + - ]: 3 : serv = new_service(options);
127 : 3 : }
128 : :
129 : 3 : ~ServiceTestFixture() {
130 : 3 : serv.reset();
131 : 3 : }
132 : : };
133 : :
134 : : __END_NS_SSRC_WISP_TEST
135 : :
136 : : #endif
|