intraprocess_subscriptions.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /* Author: Josh Faust */
31 
32 /*
33  * Subscribe intraprocess, ensuring that no copy happens
34  */
35 
36 #include <string>
37 
38 #include <gtest/gtest.h>
39 
40 #include <stdlib.h>
41 
42 #include "ros/ros.h"
43 
44 uint32_t g_msg_constructor = 0;
45 uint32_t g_msg2_constructor = 0;
46 
47 struct Msg
48 {
49  Msg()
50  : serialized(false)
51  , deserialized(false)
52  {
54  }
55 
56  mutable bool serialized;
58 };
60 
61 namespace ros
62 {
63 namespace message_traits
64 {
65  template<>
66  struct MD5Sum<Msg>
67  {
68  static const char* value() { return "MsgMD5Sum"; }
69  static const char* value(const Msg&) { return "MsgMD5Sum"; }
70  };
71 
72  template<>
73  struct DataType<Msg>
74  {
75  static const char* value() { return "roscpp/MsgDataType"; }
76  static const char* value(const Msg&) { return "roscpp/MsgDataType"; }
77  };
78 
79  template<>
80  struct Definition<Msg>
81  {
82  static const char* value() { return ""; }
83  static const char* value(const Msg&) { return ""; }
84  };
85 } // namespace message_traits
86 
87 namespace serialization
88 {
89 template<>
90 struct Serializer<Msg>
91 {
92  template<typename Stream>
93  inline static void write(Stream&, const Msg& v)
94  {
95  v.serialized = true;
96  }
97 
98  template<typename Stream>
99  inline static void read(Stream&, Msg& v)
100  {
101  v.deserialized = true;
102  }
103 
104  inline static uint32_t serializedLength(const Msg&)
105  {
106  return 0;
107  }
108 };
109 } // namespace serialization
110 } // namespace ros
111 
113 
114 struct Msg2
115 {
117  : serialized(false)
118  , deserialized(false)
119  {
121  }
122 
123  mutable bool serialized;
125 };
127 
128 namespace ros
129 {
130 namespace message_traits
131 {
132  template<>
133  struct MD5Sum<Msg2>
134  {
135  static const char* value() { return "MsgMD5Sum"; }
136  static const char* value(const Msg2&) { return "MsgMD5Sum"; }
137  };
138 
139  template<>
140  struct DataType<Msg2>
141  {
142  static const char* value() { return "roscpp/MsgDataType"; }
143  static const char* value(const Msg2&) { return "roscpp/MsgDataType"; }
144  };
145 
146  template<>
147  struct Definition<Msg2>
148  {
149  static const char* value() { return ""; }
150  static const char* value(const Msg2&) { return ""; }
151  };
152 } // namespace message_traits
153 
154 namespace serialization
155 {
156 template<>
158 {
159  template<typename Stream>
160  inline static void write(Stream&, const Msg2& v)
161  {
162  v.serialized = true;
163  }
164 
165  template<typename Stream>
166  inline static void read(Stream&, Msg2& v)
167  {
168  v.deserialized = true;
169  }
170 
171  inline static uint32_t serializedLength(const Msg2&)
172  {
173  return 0;
174  }
175 };
176 } // namespace serialization
177 } // namespace ros
178 
179 void messageCallback(const MsgConstPtr& msg)
180 {
181  g_msg = msg;
182 }
183 
184 TEST(IntraprocessSubscriptions, noCopy)
185 {
186  g_msg.reset();
187  g_msg_constructor = 0;
188 
189  ros::NodeHandle nh;
190  ros::Subscriber sub = nh.subscribe("test", 0, messageCallback);
191  ros::Publisher pub = nh.advertise<Msg>("test", 0);
192 
193  MsgConstPtr msg(boost::make_shared<Msg>());
194 
195  while (pub.getNumSubscribers() == 0)
196  {
197  ros::Duration(0.01).sleep();
198  }
199 
200  pub.publish(msg);
201 
202  while (!g_msg)
203  {
204  ros::spinOnce();
205  ros::Duration(0.01).sleep();
206  }
207 
208  ASSERT_TRUE(g_msg);
209  EXPECT_EQ(g_msg.get(), msg.get());
210  EXPECT_FALSE(g_msg->serialized);
211  EXPECT_FALSE(g_msg->deserialized);
212  EXPECT_EQ(g_msg_constructor, 1ULL);
213 }
214 
215 TEST(IntraprocessSubscriptions, differentRTTI)
216 {
217  g_msg_constructor = 0;
218  g_msg.reset();
219 
220  ros::NodeHandle nh;
221  ros::Subscriber sub = nh.subscribe("test", 0, messageCallback);
222  ros::Publisher pub = nh.advertise<Msg2>("test", 0);
223 
224  Msg2ConstPtr msg(boost::make_shared<Msg2>());
225 
226  while (pub.getNumSubscribers() == 0)
227  {
228  ros::Duration(0.01).sleep();
229  }
230 
231  pub.publish(msg);
232 
233  while (!g_msg)
234  {
235  ros::spinOnce();
236  ros::Duration(0.01).sleep();
237  }
238 
239  ASSERT_TRUE(g_msg);
240  EXPECT_NE((void*)g_msg.get(), (void*)msg.get());
241  EXPECT_TRUE(msg->serialized);
242  EXPECT_TRUE(g_msg->deserialized);
243  EXPECT_EQ(g_msg_constructor, 1ULL);
244  EXPECT_EQ(g_msg2_constructor, 1ULL);
245 }
246 
249 {
250  g_msg2 = msg;
251 }
252 
253 TEST(IntraprocessSubscriptions, noCopyAndDifferentRTTI)
254 {
255  g_msg.reset();
256 
257  ros::NodeHandle nh;
258  ros::Subscriber sub1 = nh.subscribe("test", 0, messageCallback);
259  ros::Subscriber sub2 = nh.subscribe("test", 0, messageCallback2);
260  ros::Publisher pub = nh.advertise<Msg2>("test", 0);
261 
262  Msg2ConstPtr msg(boost::make_shared<Msg2>());
263 
264  while (pub.getNumSubscribers() == 0)
265  {
266  ros::Duration(0.01).sleep();
267  }
268 
269  pub.publish(msg);
270 
271  while (!g_msg || !g_msg2)
272  {
273  ros::spinOnce();
274  ros::Duration(0.01).sleep();
275  }
276 
277  ASSERT_TRUE(g_msg);
278  EXPECT_NE((void*)g_msg.get(), (void*)msg.get());
279  EXPECT_TRUE(msg->serialized);
280  EXPECT_TRUE(g_msg->deserialized);
281 
282  ASSERT_TRUE(g_msg2);
283  EXPECT_EQ(g_msg2.get(), msg.get());
284  EXPECT_TRUE(g_msg2->serialized);
285  EXPECT_FALSE(g_msg2->deserialized);
286 }
287 
288 int main(int argc, char** argv)
289 {
290  testing::InitGoogleTest(&argc, argv);
291  ros::init(argc, argv, "intraprocess_subscriptions");
292 
293  ros::NodeHandle nh;
294 
295  return RUN_ALL_TESTS();
296 }
297 
TEST(IntraprocessSubscriptions, noCopy)
void publish(const boost::shared_ptr< M > &message) const
static void write(Stream &, const Msg &v)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
MsgConstPtr g_msg
static const char * value(const Msg &)
int main(int argc, char **argv)
static const char * value(const Msg2 &)
boost::shared_ptr< Msg const > MsgConstPtr
void messageCallback2(const Msg2ConstPtr &msg)
uint32_t g_msg_constructor
void messageCallback(const MsgConstPtr &msg)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
uint32_t getNumSubscribers() const
Msg2ConstPtr g_msg2
static void write(Stream &, const Msg2 &v)
ROSCPP_DECL void spinOnce()
boost::shared_ptr< Msg2 const > Msg2ConstPtr
uint32_t g_msg2_constructor


test_roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Sun Feb 3 2019 03:30:13