corba_mqueue_ipc_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 corba_mqueue_ipc_test.cpp
3 
4  corba_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 "corba_mqueue_test.hpp"
22 
23 #include <iostream>
24 
29 #include <transports/corba/corba.h>
30 #include <rtt/InputPort.hpp>
31 #include <rtt/OutputPort.hpp>
32 #include <rtt/TaskContext.hpp>
35 #include <string>
36 #include <cstdlib>
37 
38 using namespace RTT;
39 using namespace RTT::detail;
40 
42 {
43 public:
44  CorbaMQueueIPCTest() { this->setUp(); }
45  ~CorbaMQueueIPCTest() { this->tearDown(); }
46 
50 
52  void new_data_listener(base::PortInterface* port);
53 
54  // Ports
57 
58  void setUp();
59  void tearDown();
60 
61  // helper test functions
62  void testPortDataConnection();
63  void testPortBufferConnection();
64  void testPortDisconnected();
65 };
66 
67 using namespace std;
69 
70 void
72 {
73  // setup DataPorts: we write into mw1, the server roundtrips it to mr1
74  mr1 = new InputPort<double>("mr");
75  mw1 = new OutputPort<double>("mw");
76 
77  tc = new TaskContext( "root" );
78  tc->ports()->addEventPort( *mr1, boost::bind(&CorbaMQueueIPCTest::new_data_listener, this, _1) );
79  tc->ports()->addPort( *mw1 );
80  tc->start();
81 
82  ts2 = ts = 0;
83  tp2 = tp = 0;
84 }
85 
86 
87 void
89 {
90  delete tp;
91  delete ts;
92  delete tp2;
93  delete tc;
94 
95  delete mr1;
96  delete mw1;
97 }
98 
100 {
101  signalled_port = port;
102 }
103 
104 #define ASSERT_PORT_SIGNALLING(code, read_port) do { \
105  signalled_port = 0; \
106  int wait = 0; \
107  code; \
108  while (read_port != signalled_port && wait++ != 5) \
109  usleep(100000); \
110  BOOST_CHECK( read_port == signalled_port ); \
111 } while(0)
112 
113 #define wait_for( cond, times ) do { \
114  bool wait_for_helper; \
115  int wait = 0; \
116  while( (wait_for_helper = !(cond)) && wait++ != times ) \
117  usleep(100000); \
118  if (wait_for_helper) BOOST_CHECK( cond ); \
119 } while(0)
120 
121 #define wait_for_equal( a, b, times ) do { \
122  bool wait_for_helper; \
123  int wait = 0; \
124  while( (wait_for_helper = ((a) != (b))) && wait++ != times ) \
125  usleep(100000); \
126  if (wait_for_helper) BOOST_CHECK_EQUAL( a, b ); \
127 } while(0)
128 
130 {
131  // This test assumes that there is a data connection mw1 => server => mr1
132  // Check if connection succeeded both ways:
133  BOOST_CHECK( mw1->connected() );
134  BOOST_CHECK( mr1->connected() );
135 
136  double value = 0;
137 
138  // Check if no-data works
139  BOOST_CHECK( !mr1->read(value) );
140 
141  // Check if writing works (including signalling)
142  ASSERT_PORT_SIGNALLING(mw1->write(1.0), mr1);
143  BOOST_CHECK( mr1->read(value) );
144  BOOST_CHECK_EQUAL( 1.0, value );
145  ASSERT_PORT_SIGNALLING(mw1->write(2.0), mr1);
146  BOOST_CHECK( mr1->read(value) );
147  BOOST_CHECK_EQUAL( 2.0, value );
148 }
149 
151 {
152  // This test assumes that there is a buffer connection mw1 => server => mr1 of size 3
153  // Check if connection succeeded both ways:
154  BOOST_CHECK( mw1->connected() );
155  BOOST_CHECK( mr1->connected() );
156 
157  double value = 0;
158 
159  // Check if no-data works
160  BOOST_CHECK( !mr1->read(value) );
161 
162  // Check if writing works
163  ASSERT_PORT_SIGNALLING(mw1->write(1.0), mr1);
164  ASSERT_PORT_SIGNALLING(mw1->write(2.0), mr1);
165  ASSERT_PORT_SIGNALLING(mw1->write(3.0), mr1);
166  // it will be emptied too fast by mqueue.
167  //ASSERT_PORT_SIGNALLING(mw1->write(4.0), 0);
168  BOOST_CHECK( mr1->read(value) );
169  BOOST_CHECK_EQUAL( 1.0, value );
170  BOOST_CHECK( mr1->read(value) );
171  BOOST_CHECK_EQUAL( 2.0, value );
172  BOOST_CHECK( mr1->read(value) );
173  BOOST_CHECK_EQUAL( 3.0, value );
174  BOOST_CHECK_EQUAL( mr1->read(value), OldData );
175 }
176 
178 {
179  BOOST_CHECK( !mw1->connected() );
180  BOOST_CHECK( !mr1->connected() );
181 }
182 
183 
184 // Registers the fixture into the 'registry'
185 BOOST_FIXTURE_TEST_SUITE( CorbaMQueueIPCTestSuite, CorbaMQueueIPCTest )
186 
187 BOOST_AUTO_TEST_CASE( testPortConnections )
188 {
189  // This test tests the different port-to-port connections.
190  ts = corba::TaskContextServer::Create( tc, /* use_naming = */ false );
191  tp = corba::TaskContextProxy::Create( "other", /* is_ior = */ false );
192  if (!tp)
193  tp = corba::TaskContextProxy::CreateFromFile( "other.ior");
194 
195  BOOST_REQUIRE(tp);
196 
197  // Create a default CORBA policy specification
198  RTT::corba::CConnPolicy policy = toCORBA( RTT::ConnPolicy() );
199  policy.type = RTT::corba::CData;
200  policy.init = false;
201  policy.lock_policy = RTT::corba::CLockFree;
202  policy.size = 0;
203  policy.data_size = 0;
204 
205  corba::CDataFlowInterface_var ports = ts->server()->ports();
206  corba::CDataFlowInterface_var ports2 = tp->server()->ports();
207 
208  // WARNING: in the following, there is four configuration tested. There is
209  // also three different ways to disconnect. We need to test those three
210  // "disconnection methods", so beware when you change something ...
211 
212  policy.type = RTT::corba::CData;
213  policy.pull = false;
214  policy.transport = ORO_MQUEUE_PROTOCOL_ID;
215  BOOST_CHECK( ports->createConnection("mw", ports2, "mr", policy) );
216  BOOST_CHECK( ports2->createConnection("mw", ports, "mr", policy) );
217  testPortDataConnection();
218  ports->disconnectPort("mw");
219  ports2->disconnectPort("mw");
220  testPortDisconnected();
221 
222  policy.type = RTT::corba::CData;
223  policy.pull = true;
224  policy.transport = ORO_MQUEUE_PROTOCOL_ID;
225  BOOST_CHECK( ports->createConnection("mw", ports2, "mr", policy) );
226  BOOST_CHECK( ports2->createConnection("mw", ports, "mr", policy) );
227  testPortDataConnection();
228  ports2->disconnectPort("mr");
229  ports->disconnectPort("mr");
230  testPortDisconnected();
231 
232  policy.type = RTT::corba::CBuffer;
233  policy.pull = false;
234  policy.size = 3;
235  policy.transport = ORO_MQUEUE_PROTOCOL_ID;
236  BOOST_CHECK( ports->createConnection("mw", ports2, "mr", policy) );
237  BOOST_CHECK( ports2->createConnection("mw", ports, "mr", policy) );
238  testPortBufferConnection();
239  ports->disconnectPort("mw");
240  ports2->disconnectPort("mw");
241  testPortDisconnected();
242 
243  policy.type = RTT::corba::CBuffer;
244  policy.pull = true;
245  policy.size = 3;
246  policy.transport = ORO_MQUEUE_PROTOCOL_ID;
247  BOOST_CHECK( ports->createConnection("mw", ports2, "mr", policy) );
248  BOOST_CHECK( ports2->createConnection("mw", ports, "mr", policy) );
249  testPortBufferConnection();
250  ports->disconnectPort("mw");
251  ports2->disconnectPort("mw");
252  testPortDisconnected();
253 }
254 
256 
corba::TaskContextServer * ts2
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
void new_data_listener(base::PortInterface *port)
corba::TaskContextProxy * tp2
#define BOOST_AUTO_TEST_SUITE_END()
Definition: mystd.hpp:163
InputPort< double > * mr1
#define ASSERT_PORT_SIGNALLING(code, read_port)
static TaskContextServer * Create(TaskContext *tc, bool use_naming=true, bool require_name_service=false)
OutputPort< double > * mw1
static TaskContextProxy * CreateFromFile(std::string filename)
BOOST_AUTO_TEST_CASE(testPortConnections)
RTT::corba::CConnPolicy toCORBA(RTT::ConnPolicy const &policy)
base::PortInterface * signalled_port
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
static TaskContextProxy * Create(std::string name, bool is_ior=false)
#define ORO_MQUEUE_PROTOCOL_ID
Definition: MQLib.hpp:57


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