Savarese Software Research Corporation
ServiceTestFixture.h
Go to the documentation of this file.
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 
20 #ifndef __SSRC_WISP_TEST_SERVICE_TEST_FIXTURE_H
21 #define __SSRC_WISP_TEST_SERVICE_TEST_FIXTURE_H
22 
25 
26 #include <thread>
27 
29 
30 using namespace NS_SSRC_WISP;
31 using NS_SSRC_WISP_SERVICE::ServiceEventHandler;
32 using NS_SSRC_WISP_SERVICE::Service;
34 
35 template<typename ProtocolProcessor,
36  template <typename PP, typename...> class EH = ServiceEventHandler>
37 class TestService : public Service<EH<ProtocolProcessor> > {
38  typedef Service<EH<ProtocolProcessor> > super;
39 
40 public:
41 
42  explicit TestService(const std::string & connection = "",
43  const std::string & name = "",
44  const unsigned int message_capacity =
45  NS_SSRC_SPREAD::Message::DefaultCapacity) :
46  super(connection, name, message_capacity)
47  { }
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  ServiceThread(service_type & serv) :
76  super(WISP_SPREAD_DAEMON),
77  serv(serv), thread(), loop(make_smart_ptr<loop_ptr>())
78  {
79  serv.start(*loop, 300);
80  thread = make_smart_ptr<thread_ptr>(std::bind(&loop_type::start, loop));
81 
82  while(serv.state() != protocol_processor::Started) {
83  std::this_thread::yield();
84  }
85  }
86 
88  super::template send<CallStop>(serv.name());
89  thread->join();
90  }
91 };
92 
93 struct TestOptions {
94  const std::string connection;
95  const std::string name;
96  const unsigned int capacity;
97 
99  connection(WISP_SPREAD_DAEMON), name(),
100  capacity(NS_SSRC_SPREAD::Message::DefaultCapacity)
101  { }
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>
110 protected:
111  typedef ProtocolProcessor protocol_processor;
112  //typedef EH<protocol_processor> event_handler;
115  typedef
116  utility::ServiceFactory<service_type, Options, has_initializer>
118 
119  Options options;
121  typename service_factory::service_ptr serv;
122 
123 public:
124 
126  serv = new_service(options);
127  }
128 
130  serv.reset();
131  }
132 };
133 
135 
136 #endif

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