test_subscription_queue.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, 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  * Test subscription queue
34  */
35 
36 #include <gtest/gtest.h>
37 #include "ros/subscription_queue.h"
41 #include "ros/init.h"
42 
43 #include <boost/shared_array.hpp>
44 #include <boost/bind/bind.hpp>
45 #include <boost/thread.hpp>
46 
47 #include "fake_message.h"
48 
49 using namespace ros;
50 
52 {
53  SubscriptionQueue queue("blah", 1, false);
54 
55  FakeSubHelperPtr helper(boost::make_shared<FakeSubHelper>());
56  MessageDeserializerPtr des(boost::make_shared<MessageDeserializer>(helper, SerializedMessage(), boost::shared_ptr<M_string>()));
57 
58  ASSERT_FALSE(queue.full());
59 
60  queue.push(helper, des, false, VoidConstWPtr(), true);
61 
62  ASSERT_TRUE(queue.full());
63 
64  ASSERT_EQ(queue.call(), CallbackInterface::Success);
65 
66  ASSERT_FALSE(queue.full());
67 
68  queue.push(helper, des, false, VoidConstWPtr(), true);
69 
70  ASSERT_TRUE(queue.full());
71 
72  ASSERT_TRUE(queue.ready());
73 
74  queue.push(helper, des, false, VoidConstWPtr(), true);
75 
76  ASSERT_TRUE(queue.full());
77 
78  ASSERT_EQ(queue.call(), CallbackInterface::Success);
79  ASSERT_EQ(queue.call(), CallbackInterface::Invalid);
80 
81  ASSERT_EQ(helper->calls_, 2u);
82 }
83 
84 TEST(SubscriptionQueue, infiniteQueue)
85 {
86  SubscriptionQueue queue("blah", 0, false);
87 
88  FakeSubHelperPtr helper(boost::make_shared<FakeSubHelper>());
89  MessageDeserializerPtr des(boost::make_shared<MessageDeserializer>(helper, SerializedMessage(), boost::shared_ptr<M_string>()));
90 
91  ASSERT_FALSE(queue.full());
92 
93  queue.push(helper, des, false, VoidConstWPtr(), true);
94  ASSERT_EQ(queue.call(), CallbackInterface::Success);
95 
96  ASSERT_FALSE(queue.full());
97 
98  for (int i = 0; i < 10000; ++i)
99  {
100  queue.push(helper, des, false, VoidConstWPtr(), true);
101  }
102 
103  ASSERT_FALSE(queue.full());
104 
105  for (int i = 0; i < 10000; ++i)
106  {
107  ASSERT_EQ(queue.call(), CallbackInterface::Success);
108  }
109 
110  ASSERT_EQ(queue.call(), CallbackInterface::Invalid);
111 
112  ASSERT_EQ(helper->calls_, 10001u);
113 }
114 
116 {
117  SubscriptionQueue queue("blah", 1, false);
118 
119  FakeSubHelperPtr helper(boost::make_shared<FakeSubHelper>());
120  MessageDeserializerPtr des(boost::make_shared<MessageDeserializer>(helper, SerializedMessage(), boost::shared_ptr<M_string>()));
121 
122  queue.push(helper, des, false, VoidConstWPtr(), true);
123  queue.clear();
124  ASSERT_EQ(queue.call(), CallbackInterface::Invalid);
125 }
126 
127 TEST(SubscriptionQueue, clearThenAddAndCall)
128 {
129  SubscriptionQueue queue("blah", 1, false);
130 
131  FakeSubHelperPtr helper(boost::make_shared<FakeSubHelper>());
132  MessageDeserializerPtr des(boost::make_shared<MessageDeserializer>(helper, SerializedMessage(), boost::shared_ptr<M_string>()));
133 
134  queue.push(helper, des, false, VoidConstWPtr(), true);
135  queue.clear();
136  queue.push(helper, des, false, VoidConstWPtr(), true);
137  ASSERT_EQ(queue.call(), CallbackInterface::Success);
138 }
139 
141 {
142  queue.clear();
143 }
144 
145 TEST(SubscriptionQueue, clearInCallback)
146 {
147  SubscriptionQueue queue("blah", 1, false);
148 
149  FakeSubHelperPtr helper(boost::make_shared<FakeSubHelper>());
150  MessageDeserializerPtr des(boost::make_shared<MessageDeserializer>(helper, SerializedMessage(), boost::shared_ptr<M_string>()));
151 
152  helper->cb_ = boost::bind(clearInCallbackCallback, boost::ref(queue));
153  queue.push(helper, des, false, VoidConstWPtr(), true);
154  queue.call();
155 }
156 
157 void clearWhileThreadIsBlockingCallback(bool* done, boost::barrier* barrier)
158 {
159  barrier->wait();
160  ros::WallDuration(.1).sleep();
161  *done = true;
162 }
163 
165 {
166  queue.call();
167 }
168 
169 TEST(SubscriptionQueue, clearWhileThreadIsBlocking)
170 {
171  SubscriptionQueue queue("blah", 1, false);
172 
173  FakeSubHelperPtr helper(boost::make_shared<FakeSubHelper>());
174  MessageDeserializerPtr des(boost::make_shared<MessageDeserializer>(helper, SerializedMessage(), boost::shared_ptr<M_string>()));
175 
176  bool done = false;
177  boost::barrier barrier(2);
178  helper->cb_ = boost::bind(clearWhileThreadIsBlockingCallback, &done, &barrier);
179  queue.push(helper, des, false, VoidConstWPtr(), true);
180  boost::thread t(callThread, boost::ref(queue));
181  barrier.wait();
182 
183  queue.clear();
184 
185  ASSERT_TRUE(done);
186 }
187 
188 void waitForBarrier(boost::barrier* bar)
189 {
190  bar->wait();
191 }
192 
193 TEST(SubscriptionQueue, concurrentCallbacks)
194 {
195  SubscriptionQueue queue("blah", 0, true);
196  FakeSubHelperPtr helper(boost::make_shared<FakeSubHelper>());
197  MessageDeserializerPtr des(boost::make_shared<MessageDeserializer>(helper, SerializedMessage(), boost::shared_ptr<M_string>()));
198 
199  boost::barrier bar(2);
200  helper->cb_ = boost::bind(waitForBarrier, &bar);
201  queue.push(helper, des, false, VoidConstWPtr(), true);
202  queue.push(helper, des, false, VoidConstWPtr(), true);
203  boost::thread t1(callThread, boost::ref(queue));
204  boost::thread t2(callThread, boost::ref(queue));
205  t1.join();
206  t2.join();
207 
208  ASSERT_EQ(helper->calls_, 2u);
209 }
210 
212 {
213  ros::WallDuration(1.0).sleep();
214 }
215 
216 TEST(SubscriptionQueue, nonConcurrentOrdering)
217 {
218  SubscriptionQueue queue("blah", 0, false);
219  FakeSubHelperPtr helper(boost::make_shared<FakeSubHelper>());
220  MessageDeserializerPtr des(boost::make_shared<MessageDeserializer>(helper, SerializedMessage(), boost::shared_ptr<M_string>()));
221 
222  helper->cb_ = waitForASecond;
223  queue.push(helper, des, false, VoidConstWPtr(), true);
224  queue.push(helper, des, false, VoidConstWPtr(), true);
225  boost::thread t1(callThread, boost::ref(queue));
226  boost::thread t2(callThread, boost::ref(queue));
227  t1.join();
228  t2.join();
229 
230  ASSERT_EQ(helper->calls_, 1u);
231  queue.call();
232  ASSERT_EQ(helper->calls_, 2u);
233 }
234 
235 int main(int argc, char** argv)
236 {
237  testing::InitGoogleTest(&argc, argv);
238  ros::init(argc, argv, "blah");
239  return RUN_ALL_TESTS();
240 }
ros::CallbackInterface::Invalid
Invalid
t
ros::WallTime t
Definition: pointcloud_serdes.cpp:41
ros::WallDuration::sleep
bool sleep() const
ros::SerializedMessage
boost::shared_ptr
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
ros
subscription_callback_helper.h
clearWhileThreadIsBlockingCallback
void clearWhileThreadIsBlockingCallback(bool *done, boost::barrier *barrier)
Definition: test_subscription_queue.cpp:157
callback_queue_interface.h
clearInCallbackCallback
void clearInCallbackCallback(SubscriptionQueue &queue)
Definition: test_subscription_queue.cpp:140
init.h
waitForBarrier
void waitForBarrier(boost::barrier *bar)
Definition: test_subscription_queue.cpp:188
waitForASecond
void waitForASecond()
Definition: test_subscription_queue.cpp:211
subscription_queue.h
ros::SubscriptionQueue::push
void push(const SubscriptionCallbackHelperPtr &helper, const MessageDeserializerPtr &deserializer, bool has_tracked_object, const VoidConstWPtr &tracked_object, bool nonconst_need_copy, ros::Time receipt_time=ros::Time(), bool *was_full=0)
TEST
TEST(SubscriptionQueue, queueSize)
Definition: test_subscription_queue.cpp:51
ros::SubscriptionQueue::call
virtual CallbackInterface::CallResult call()
fake_message.h
ros::CallbackInterface::Success
Success
message_deserializer.h
ros::VoidConstWPtr
boost::weak_ptr< void const > VoidConstWPtr
main
int main(int argc, char **argv)
Definition: test_subscription_queue.cpp:235
ros::SubscriptionQueue::ready
virtual bool ready()
ros::SubscriptionQueue
ros::SubscriptionQueue::clear
void clear()
ros::SubscriptionQueue::full
bool full()
ros::WallDuration
callThread
void callThread(SubscriptionQueue &queue)
Definition: test_subscription_queue.cpp:164


test_roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas , Jacob Perron
autogenerated on Tue May 20 2025 03:00:30