mqueue_ipc_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 mqueue_ipc_test.cpp
3 
4  mqueue_ipc_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 #include "unit.hpp"
20 
21 #include "mqueue_test.hpp"
22 
23 #include <iostream>
24 
25 #include <Service.hpp>
29 #include <os/fosi.h>
30 
31 using namespace std;
32 using namespace RTT;
33 using namespace RTT::detail;
34 
35 void
36 MQueueTest::setUp()
37 {
38  // connect DataPorts
39  mr1 = new InputPort<double>("mr");
40  mw1 = new OutputPort<double>("mw");
41 
42  mr2 = new InputPort<double>("mr");
43  mw2 = new OutputPort<double>("mw");
44 
45  tc = new TaskContext( "root" );
46  tc->ports()->addPort( mr1 );
47  tc->ports()->addPort( mw1 );
48 
49  t2 = new TaskContext("other");
50  t2->ports()->addPort( mr2 );
51  t2->ports()->addPort( mw2 );
52 
53 }
54 
55 
56 void
57 MQueueTest::tearDown()
58 {
59  delete tc;
60  delete t2;
61 
62  delete mr1;
63  delete mw1;
64  delete mr2;
65  delete mw2;
66 }
67 
69 {
70  signalled_port = port;
71 }
72 
73 
74 #define ASSERT_PORT_SIGNALLING(code, read_port) \
75  signalled_port = 0; \
76  code; \
77  rtos_disable_rt_warning(); \
78  usleep(100000); \
79  rtos_enable_rt_warning(); \
80  BOOST_CHECK( read_port == signalled_port );
81 
83 {
85  // This test assumes that there is a data connection mw1 => mr2
86  // Check if connection succeeded both ways:
87  BOOST_CHECK( mw1->connected() );
88  BOOST_CHECK( mr2->connected() );
89 
90  double value = 0;
91 
92  // Check if no-data works
93  BOOST_CHECK( NoData == mr2->read(value) );
94 
95  // Check if writing works (including signalling)
96  ASSERT_PORT_SIGNALLING(mw1->write(1.0), mr2)
97  BOOST_CHECK( mr2->read(value) );
98  BOOST_CHECK_EQUAL( 1.0, value );
99  ASSERT_PORT_SIGNALLING(mw1->write(2.0), mr2);
100  BOOST_CHECK( mr2->read(value) );
101  BOOST_CHECK_EQUAL( 2.0, value );
102  BOOST_CHECK( OldData == mr2->read(value) );
103 
105 }
106 
108 {
110  // This test assumes that there is a buffer connection mw1 => mr2 of size 3
111  // Check if connection succeeded both ways:
112  BOOST_CHECK( mw1->connected() );
113  BOOST_CHECK( mr2->connected() );
114 
115  double value = 0;
116 
117  // Check if no-data works
118  BOOST_CHECK( NoData == mr2->read(value) );
119 
120  // Check if writing works
121  ASSERT_PORT_SIGNALLING(mw1->write(1.0), mr2);
122  ASSERT_PORT_SIGNALLING(mw1->write(2.0), mr2);
123  ASSERT_PORT_SIGNALLING(mw1->write(3.0), mr2);
124  ASSERT_PORT_SIGNALLING(mw1->write(4.0), 0); // because size == 3
125  BOOST_CHECK( mr2->read(value) );
126  BOOST_CHECK_EQUAL( 1.0, value );
127  BOOST_CHECK( mr2->read(value) );
128  BOOST_CHECK_EQUAL( 2.0, value );
129  BOOST_CHECK( mr2->read(value) );
130  BOOST_CHECK_EQUAL( 3.0, value );
131  BOOST_CHECK( OldData == mr2->read(value) );
132 
134 }
135 
137 {
138  BOOST_CHECK( !mw1->connected() );
139  BOOST_CHECK( !mr2->connected() );
140 }
141 
142 
143 // Registers the fixture into the 'registry'
144 BOOST_FIXTURE_TEST_SUITE( MQueueTestSuite, MQueueTest )
145 
146 
150 BOOST_AUTO_TEST_CASE( testPortConnections )
151 {
152  // Create a default policy specification
153  ConnPolicy policy;
154  policy.type = ConnPolicy::DATA;
155  policy.init = false;
157  policy.size = 0;
159 
160  // Set up an event handler to check if signalling works properly as well
161  Handle hl( mr2->getNewDataOnPortEvent()->setup(
162  boost::bind(&MQueueTest::new_data_listener, this, _1) ) );
163  hl.connect();
164 
165  DataFlowInterface* ports = tc->ports();
166  DataFlowInterface* ports2 = t2->ports();
167 
168 #if 1
169  // WARNING: in the following, there is four configuration tested.
170  // We need to manually disconnect both sides since mqueue are connection-less.
171  policy.type = ConnPolicy::DATA;
172  policy.pull = false;
173  // test user supplied connection.
174  policy.name_id = "/data1";
175  BOOST_CHECK( mw1->createConnection(*mr2, policy) );
176  BOOST_CHECK( policy.name_id == "/data1" );
177  testPortDataConnection();
178  mw1->disconnect();
179  mr2->disconnect();
180  testPortDisconnected();
181 
182  policy.type = ConnPolicy::DATA;
183  policy.pull = true;
184  policy.name_id = "";
185  BOOST_CHECK( mw1->createConnection(*mr2, policy) );
186  testPortDataConnection();
187  mw1->disconnect();
188  mr2->disconnect();
189  testPortDisconnected();
190 #endif
191 #if 1
192  policy.type = ConnPolicy::BUFFER;
193  policy.pull = false;
194  policy.size = 3;
195  policy.name_id = "";
196  //policy.name_id = "buffer1";
197  BOOST_CHECK( mw1->createConnection(*mr2, policy) );
198  testPortBufferConnection();
199  mw1->disconnect();
200  mr2->disconnect();
201  testPortDisconnected();
202 #endif
203 #if 1
204  policy.type = ConnPolicy::BUFFER;
205  policy.pull = true;
206  policy.size = 3;
207  policy.name_id = "";
208  //policy.name_id = "buffer2";
209  BOOST_CHECK( mw1->createConnection(*mr2, policy) );
210  testPortBufferConnection();
211  //while(1) sleep(1);
212  mw1->disconnect();
213  mr2->disconnect();
214  testPortDisconnected();
215 #endif
216  }
217 
218 BOOST_AUTO_TEST_CASE( testPortStreams )
219 {
220  // Create a default policy specification
221  ConnPolicy policy;
222  policy.type = ConnPolicy::DATA;
223  policy.init = false;
225  policy.size = 0;
227 
228  // Set up an event handler to check if signalling works properly as well
229  Handle hl( mr2->getNewDataOnPortEvent()->setup(
230  boost::bind(&MQueueTest::new_data_listener, this, _1) ) );
231  hl.connect();
232 
233  DataFlowInterface* ports = tc->ports();
234  DataFlowInterface* ports2 = t2->ports();
235 
236 
237  // Test all four configurations of Data/Buffer & push/pull
238  policy.type = ConnPolicy::DATA;
239  policy.pull = false;
240  policy.name_id = "/data1";
241  BOOST_CHECK( mw1->createStream( policy ) );
242  BOOST_CHECK( mr2->createStream( policy ) );
243  testPortDataConnection();
244  mw1->disconnect();
245  mr2->disconnect();
246  testPortDisconnected();
247 
248  policy.type = ConnPolicy::DATA;
249  policy.pull = true;
250  policy.name_id = "";
251  BOOST_CHECK( mw1->createStream( policy ) );
252  BOOST_CHECK( mr2->createStream( policy ) );
253  testPortDataConnection();
254  mw1->disconnect();
255  mr2->disconnect();
256  testPortDisconnected();
257 
258  policy.type = ConnPolicy::BUFFER;
259  policy.pull = false;
260  policy.size = 3;
261  policy.name_id = "/buffer1";
262  BOOST_CHECK( mw1->createStream( policy ) );
263  BOOST_CHECK( mr2->createStream( policy ) );
264  testPortBufferConnection();
265  mw1->disconnect();
266  mr2->disconnect();
267  testPortDisconnected();
268 
269  policy.type = ConnPolicy::BUFFER;
270  policy.pull = true;
271  policy.size = 3;
272  policy.name_id = "";
273  BOOST_CHECK( mw1->createStream( policy ) );
274  BOOST_CHECK( mr2->createStream( policy ) );
275  testPortBufferConnection();
276  mw1->disconnect();
277  mr2->disconnect();
278  testPortDisconnected();
279 }
280 
281 BOOST_AUTO_TEST_CASE( testPortStreamsTimeout )
282 {
283  // Create a default policy specification
284  ConnPolicy policy;
285  policy.type = ConnPolicy::DATA;
286  policy.init = false;
288  policy.size = 0;
290 
291  // Test creating an input stream without an output stream available.
292  policy.type = ConnPolicy::DATA;
293  policy.pull = false;
294  policy.name_id = "/data1";
295  BOOST_CHECK( mr2->createStream( policy ) == false );
296  BOOST_CHECK( mr2->connected() == false );
297  mr2->disconnect();
298 
299  policy.type = ConnPolicy::BUFFER;
300  policy.pull = false;
301  policy.size = 10;
302  policy.name_id = "/buffer1";
303  BOOST_CHECK( mr2->createStream( policy ) == false );
304  BOOST_CHECK( mr2->connected() == false );
305  mr2->disconnect();
306 }
307 
308 
309 BOOST_AUTO_TEST_CASE( testPortStreamsWrongName )
310 {
311  // Create a default policy specification
312  ConnPolicy policy;
313  policy.type = ConnPolicy::DATA;
314  policy.init = false;
316  policy.size = 0;
318 
319  // Test creating an input stream without an output stream available.
320  policy.type = ConnPolicy::DATA;
321  policy.pull = false;
322  policy.name_id = "data1"; // name must start with '/'
323  BOOST_CHECK( mr2->createStream( policy ) == false );
324  BOOST_CHECK( mr2->connected() == false );
325  mr2->disconnect();
326 
327  policy.type = ConnPolicy::BUFFER;
328  policy.pull = false;
329  policy.size = 10;
330  policy.name_id = "buffer1";
331  BOOST_CHECK( mr2->createStream( policy ) == false );
332  BOOST_CHECK( mr2->connected() == false );
333  mr2->disconnect();
334 }
335 
336 // copied from testPortStreams
337 BOOST_AUTO_TEST_CASE( testVectorTransport )
338 {
339  // Create a default policy specification
340  ConnPolicy policy;
341  policy.type = ConnPolicy::DATA;
342  policy.init = false;
344  policy.size = 0;
346 
347  // Set up an event handler to check if signalling works properly as well
348  Handle hl( mr2->getNewDataOnPortEvent()->setup(
349  boost::bind(&MQueueTest::new_data_listener, this, _1) ) );
350  hl.connect();
351 
352  DataFlowInterface* ports = tc->ports();
353  DataFlowInterface* ports2 = t2->ports();
354 
355  std::vector<double> data(20, 3.33);
356  InputPort< std::vector<double> > vin("VIn");
357  OutputPort< std::vector<double> > vout("Vout");
358  ports->addPort(&vin, "input port");
359  ports2->addPort(&vout, "output port");
360 
361  // init the output port with a vector of size 20, values 3.33
362  vout.setDataSample( data );
363  data = vout.getLastWrittenValue();
364  for(int i=0; i != 20; ++i)
365  BOOST_CHECK_CLOSE( data[i], 3.33, 0.01);
366 
367  policy.type = ConnPolicy::DATA;
368  policy.pull = false;
369  policy.name_id = "/vdata1";
370  BOOST_CHECK( vout.createStream( policy ) );
371  BOOST_CHECK( vin.createStream( policy ) );
372 
373  // check that the receiver did not get any data
374  BOOST_CHECK_EQUAL( vin.read(data), NoData);
375 
376  // prepare a new data sample, size 10, values 6.66
377  data.clear();
378  data.resize(10, 6.66);
379  for(int i=0; i != data.size(); ++i)
380  BOOST_CHECK_CLOSE( data[i], 6.66, 0.01);
381 
383  vout.write( data );
385 
386  // prepare data buffer for reception:
387  data.clear();
388  data.resize(20, 0.0);
389  usleep(200000);
390 
392  BOOST_CHECK_EQUAL( vin.read(data), NewData);
394 
395  // check if both size and capacity and values are as expected.
396  BOOST_CHECK_EQUAL( data.size(), 10);
397  BOOST_CHECK_EQUAL( data.capacity(), 20);
398  for(int i=0; i != data.size(); ++i)
399  BOOST_CHECK_CLOSE( data[i], 6.66, 0.01);
400 
402  BOOST_CHECK_EQUAL( vin.read(data), OldData);
404 }
405 
407 
void setDataSample(const T &sample)
Definition: OutputPort.hpp:209
base::PortInterface & addPort(const std::string &name, base::PortInterface &port)
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
virtual bool createConnection(internal::SharedConnectionBase::shared_ptr shared_connection, ConnPolicy const &policy=ConnPolicy())
#define BOOST_AUTO_TEST_SUITE_END()
void testPortDisconnected()
Definition: mystd.hpp:163
int usleep(unsigned int us)
Definition: fosi.cpp:58
void testPortBufferConnection()
FlowStatus read(base::DataSourceBase::shared_ptr source)
Definition: InputPort.hpp:97
static const int DATA
Definition: ConnPolicy.hpp:111
BOOST_AUTO_TEST_CASE(testPortConnections)
void rtos_disable_rt_warning()
virtual bool createStream(ConnPolicy const &policy)
Definition: OutputPort.hpp:314
bool connect()
Definition: Handle.cpp:65
WriteStatus write(const T &sample)
Definition: OutputPort.hpp:243
static const int LOCK_FREE
Definition: ConnPolicy.hpp:117
void new_data_listener(PortInterface *port)
Definition: mqueue_test.cpp:81
static const int BUFFER
Definition: ConnPolicy.hpp:112
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
void testPortDataConnection()
std::string name_id
Definition: ConnPolicy.hpp:256
void rtos_enable_rt_warning()
T getLastWrittenValue() const
Definition: OutputPort.hpp:173
#define ASSERT_PORT_SIGNALLING(code, read_port)
The Handle holds the information, and allows manipulation, of a connection between a internal::Signal...
Definition: Handle.hpp:66
virtual bool createStream(ConnPolicy const &policy)
Definition: InputPort.hpp:207
#define ORO_MQUEUE_PROTOCOL_ID
Definition: MQLib.hpp:57


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