slave_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 slave_test.cpp
3 
4  slave_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 #include "unit.hpp"
21 
22 #include <rtt-fwd.hpp>
23 
24 #include <rtt/TaskContext.hpp>
25 #include <rtt/Operation.hpp>
26 #include <rtt/OperationCaller.hpp>
28 
29 #include <rtt/os/Mutex.hpp>
30 #include <rtt/os/Condition.hpp>
31 #include <rtt/os/fosi.h>
32 
33 using namespace std;
34 using namespace RTT::detail;
35 using namespace RTT;
36 
38 {
39 public:
41  , activity(0)
42  {
43  }
44 
45  void updateHook()
46  {
47  BOOST_TEST_MESSAGE( "[ENTER] MasterComponent::updateHook()" );
48  if (activity) activity->execute();
49  BOOST_TEST_MESSAGE( "[EXIT] MasterComponent::updateHook()" );
50  }
51 
53  {
54  this->activity = activity;
55  }
56 
57 public:
59 };
60 
62 {
63 public:
65  , own_thread_operation_called_counter(0)
66  {
67  this->addOperation("ownThreadOperation", &SlaveComponent::ownThreadOperation, this, RTT::OwnThread);
68  slave_operation_caller.setCaller(this->engine());
69  }
70 
72  {
73  BOOST_TEST_MESSAGE( "[ENTER] SlaveComponent::ownThreadOperation()" );
74  own_thread_operation_called_counter++;
75 
76  if (slave_operation_caller.ready()) {
77  slave_operation_caller();
78  }
79  BOOST_TEST_MESSAGE( "[EXIT] SlaveComponent::ownThreadOperation()" );
80  }
81 
82 public:
85 };
86 
88 {
89 public:
91  , callback_operation_called_counter(0)
92  {
93  this->addOperation("callSlaveOperation", &ClientComponent::callSlaveOperation, this, RTT::OwnThread);
94  this->addOperation("callbackOperation", &ClientComponent::callbackOperation, this, RTT::OwnThread);
95  slave_operation_caller.setCaller(this->engine());
96  }
97 
99  {
100  BOOST_TEST_MESSAGE( "[ENTER] ClientComponent::callSlaveOperation()" );
101  BOOST_REQUIRE(slave_operation_caller.ready());
102  slave_operation_caller();
103  BOOST_TEST_MESSAGE( "[EXIT] ClientComponent::callSlaveOperation()" );
104  }
105 
107  {
108  BOOST_TEST_MESSAGE( "[ENTER] ClientComponent::callbackOperation()" );
109  callback_operation_called_counter++;
110  BOOST_TEST_MESSAGE( "[EXIT] ClientComponent::callbackOperation()" );
111  }
112 
113 public:
116 };
117 
122 {
123 public:
125  {
126  //RTT::Logger::Instance()->setLogLevel(RTT::Logger::Debug);
127 
128  slave.setActivity(new RTT::extras::SlaveActivity(master.getActivity()));
129  master.execute(slave.getActivity());
130  client.slave_operation_caller = slave.getOperation("ownThreadOperation");
131  }
132 
134  {}
135 
136 public:
140 };
141 
142 // Registers the fixture into the 'registry'
144 
145 // Test operation call on the SlaveComponent
146 BOOST_AUTO_TEST_CASE( testSlaveOperationCall )
147 {
148  RTT::OperationCaller<void()> callSlaveOperation = client.getOperation("callSlaveOperation");
149  BOOST_REQUIRE( callSlaveOperation.ready() );
150 
151  // Note: master is not running!
152  RTT::SendHandle<void()> handle = callSlaveOperation.send();
153  int wait = 5;
154  while( !handle.collectIfDone() && wait-- ) {
155  sleep(1);
156  }
157  BOOST_CHECK( handle.collectIfDone() );
158  BOOST_CHECK_EQUAL( slave.own_thread_operation_called_counter, 1 );
159  BOOST_CHECK_EQUAL( client.callback_operation_called_counter, 0 );
160 }
161 
162 // Test operation call with a callback to the ClientComponent
163 BOOST_AUTO_TEST_CASE( testSlaveOperationCallWithCallback )
164 {
165  RTT::OperationCaller<void()> callSlaveOperation = client.getOperation("callSlaveOperation");
166  BOOST_REQUIRE( callSlaveOperation.ready() );
167 
168  // connect SlaveComponent::slave_operation_caller to ClientComponent::callbackOperation()
169  slave.slave_operation_caller = client.getOperation("callbackOperation");
170  BOOST_REQUIRE( slave.slave_operation_caller.ready() );
171 
172  // Note: master is not running!
173  RTT::SendHandle<void()> handle = callSlaveOperation.send();
174  int wait = 5;
175  while( !handle.collectIfDone() && wait-- ) {
176  sleep(1);
177  }
178  BOOST_CHECK( handle.collectIfDone() );
179  BOOST_CHECK_EQUAL( slave.own_thread_operation_called_counter, 1 );
180  BOOST_CHECK_EQUAL( client.callback_operation_called_counter, 1 );
181 }
182 
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
MasterComponent master
Definition: slave_test.cpp:137
void callSlaveOperation()
Definition: slave_test.cpp:98
void execute(RTT::base::ActivityInterface *activity)
Definition: slave_test.cpp:52
#define BOOST_AUTO_TEST_SUITE_END()
RTT::base::ActivityInterface * activity
Definition: slave_test.cpp:58
Definition: mystd.hpp:163
SlaveComponent slave
Definition: slave_test.cpp:138
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
void ownThreadOperation()
Definition: slave_test.cpp:71
Interface to start/stop and query a Activity.
BOOST_AUTO_TEST_CASE(testSlaveOperationCall)
Definition: slave_test.cpp:146
unsigned int own_thread_operation_called_counter
Definition: slave_test.cpp:83
unsigned int callback_operation_called_counter
Definition: slave_test.cpp:114
unsigned int sleep(unsigned int s)
Definition: fosi.cpp:51
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
void callbackOperation()
Definition: slave_test.cpp:106
ClientComponent client
Definition: slave_test.cpp:139
An base::ActivityInterface implementation which executes &#39;step&#39; upon the invocation of &#39;execute()&#39;...
RTT::OperationCaller< void()> slave_operation_caller
Definition: slave_test.cpp:115
RTT::OperationCaller< void()> slave_operation_caller
Definition: slave_test.cpp:84


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