service_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 service_test.cpp
3 
4  service_test.cpp - description
5  -------------------
6  begin : Tue September 07 2010
7  copyright : (C) 2010 The SourceWorks
8  email : peter@thesourceworks.com
9 
10  ***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 
20 
21 #include <TaskContext.hpp>
22 #include <OperationCaller.hpp>
23 #include <Operation.hpp>
24 #include <Service.hpp>
25 #include <ServiceRequester.hpp>
26 
27 #include "unit.hpp"
28 #include "operations_fixture.hpp"
29 
30 // Registers the suite into the 'registry'
32 
33 
38 BOOST_AUTO_TEST_CASE(testClientThreadCall)
39 {
40  // Tests using no caller with methods (collect is not used).
42  m0 = tc->provides("methods");
43  OperationCaller<double(int)> m1("m1", tc->provides("methods"));
44  OperationCaller<double(int,double)> m2( tc->provides("methods")->getOperation("m2") );
45  OperationCaller<double(int,double,bool)> m3("m3", tc->provides("methods"));
46  OperationCaller<double(int,double,bool,std::string)> m4("m4", tc->provides("methods"));
47 
48  BOOST_CHECK_EQUAL( -1.0, m0() );
49  BOOST_CHECK_EQUAL( -2.0, m1(1) );
50  BOOST_CHECK_EQUAL( -3.0, m2(1, 2.0) );
51  BOOST_CHECK_EQUAL( -4.0, m3(1, 2.0, true) );
52  BOOST_CHECK_EQUAL( -5.0, m4(1, 2.0, true,"hello") );
53 
54  // Tests using a caller
55  m0.setCaller( caller->engine() );
56  m1.setCaller( caller->engine() );
57  m2.setCaller( caller->engine() );
58  m3.setCaller( caller->engine() );
59  m4.setCaller( caller->engine() );
60 
61  BOOST_CHECK_EQUAL( -1.0, m0() );
62  BOOST_CHECK_EQUAL( -2.0, m1(1) );
63  BOOST_CHECK_EQUAL( -3.0, m2(1, 2.0) );
64  BOOST_CHECK_EQUAL( -4.0, m3(1, 2.0, true) );
65  BOOST_CHECK_EQUAL( -5.0, m4(1, 2.0, true,"hello") );
66 
67 }
68 
69 BOOST_AUTO_TEST_CASE(testOwnThreadOperationCallerComponentCall)
70 {
71  // test using initial caller
72  OperationCaller<double(void)> o0("o0", caller->engine() );
73  o0 = tc->provides("methods");
74  OperationCaller<double(int)> o1("o1", tc->provides("methods"), caller->engine() );
75  OperationCaller<double(int,double)> o2( tc->provides("methods")->getOperation("o2"), caller->engine() );
76  OperationCaller<double(int,double,bool)> o3("o3", tc->provides("methods"), caller->engine() );
77 
78  BOOST_REQUIRE( tc->isRunning() );
79 
80  BOOST_REQUIRE( o0.ready() );
81  BOOST_REQUIRE( o1.ready() );
82  BOOST_REQUIRE( o2.ready() );
83  BOOST_REQUIRE( o3.ready() );
84 
85  BOOST_CHECK_EQUAL( -1.0, o0() );
86  BOOST_CHECK_EQUAL( -2.0, o1(1) );
87  BOOST_CHECK_EQUAL( -3.0, o2(1, 2.0) );
88  BOOST_CHECK_EQUAL( -4.0, o3(1, 2.0, true) );
89 
90  // stress test sending operations while the receiving component is being
91  // started and stopped:
92  SendHandle<double(void)> sh0 = o0.send();
93  SendHandle<double(int)> sh1 = o1.send(1);
94  SendHandle<double(int,double)> sh2 = o2.send(1,2.0);
95  SendHandle<double(int,double,bool)> sh3 = o3.send(1,2.0,true);
96  double foo;
97  while ( sh0.collectIfDone(foo) != SendSuccess || sh1.collectIfDone(foo) != SendSuccess || sh2.collectIfDone(foo) != SendSuccess || sh3.collectIfDone(foo) != SendSuccess ) {
98  cout << "doing it " << endl;
99  tc->stop();
100  tc->start();
101  }
102  // Tests setting the caller
103  o0.setCaller( caller->engine() );
104  o1.setCaller( caller->engine() );
105  o2.setCaller( caller->engine() );
106  o3.setCaller( caller->engine() );
107 
108  sh0 = o0.send();
109  sh1 = o1.send(1);
110  sh2 = o2.send(1,2.0);
111  sh3 = o3.send(1,2.0,true);
112  while ( sh0.collectIfDone(foo) != SendSuccess || sh1.collectIfDone(foo) != SendSuccess || sh2.collectIfDone(foo) != SendSuccess || sh3.collectIfDone(foo) != SendSuccess ) {
113  cout << "doing it " << endl;
114  tc->stop();
115  tc->start();
116  }
117 
118  BOOST_REQUIRE( caller->isRunning() );
119  BOOST_CHECK_EQUAL( -1.0, o0() );
120  BOOST_CHECK_EQUAL( -2.0, o1(1) );
121  BOOST_CHECK_EQUAL( -3.0, o2(1, 2.0) );
122  BOOST_CHECK_EQUAL( -4.0, o3(1, 2.0, true) );
123 }
124 
125 BOOST_AUTO_TEST_CASE(testClientThreadOperationCallerSend)
126 {
127  // test using initial caller
128  OperationCaller<double(void)> m0("m0", caller->engine() );
129  m0 = tc->provides("methods");
130  OperationCaller<double(int)> m1("m1", tc->provides("methods"), caller->engine() );
131  OperationCaller<double(int,double)> m2( tc->provides("methods")->getOperation("m2"), caller->engine() );
132  OperationCaller<double(int,double,bool)> m3("m3", tc->provides("methods"), caller->engine() );
133  OperationCaller<double(int,double,bool,std::string)> m4("m4", tc->provides("methods"), caller->engine() );
134 
135  BOOST_REQUIRE( tc->isRunning() );
136  SendHandle<double(void)> h0 = m0.send();
137  SendHandle<double(int)> h1 = m1.send(1);
138  SendHandle<double(int,double)> h2 = m2.send(1, 2.0);
139  SendHandle<double(int,double,bool)> h3 = m3.send(1, 2.0, true);
140  SendHandle<double(int,double,bool,std::string)> h4 = m4.send(1, 2.0, true,"hello");
141 
142  double retn=0;
143  BOOST_CHECK_EQUAL( SendSuccess, h0.collect(retn) );
144  BOOST_CHECK_EQUAL( retn, -1.0 );
145  BOOST_CHECK_EQUAL( SendSuccess, h1.collect(retn) );
146  BOOST_CHECK_EQUAL( retn, -2.0 );
147  BOOST_CHECK_EQUAL( SendSuccess, h2.collect(retn) );
148  BOOST_CHECK_EQUAL( retn, -3.0 );
149  BOOST_CHECK_EQUAL( SendSuccess, h3.collect(retn) );
150  BOOST_CHECK_EQUAL( retn, -4.0 );
151  BOOST_CHECK_EQUAL( SendSuccess, h4.collect(retn) );
152  BOOST_CHECK_EQUAL( retn, -5.0 );
153 
154  // collectIfDone will certainly succeed after collect
155  BOOST_CHECK_EQUAL( SendSuccess, h0.collectIfDone(retn) );
156  BOOST_CHECK_EQUAL( retn, -1.0 );
157  BOOST_CHECK_EQUAL( SendSuccess, h1.collectIfDone(retn) );
158  BOOST_CHECK_EQUAL( retn, -2.0 );
159  BOOST_CHECK_EQUAL( SendSuccess, h2.collectIfDone(retn) );
160  BOOST_CHECK_EQUAL( retn, -3.0 );
161  BOOST_CHECK_EQUAL( SendSuccess, h3.collectIfDone(retn) );
162  BOOST_CHECK_EQUAL( retn, -4.0 );
163  BOOST_CHECK_EQUAL( SendSuccess, h4.collectIfDone(retn) );
164  BOOST_CHECK_EQUAL( retn, -5.0 );
165 
166  // the return value api.
167  BOOST_CHECK_EQUAL( -1.0, h0.ret() );
168  BOOST_CHECK_EQUAL( -2.0, h1.ret(1) );
169  BOOST_CHECK_EQUAL( -3.0, h2.ret(1, 2.0) );
170  BOOST_CHECK_EQUAL( -4.0, h3.ret(1, 2.0, true) );
171  BOOST_CHECK_EQUAL( -5.0, h4.ret(1, 2.0, true,"hello") );
172  BOOST_CHECK_EQUAL( -2.0, h1.ret() );
173  BOOST_CHECK_EQUAL( -3.0, h2.ret() );
174  BOOST_CHECK_EQUAL( -4.0, h3.ret() );
175  BOOST_CHECK_EQUAL( -5.0, h4.ret() );
176 
177  // Tests using a caller
178  m0.setCaller( caller->engine() );
179  m1.setCaller( caller->engine() );
180  m2.setCaller( caller->engine() );
181  m3.setCaller( caller->engine() );
182  m4.setCaller( caller->engine() );
183 
184  BOOST_REQUIRE( caller->isRunning() );
185  BOOST_REQUIRE( tc->isRunning() );
186  h0 = m0.send();
187  h1 = m1.send(1);
188  h2 = m2.send(1, 2.0);
189  h3 = m3.send(1, 2.0, true);
190  h4 = m4.send(1, 2.0, true,"hello");
191 
192  retn=0;
193  BOOST_CHECK_EQUAL( SendSuccess, h0.collect(retn) );
194  BOOST_CHECK_EQUAL( retn, -1.0 );
195  BOOST_CHECK_EQUAL( SendSuccess, h1.collect(retn) );
196  BOOST_CHECK_EQUAL( retn, -2.0 );
197  BOOST_CHECK_EQUAL( SendSuccess, h2.collect(retn) );
198  BOOST_CHECK_EQUAL( retn, -3.0 );
199  BOOST_CHECK_EQUAL( SendSuccess, h3.collect(retn) );
200  BOOST_CHECK_EQUAL( retn, -4.0 );
201  BOOST_CHECK_EQUAL( SendSuccess, h4.collect(retn) );
202  BOOST_CHECK_EQUAL( retn, -5.0 );
203 
204  // collectIfDone will certainly succeed after collect
205  BOOST_CHECK_EQUAL( SendSuccess, h0.collectIfDone(retn) );
206  BOOST_CHECK_EQUAL( retn, -1.0 );
207  BOOST_CHECK_EQUAL( SendSuccess, h1.collectIfDone(retn) );
208  BOOST_CHECK_EQUAL( retn, -2.0 );
209  BOOST_CHECK_EQUAL( SendSuccess, h2.collectIfDone(retn) );
210  BOOST_CHECK_EQUAL( retn, -3.0 );
211  BOOST_CHECK_EQUAL( SendSuccess, h3.collectIfDone(retn) );
212  BOOST_CHECK_EQUAL( retn, -4.0 );
213  BOOST_CHECK_EQUAL( SendSuccess, h4.collectIfDone(retn) );
214  BOOST_CHECK_EQUAL( retn, -5.0 );
215 
216  // the return value api.
217  BOOST_CHECK_EQUAL( -1.0, h0.ret() );
218  BOOST_CHECK_EQUAL( -2.0, h1.ret(1) );
219  BOOST_CHECK_EQUAL( -3.0, h2.ret(1, 2.0) );
220  BOOST_CHECK_EQUAL( -4.0, h3.ret(1, 2.0, true) );
221  BOOST_CHECK_EQUAL( -5.0, h4.ret(1, 2.0, true,"hello") );
222  BOOST_CHECK_EQUAL( -2.0, h1.ret() );
223  BOOST_CHECK_EQUAL( -3.0, h2.ret() );
224  BOOST_CHECK_EQUAL( -4.0, h3.ret() );
225  BOOST_CHECK_EQUAL( -5.0, h4.ret() );
226 }
227 
228 BOOST_AUTO_TEST_CASE(testOwnThreadOperationCallerSend)
229 {
230  // test using initial caller
231  OperationCaller<double(void)> m0("o0", caller->engine() );
232  m0 = tc->provides("methods");
233  OperationCaller<double(int)> m1("o1", tc->provides("methods"), caller->engine() );
234  OperationCaller<double(int,double)> m2( tc->provides("methods")->getOperation("o2"), caller->engine() );
235  OperationCaller<double(int,double,bool)> m3("o3", tc->provides("methods"), caller->engine() );
236  OperationCaller<double(int,double,bool,std::string)> m4("o4", tc->provides("methods"), caller->engine() );
237 
238  BOOST_REQUIRE( tc->isRunning() );
239  SendHandle<double(void)> h0 = m0.send();
240  SendHandle<double(int)> h1 = m1.send(1);
241  SendHandle<double(int,double)> h2 = m2.send(1, 2.0);
242  SendHandle<double(int,double,bool)> h3 = m3.send(1, 2.0, true);
243  SendHandle<double(int,double,bool,std::string)> h4 = m4.send(1, 2.0, true,"hello");
244 
245  double retn=0;
246  BOOST_CHECK_EQUAL( SendSuccess, h0.collect(retn) );
247  BOOST_CHECK_EQUAL( retn, -1.0 );
248  BOOST_CHECK_EQUAL( SendSuccess, h1.collect(retn) );
249  BOOST_CHECK_EQUAL( retn, -2.0 );
250  BOOST_CHECK_EQUAL( SendSuccess, h2.collect(retn) );
251  BOOST_CHECK_EQUAL( retn, -3.0 );
252  BOOST_CHECK_EQUAL( SendSuccess, h3.collect(retn) );
253  BOOST_CHECK_EQUAL( retn, -4.0 );
254  BOOST_CHECK_EQUAL( SendSuccess, h4.collect(retn) );
255  BOOST_CHECK_EQUAL( retn, -5.0 );
256 
257  // collectIfDone will certainly succeed after collect
258  BOOST_CHECK_EQUAL( SendSuccess, h0.collectIfDone(retn) );
259  BOOST_CHECK_EQUAL( retn, -1.0 );
260  BOOST_CHECK_EQUAL( SendSuccess, h1.collectIfDone(retn) );
261  BOOST_CHECK_EQUAL( retn, -2.0 );
262  BOOST_CHECK_EQUAL( SendSuccess, h2.collectIfDone(retn) );
263  BOOST_CHECK_EQUAL( retn, -3.0 );
264  BOOST_CHECK_EQUAL( SendSuccess, h3.collectIfDone(retn) );
265  BOOST_CHECK_EQUAL( retn, -4.0 );
266  BOOST_CHECK_EQUAL( SendSuccess, h4.collectIfDone(retn) );
267  BOOST_CHECK_EQUAL( retn, -5.0 );
268 
269  // the return value api.
270  BOOST_CHECK_EQUAL( -1.0, h0.ret() );
271  BOOST_CHECK_EQUAL( -2.0, h1.ret(1) );
272  BOOST_CHECK_EQUAL( -3.0, h2.ret(1, 2.0) );
273  BOOST_CHECK_EQUAL( -4.0, h3.ret(1, 2.0, true) );
274  BOOST_CHECK_EQUAL( -5.0, h4.ret(1, 2.0, true,"hello") );
275  BOOST_CHECK_EQUAL( -2.0, h1.ret() );
276  BOOST_CHECK_EQUAL( -3.0, h2.ret() );
277  BOOST_CHECK_EQUAL( -4.0, h3.ret() );
278  BOOST_CHECK_EQUAL( -5.0, h4.ret() );
279 
280  // Tests using a caller
281  m0.setCaller( caller->engine() );
282  m1.setCaller( caller->engine() );
283  m2.setCaller( caller->engine() );
284  m3.setCaller( caller->engine() );
285  m4.setCaller( caller->engine() );
286 
287  BOOST_REQUIRE( caller->isRunning() );
288  BOOST_REQUIRE( tc->isRunning() );
289  h0 = m0.send();
290  h1 = m1.send(1);
291  h2 = m2.send(1, 2.0);
292  h3 = m3.send(1, 2.0, true);
293  h4 = m4.send(1, 2.0, true,"hello");
294 
295  retn=0;
296  BOOST_CHECK_EQUAL( SendSuccess, h0.collect(retn) );
297  BOOST_CHECK_EQUAL( retn, -1.0 );
298  BOOST_CHECK_EQUAL( SendSuccess, h1.collect(retn) );
299  BOOST_CHECK_EQUAL( retn, -2.0 );
300  BOOST_CHECK_EQUAL( SendSuccess, h2.collect(retn) );
301  BOOST_CHECK_EQUAL( retn, -3.0 );
302  BOOST_CHECK_EQUAL( SendSuccess, h3.collect(retn) );
303  BOOST_CHECK_EQUAL( retn, -4.0 );
304  BOOST_CHECK_EQUAL( SendSuccess, h4.collect(retn) );
305  BOOST_CHECK_EQUAL( retn, -5.0 );
306 
307  // collectIfDone will certainly succeed after collect
308  BOOST_CHECK_EQUAL( SendSuccess, h0.collectIfDone(retn) );
309  BOOST_CHECK_EQUAL( retn, -1.0 );
310  BOOST_CHECK_EQUAL( SendSuccess, h1.collectIfDone(retn) );
311  BOOST_CHECK_EQUAL( retn, -2.0 );
312  BOOST_CHECK_EQUAL( SendSuccess, h2.collectIfDone(retn) );
313  BOOST_CHECK_EQUAL( retn, -3.0 );
314  BOOST_CHECK_EQUAL( SendSuccess, h3.collectIfDone(retn) );
315  BOOST_CHECK_EQUAL( retn, -4.0 );
316  BOOST_CHECK_EQUAL( SendSuccess, h4.collectIfDone(retn) );
317  BOOST_CHECK_EQUAL( retn, -5.0 );
318 
319  // the return value api.
320  BOOST_CHECK_EQUAL( -1.0, h0.ret() );
321  BOOST_CHECK_EQUAL( -2.0, h1.ret(1) );
322  BOOST_CHECK_EQUAL( -3.0, h2.ret(1, 2.0) );
323  BOOST_CHECK_EQUAL( -4.0, h3.ret(1, 2.0, true) );
324  BOOST_CHECK_EQUAL( -5.0, h4.ret(1, 2.0, true,"hello") );
325  BOOST_CHECK_EQUAL( -2.0, h1.ret() );
326  BOOST_CHECK_EQUAL( -3.0, h2.ret() );
327  BOOST_CHECK_EQUAL( -4.0, h3.ret() );
328  BOOST_CHECK_EQUAL( -5.0, h4.ret() );
329 
330 }
331 
332 BOOST_AUTO_TEST_CASE(testOwnThreadOperationCallerSend_ChangePolicy)
333 {
334  // Tests changing the policy later on
335  OperationCaller<double(void)> m0("o0", caller->engine() );
336 
337  // set to ClientThread and send/collect it:
338  tc->provides("methods")->setOperationThread("o0",ClientThread);
339  m0 = tc->provides("methods");
340 
341  BOOST_REQUIRE( tc->isRunning() );
342  SendHandle<double(void)> h0 = m0.send();
343 
344  double retn=0;
345  BOOST_CHECK_EQUAL( SendSuccess, h0.collect(retn) );
346  BOOST_CHECK_EQUAL( retn, -1.0 );
347 
348  // set to OwnThread and send/collect it:
349  tc->provides("methods")->setOperationThread("o0",OwnThread);
350  m0 = tc->provides("methods");
351 
352  BOOST_REQUIRE( tc->isRunning() );
353  h0 = m0.send();
354 
355  retn = 0;
356  BOOST_CHECK_EQUAL( SendSuccess, h0.collect(retn) );
357  BOOST_CHECK_EQUAL( retn, -1.0 );
358 }
359 
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
BOOST_AUTO_TEST_CASE(testClientThreadCall)
#define BOOST_AUTO_TEST_SUITE_END()
void setCaller(ExecutionEngine *caller)
printstream cout
Definition: rtstreams.cpp:45
The SendHandle is used to collect the result values of an asynchronous invocation. The template argument Signature must have the same type as the method being invoked.
Definition: rtt-fwd.hpp:79
basic_ostreams & endl(basic_ostreams &s)
Definition: rtstreams.cpp:110
SendStatus collect() const
Definition: SendHandle.hpp:124
void foo(double d)


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:35