transport_tests.cpp
Go to the documentation of this file.
1 
6 #include <rtt/os/startstop.h>
7 #include <rtt/Logger.hpp>
9 
10 #include <rtt_roscomm/rostopic.h>
11 #include <rtt_roscomm/rosservice.h>
12 #include <std_msgs/typekit/String.h>
13 #include <std_srvs/Empty.h>
14 
15 #include <ros/names.h>
16 #include <ros/service_manager.h>
17 #include <ros/this_node.h>
18 
19 #include <boost/weak_ptr.hpp>
20 
21 #include <gtest/gtest.h>
22 
23 #include <algorithm>
24 
25 TEST(TransportTest, OutOfBandTest)
26 {
27  ros::V_string advertised_topics, subscribed_topics;
28 
29  // Import plugins
30  ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_ros", "" ));
31  ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_rosnode", "" ));
32  ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_roscomm", "" ));
33  ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_std_msgs", "" ));
34 
37 
38  // Create an out-of-band connection with ROS transport (a publisher/subscriber pair)
39  // NOTE: The rtt-ros-primitives-transport installs a transport for the std::string type
40  // which is compatible to std_msgs/String.
41  std::string topic = ros::names::resolve("~talker");
42 // EXPECT_TRUE(out.connectTo(&in, rtt_roscomm::topicLatched(topic)));
43  EXPECT_TRUE(out.createStream(rtt_roscomm::topicLatched(topic)));
44  EXPECT_TRUE(in.createStream(rtt_roscomm::topic(topic)));
45 
46  // Check that the publisher and subscriber have been successfully registered:
47  ros::this_node::getAdvertisedTopics(advertised_topics);
48  EXPECT_TRUE(std::find(advertised_topics.begin(), advertised_topics.end(),
49  topic) != advertised_topics.end());
50  ros::this_node::getSubscribedTopics(subscribed_topics);
51  EXPECT_TRUE(std::find(subscribed_topics.begin(), subscribed_topics.end(),
52  topic) != subscribed_topics.end());
53 
54  // publish and latch one sample
55  std_msgs::String sample;
56  sample.data = "Hello world!";
57  out.write(sample);
58  usleep(1000000);
59 
60  // read sample through input port
61  std::string received;
62  EXPECT_EQ(RTT::NewData, in.read(received) );
63  EXPECT_EQ(sample.data, received);
64 
65  // Close connection
66  out.disconnect();
67  EXPECT_FALSE(out.connected());
68  in.disconnect();
69  EXPECT_FALSE(in.connected());
70 
71  // Check that the publisher and subscriber have been destroyed:
72  advertised_topics.clear();
73  subscribed_topics.clear();
74  ros::this_node::getAdvertisedTopics(advertised_topics);
75  EXPECT_FALSE(std::find(advertised_topics.begin(), advertised_topics.end(),
76  topic) != advertised_topics.end());
77  ros::this_node::getSubscribedTopics(subscribed_topics);
78  EXPECT_FALSE(std::find(subscribed_topics.begin(), subscribed_topics.end(),
79  topic) != subscribed_topics.end());
80 }
81 
82 TEST(TransportTest, VectorTest)
83 {
84  // Import plugins
85  ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_ros", "" ));
86  ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_rosnode", "" ));
87  ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_roscomm", "" ));
88  ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_std_msgs", "" ));
89 
92 
93  // Create an out-of-band connection with ROS transport (a publisher/subscriber pair)
94  std::string topic = ros::names::resolve("~array_talker");
95  EXPECT_TRUE(out.connectTo(&in, rtt_roscomm::topicLatched(topic)));
96 // EXPECT_TRUE(out.createStream(rtt_roscomm::topicLatched(topic)));
97 // EXPECT_TRUE(in.createStream(rtt_roscomm::topic(topic)));
98 
99  // publish and latch one sample
100  static const double sample_array[] = { 1., 2., 3., 4., 5. };
101  std::vector<double> sample(sample_array, sample_array + sizeof(sample_array)/sizeof(sample_array[0]));
102  out.write(sample);
103  usleep(1000000);
104 
105  // read sample through input port
106  std::vector<double> received;
107  EXPECT_EQ(RTT::NewData, in.read(received) );
108  EXPECT_EQ(sample.size(), received.size());
109  if (sample.size() == received.size()) {
110  EXPECT_TRUE(std::equal(received.begin(), received.end(), sample.begin()));
111  }
112 
113  // Close connection
114  out.disconnect();
115  in.disconnect();
116  EXPECT_FALSE(out.connected());
117  EXPECT_FALSE(in.connected());
118 }
119 
120 static int callback_called = 0;
121 bool callback0(std_srvs::Empty::Request&, std_srvs::Empty::Response&)
122 {
123  ++callback_called;
124  return true;
125 }
126 
127 bool callback1()
128 {
129  ++callback_called;
130  return true;
131 }
132 
133 void callback2()
134 {
135  ++callback_called;
136 }
137 
138 TEST(TransportTest, ServiceServerTest)
139 {
140  std::string service = ros::names::resolve("~empty");
141 
142  // Import plugins
143  ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_rosnode", "" ));
144  ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_roscomm", "" ));
145  ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_std_srvs", "" ));
146 
147  // Create a TaskContext
148  RTT::TaskContext *tc = new RTT::TaskContext("TaskContext");
149  tc->addOperation("empty0", &callback0);
150  tc->addOperation("empty1", &callback1);
151  tc->addOperation("empty2", &callback2);
152 
153  // Load the rosservice service
154  boost::weak_ptr<rtt_rosservice::ROSService> rosservice;
155  rosservice = tc->getProvider<rtt_rosservice::ROSService>("rosservice");
156  ASSERT_FALSE(rosservice.expired());
157 
158  // Create a service server
159  EXPECT_TRUE(rosservice.lock()->connect("empty0", service + "0", "std_srvs/Empty"));
160  EXPECT_TRUE(rosservice.lock()->connect("empty1", service + "1", "std_srvs/Empty"));
161  EXPECT_TRUE(rosservice.lock()->connect("empty2", service + "2", "std_srvs/Empty"));
162 
163  // Check that the service server has been successfully registered:
164  EXPECT_TRUE(ros::ServiceManager::instance()->lookupServicePublication(service + "0").get());
165  EXPECT_TRUE(ros::ServiceManager::instance()->lookupServicePublication(service + "1").get());
166  EXPECT_TRUE(ros::ServiceManager::instance()->lookupServicePublication(service + "2").get());
167 
168  // Create a service client
172  tc->requires()->addOperationCaller(service_caller0);
173  tc->requires()->addOperationCaller(service_caller1);
174  tc->requires()->addOperationCaller(service_caller2);
175  EXPECT_TRUE(rosservice.lock()->connect("empty0", service + "0", "std_srvs/Empty"));
176  EXPECT_TRUE(service_caller0.ready());
177  EXPECT_TRUE(rosservice.lock()->connect("empty1", service + "1", "std_srvs/Empty"));
178  EXPECT_TRUE(service_caller1.ready());
179  EXPECT_TRUE(rosservice.lock()->connect("empty2", service + "2", "std_srvs/Empty"));
180  EXPECT_TRUE(service_caller2.ready());
181 
182  // Call the service
183  EXPECT_EQ(0, callback_called);
184  std_srvs::Empty empty;
185  EXPECT_TRUE(service_caller0(empty.request, empty.response));
186  EXPECT_EQ(1, callback_called);
187  EXPECT_TRUE(service_caller1(empty.request, empty.response));
188  EXPECT_EQ(2, callback_called);
189  EXPECT_TRUE(service_caller2(empty.request, empty.response));
190  EXPECT_EQ(3, callback_called);
191 
192  // Disconnect the service
193  EXPECT_TRUE(rosservice.lock()->disconnect(service + "0"));
194  EXPECT_TRUE(rosservice.lock()->disconnect(service + "1"));
195  EXPECT_TRUE(rosservice.lock()->disconnect(service + "2"));
196 
197  // Check that the service server has been destroyed
198  EXPECT_FALSE(ros::ServiceManager::instance()->lookupServicePublication(service));
199 
200  // Destroy the TaskContext
201  delete tc;
202  EXPECT_TRUE(rosservice.expired());
203 
204  // Check that the service server has been destroyed (again)
205  EXPECT_FALSE(ros::ServiceManager::instance()->lookupServicePublication(service));
206 }
207 
208 int main(int argc, char** argv) {
209  testing::InitGoogleTest(&argc, argv);
210 
211  // Initialize Orocos
212  __os_init(argc, argv);
213 
214  RTT::Logger::log().setStdStream(std::cerr);
217 
218  return RUN_ALL_TESTS();
219 }
220 
RTT::ConnPolicy topicLatched(const std::string &name)
void callback2()
bool ready() const
int __os_init(int argc, char **argv)
bool callback1()
int usleep(unsigned int us)
static int callback_called
ROSCPP_DECL std::string resolve(const std::string &name, bool remap=true)
int main(int argc, char **argv)
static boost::shared_ptr< ComponentLoader > Instance()
RTT::ConnPolicy topic(const std::string &name)
FlowStatus read(base::DataSourceBase::shared_ptr source)
static const ServiceManagerPtr & instance()
TEST(TransportTest, OutOfBandTest)
std::vector< std::string > V_string
ServiceRequester::shared_ptr requires()
ROSCPP_DECL void getSubscribedTopics(V_string &topics)
void mayLogStdOut(bool tf)
virtual bool createStream(ConnPolicy const &policy)
virtual bool connectTo(PortInterface *other, ConnPolicy const &policy)
WriteStatus write(const T &sample)
bool callback0(std_srvs::Empty::Request &, std_srvs::Empty::Response &)
virtual bool connected() const
static Logger & log()
virtual bool connected() const
ROSCPP_DECL void getAdvertisedTopics(V_string &topics)
void setStdStream(std::ostream &stdos)
Operation< Signature > & addOperation(Operation< Signature > &op)
virtual bool createStream(ConnPolicy const &policy)
void setLogLevel(LogLevel ll)
boost::shared_ptr< ServiceType > getProvider(const std::string &name)


rtt_roscomm_tests
Author(s): Jonathan Bohren
autogenerated on Sat Jun 8 2019 18:06:26