event_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jan 10 15:59:51 CET 2005 event_test.cpp
3 
4  event_test.cpp - description
5  -------------------
6  begin : Mon January 10 2005
7  copyright : (C) 2005 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
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 #include "unit.hpp"
20 
21 #include <internal/Signal.hpp>
22 #include <extras/SlaveActivity.hpp>
23 #include <string>
24 #include <rtt-detail-fwd.hpp>
25 #include <Logger.hpp>
29 #include <Activity.hpp>
30 #include <os/Atomic.hpp>
31 
32 #include <boost/bind.hpp>
33 #include <boost/scoped_ptr.hpp>
34 
35 using namespace RTT;
36 using namespace boost;
37 using namespace RTT::detail;
38 
39 
40 
41 struct Runner : public RunnableInterface
42 {
43  bool result;
44  int data;
47  Runner( Signal<void(int)>& e_ ) : e(e_) {
48  }
49 
50  bool initialize() {
51  result = false;
52  // connect sync and async handler with event
53  // and run async handler in thread of this task.
54  h1 = e.connect( boost::bind(&Runner::handle,this, _1) );
55  return true;
56  }
57  void step() {
58  e( 123456 );
59  }
60 
61  // blocking implementation
62  void loop() {
63  e( 123456 );
64  }
65 
66  bool breakLoop() {
67  return true;
68  }
69 
70  void finalize() {
71  h1.disconnect();
72  }
73 
74  void handle(int i) {
75  data = i;
76  if (data == 123456)
77  result = true;
78  else
79  result = false;
80  }
81 };
82 
83 struct SelfRemover : public RunnableInterface
84 {
87  SelfRemover( Signal<void(void)>& e_ ) : e(e_) {
88  }
89 
90  bool initialize() {
91  // connect sync and async handler with event
92  // and run async handler in thread of this task.
93  h1 = e.setup( boost::bind(&SelfRemover::handle,this));
94  return true;
95  }
96  void step() {
97  h1.connect();
98  e();
99  }
100 
101  void finalize() {
102  }
103 
104  void handle(void) {
105  // do not emit within handle!
106  // first disconnect self, then reconnect
107  h1.disconnect();
108  h1.connect();
109  h1.disconnect();
110  }
111 };
112 
113 struct CrossRemover : public RunnableInterface
114 {
117  CrossRemover( Signal<void(void)>& e_ ) : e(e_), count(0) {
118  }
119  int count;
120 
121  bool initialize() {
122  // connect sync handler with event
123  e.connect( boost::bind(&CrossRemover::handle,this));
124  h1 = e.connect( boost::bind(&CrossRemover::handle,this));
125  e.connect( boost::bind(&CrossRemover::handle,this));
126  return true;
127  }
128  void step() {
129  // for syn :
130  count = 0;
131  e();
132  h1.disconnect(); // disconnect !
133  }
134 
135  void finalize() {
136  }
137 
138  void handle(void) {
139  if ( count == 0 ) {
140  h1.disconnect(); // remove next handler
141  }
142  if ( count == 1 ){
143  h1.connect(); // insert again
144  }
145  }
146 };
147 
149 {
150 public:
151 
156  std::string t_listener_what;
157  float float_sum;
158 public:
159 
160  EventTest(): t_listener_value(false), float_sum(0.0) {
161  reset();
162  }
163 
165  }
166 
167 
168  void listener(void)
169  {
170  t_listener_value = true;
171  }
172 
173  void listenerString(const std::string& what)
174  {
175  t_listener_what = what;
176  }
177 
178  int float_listener(float a, float b)
179  {
180  Logger::log() << Logger::Debug << "float_listener "<< a<<", "<<b<<Logger::endl;
181  float_sum += a + b;
182  return 1;
183  }
184 
185  void reset()
186  {
187  t_listener_value = false;
188  t_listener_what = "";
189  }
190 
191  void float_reference(float& f) {
192  log(Debug) << "Received f:"<<f<<endlog();
193  f = 10.0;
194  float_sum +=f ;
195  }
196 };
197 
199 
201 {
202  testConcurrentEmitHandlerCount.inc();
203 }
204 
206  :public RunnableInterface
207 {
208 public:
209  EmitAndcount(Signal<void(void)> &ev)
210  : mev(ev), count(0) {}
212  int count;
213  bool initialize() { return true;}
214  void step() {}
215  void finalize() {}
216  bool breakLoop() { return true; }
217  void loop()
218  {
219  mev();
220  ++count;
221  this->getActivity()->trigger();
222  }
223 
224 };
225 
226 BOOST_FIXTURE_TEST_SUITE( EventTestSuite, EventTest )
227 
229 {
230  t_event();
231 }
232 
234 {
235  Signal<void(int)> event;
236  Runner runobj(event);
237  SimulationActivity task(0.01, &runobj);
238  BOOST_CHECK(task.start());
239  BOOST_CHECK( SimulationThread::Instance()->run(100) );
240  BOOST_CHECK( runobj.result );
241 }
242 
243 BOOST_AUTO_TEST_CASE( testSelfRemoval )
244 {
245  SelfRemover runobj(t_event);
246  SimulationActivity task(0.01, &runobj);
247  BOOST_CHECK( task.start() );
248  BOOST_CHECK( SimulationThread::Instance()->run(100) );
249  BOOST_CHECK( task.stop() );
250 }
251 
255 BOOST_AUTO_TEST_CASE( testReference )
256 {
257  Signal<void(float&)> event;
258 
259  Handle h = event.connect( boost::bind(&testReference::float_reference, this, _1) );
260 
261  float f = 5.0;
262  event.emit( f );
263 
264  // Note: we actually don't guarantee that this will succeed.
265  // The current implementation allows it though. This check may
266  // be removed in the future.
267  BOOST_CHECK( f == 10.0 );
268  BOOST_CHECK(float_sum == 10.0 );
269 }
270 
271 BOOST_AUTO_TEST_CASE( testCrossRemoval )
272 {
273  CrossRemover runobj(t_event);
274  SimulationActivity task(0.01, &runobj);
275  BOOST_CHECK( task.start() );
276  BOOST_CHECK( SimulationThread::Instance()->run(100) );
277  BOOST_CHECK( task.stop() );
278 }
279 
280 #ifdef OROCOS_TARGET_GNULINUX
281 BOOST_AUTO_TEST_CASE( testConcurrentEmit )
282 {
283  testConcurrentEmitHandlerCount.set(0);
284  Signal<void(void)> event;
285  EmitAndcount arunobj(event);
286  EmitAndcount brunobj(event);
287  EmitAndcount crunobj(event);
288  EmitAndcount drunobj(event);
289  Activity atask(ORO_SCHED_OTHER, 0, 0, &arunobj);
290  Activity btask(ORO_SCHED_OTHER, 0, 0, &brunobj);
291  Activity ctask(ORO_SCHED_OTHER, 0, 0, &crunobj);
292  Activity dtask(ORO_SCHED_OTHER, 0, 0, &drunobj);
294  BOOST_CHECK( h.connected() );
295  BOOST_CHECK( atask.start() );
296  BOOST_CHECK( btask.start() );
297  BOOST_CHECK( ctask.start() );
298  BOOST_CHECK( dtask.start() );
299  sleep(1);
300  BOOST_CHECK( atask.stop() );
301  BOOST_CHECK( btask.stop() );
302  BOOST_CHECK( ctask.stop() );
303  BOOST_CHECK( dtask.stop() );
304  // Verify that all emits also caused the handler to be called.
305  BOOST_CHECK_EQUAL( arunobj.count + brunobj.count + crunobj.count + drunobj.count, testConcurrentEmitHandlerCount.read() );
306 }
307 #endif
308 
309 BOOST_AUTO_TEST_CASE( testBlockingTask )
310 {
311  Signal<void(int)> event;
312  Runner runobj(event);
313  Activity task(15, &runobj);
314  BOOST_CHECK(task.start());
315  usleep(100000);
316  BOOST_CHECK(task.stop());
317 
318  BOOST_CHECK( runobj.result );
319 }
320 
321 BOOST_AUTO_TEST_CASE( testEventArgs )
322 {
323  float_sum = 0;
324  Handle h1 = t_event_float.connect( boost::bind(&EventTest::float_listener, this,_1,_2) );
325 
326  t_event_float(1.0, 4.0);
327  BOOST_CHECK_EQUAL( float(5.0), float_sum );
328 
329  float a = 10.0, b = 5.0;
330  t_event_float(a, b);
331  BOOST_CHECK_EQUAL( float(20.0), float_sum );
332 
333  h1.disconnect();
334 }
335 
336 BOOST_AUTO_TEST_CASE( testSyncListener )
337 {
338  // No completer:
339  reset();
340  Handle h = t_event.connect( boost::bind(&EventTest::listener, this) );
341  BOOST_CHECK( h.connected() );
342  t_event();
343  h.disconnect();
344  BOOST_CHECK( !h.connected() );
345 
346  BOOST_CHECK( t_listener_value );
347 }
348 
349 BOOST_AUTO_TEST_CASE( testSyncListenerString )
350 {
351  reset();
352  Handle h = t_event_string.connect( boost::bind(&EventTest::listenerString,this,_1) );
353  BOOST_CHECK( h.connected() );
354  t_event_string( std::string("What") );
355  h.disconnect();
356  BOOST_CHECK( !h.connected() );
357 
358  BOOST_CHECK_EQUAL( t_listener_what, std::string("What") );
359  BOOST_CHECK( !t_listener_value );
360 }
361 
362 BOOST_AUTO_TEST_CASE( testRTEvent )
363 {
364  reset();
365 
366  Handle hl( t_event.setup( boost::bind(&EventTest::listener,this) ) );
367 
368  BOOST_CHECK( !hl.connected() );
369 
370  hl.connect();
371  BOOST_CHECK( hl.connected() );
372 
373  t_event();
374 
375 
376  hl.disconnect();
377  BOOST_CHECK( !hl.connected() );
378  BOOST_CHECK( t_listener_value );
379 
380  reset();
381  t_event();
382  BOOST_CHECK( t_listener_value == false );
383 
384  hl.connect();
385  t_event();
386 
387  hl.disconnect();
388  BOOST_CHECK( !hl.connected() );
389  BOOST_CHECK( t_listener_value == true );
390 }
391 
393 
float float_sum
Definition: event_test.cpp:157
void handle(int i)
Definition: event_test.cpp:74
void finalize()
Definition: event_test.cpp:135
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
bool initialize()
Definition: event_test.cpp:90
bool breakLoop()
Definition: event_test.cpp:216
SelfRemover(Signal< void(void)> &e_)
Definition: event_test.cpp:87
virtual bool stop()
Definition: Activity.cpp:280
bool breakLoop()
Definition: event_test.cpp:66
void finalize()
Definition: event_test.cpp:70
#define BOOST_AUTO_TEST_SUITE_END()
void handle(void)
Definition: event_test.cpp:104
bool connected() const
Definition: Handle.cpp:79
int usleep(unsigned int us)
Definition: fosi.cpp:58
bool result
Definition: event_test.cpp:43
void loop()
Definition: event_test.cpp:62
std::string t_listener_what
Definition: event_test.cpp:156
int data
Definition: event_test.cpp:44
void testConcurrentEmitHandler(void)
Definition: event_test.cpp:200
bool initialize()
Definition: event_test.cpp:213
void step()
Definition: event_test.cpp:57
static SimulationThreadPtr Instance(double period=0.001)
Signal< void(void)> & e
Definition: event_test.cpp:115
void finalize()
Definition: event_test.cpp:101
bool disconnect()
Definition: Handle.cpp:72
Signal< void(void)> & e
Definition: event_test.cpp:85
void listenerString(const std::string &what)
Definition: event_test.cpp:173
bool t_listener_value
Definition: event_test.cpp:155
static std::ostream & endl(std::ostream &__os)
Definition: Logger.cpp:383
void step()
Definition: event_test.cpp:96
virtual bool start()
Definition: Activity.cpp:275
bool connect()
Definition: Handle.cpp:65
Runner(Signal< void(int)> &e_)
Definition: event_test.cpp:47
Signal< void(std::string)> t_event_string
Definition: event_test.cpp:153
Signal< void(void)> & mev
Definition: event_test.cpp:211
void handle(void)
Definition: event_test.cpp:138
An Activity executes a RunnableInterface object in a (periodic) thread.
Definition: Activity.hpp:70
unsigned int sleep(unsigned int s)
Definition: fosi.cpp:51
void reset()
Definition: event_test.cpp:185
static Logger & log()
Definition: Logger.cpp:117
int read() const
Definition: Atomic.hpp:77
EmitAndcount(Signal< void(void)> &ev)
Definition: event_test.cpp:209
Signal< int(float, float)> t_event_float
Definition: event_test.cpp:154
void listener(void)
Definition: event_test.cpp:168
Signal< void(void)> t_event
Definition: event_test.cpp:152
void set(int i)
Definition: Atomic.hpp:82
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
A SimulationActivity is a PeriodicActivity which is used for simulation.
int float_listener(float a, float b)
Definition: event_test.cpp:178
Signal< void(int)> & e
Definition: event_test.cpp:45
BOOST_AUTO_TEST_CASE(testEmpty)
Definition: event_test.cpp:228
static Logger & log()
Definition: Logger.hpp:350
The Handle holds the information, and allows manipulation, of a connection between a internal::Signal...
Definition: Handle.hpp:66
#define ORO_SCHED_OTHER
Definition: ecos/fosi.h:62
static Logger::LogFunction endlog()
Definition: Logger.hpp:362
void float_reference(float &f)
Definition: event_test.cpp:191
bool initialize()
Definition: event_test.cpp:121
CrossRemover(Signal< void(void)> &e_)
Definition: event_test.cpp:117
bool initialize()
Definition: event_test.cpp:50
void finalize()
Definition: event_test.cpp:215
Handle h1
Definition: event_test.cpp:46
static AtomicInt testConcurrentEmitHandlerCount
Definition: event_test.cpp:198


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:24