Savarese Software Research Corporation
ServiceTestCase.h
Go to the documentation of this file.
1 /* Copyright 2006-2008 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 #ifndef __SSRC_WISP_TEST_SERVICE_TEST_CASE_H
17 #define __SSRC_WISP_TEST_SERVICE_TEST_CASE_H
18 
22 
23 #include <thread>
24 
26 
27 using namespace NS_SSRC_WISP;
28 using NS_SSRC_WISP_SERVICE::ServiceEventHandler;
29 using NS_SSRC_WISP_SERVICE::Service;
31 
32 template<typename ProtocolProcessor,
33  template <typename PP, typename...> class EH = ServiceEventHandler>
34 class TestService : public Service<EH<ProtocolProcessor> > {
35  typedef Service<EH<ProtocolProcessor> > super;
36 
37 public:
38 
39  explicit TestService(const std::string & connection = "",
40  const std::string & name = "",
41  const unsigned int message_capacity =
42  NS_SSRC_SPREAD::Message::DefaultCapacity) :
43  super(connection, name, message_capacity)
44  { }
45 
46  template<typename Initializer>
47  explicit TestService(const Initializer & initializer,
48  const std::string & connection = "",
49  const std::string & name = "",
50  const unsigned int message_capacity =
51  NS_SSRC_SPREAD::Message::DefaultCapacity) :
52  super(initializer, connection, name, message_capacity)
53  { }
54 
55  using super::protocol;
56 };
57 
58 // Destructor fires on cppunit exception to terminate thread.
59 template<typename service_type, typename CallStop>
60 struct ServiceThread : public service_type::caller_type {
61  typedef typename service_type::caller_type super;
62  typedef NS_SSRC_WISP_SERVICE::EventLoop loop_type;
63  typedef std::shared_ptr<loop_type> loop_ptr;
64  typedef std::shared_ptr<std::thread> thread_ptr;
65 
66  WISP_IMPORT_T(service_type, protocol_processor);
67 
68  service_type & serv;
69  thread_ptr thread;
70  loop_ptr loop;
71 
72  ServiceThread(service_type & serv) :
73  super(WISP_SPREAD_DAEMON),
74  serv(serv), thread(), loop(make_smart_ptr<loop_ptr>())
75  {
76  serv.start(*loop, 300);
77  thread =
78  thread_ptr(make_smart_ptr<thread_ptr>(std::bind(&loop_type::start,
79  loop)));
80  while(serv.state() != protocol_processor::Started) {
81  std::this_thread::yield();
82  }
83  }
84 
86  super::template send<CallStop>(serv.name());
87  thread->join();
88  }
89 };
90 
91 struct TestOptions {
92  std::string connection;
93  std::string name;
94  unsigned int capacity;
95 
97  connection(WISP_SPREAD_DAEMON), name(),
98  capacity(NS_SSRC_SPREAD::Message::DefaultCapacity)
99  { }
100 };
101 
102 template<typename ProtocolProcessor,
103  typename CallStop,
104  template <typename PP, typename...> class EH = ServiceEventHandler,
105  typename Options = TestOptions,
106  bool has_initializer = false>
107 class ServiceTestCase : public TestCase {
108 protected:
109  typedef ProtocolProcessor protocol_processor;
110  //typedef EH<protocol_processor> event_handler;
113  typedef
114  utility::ServiceFactory<service_type, Options, has_initializer>
116 
117  Options options;
119  typename service_factory::service_ptr serv;
120 
121 public:
122 
123  virtual void setUp() {
124  serv = new_service(options);
125  }
126 
127  virtual void tearDown() {
128  serv.reset();
129  }
130 };
131 
133 
134 #endif

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