subscribe_retry_tcp.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 /*
31  * Author: Josh Faust
32  */
33 
34 #include <gtest/gtest.h>
35 
36 #include <ros/ros.h>
37 #include <ros/connection_manager.h>
38 
39 #include "test_roscpp/TestArray.h"
40 #include "roscpp/Empty.h"
41 
42 int32_t g_count = 0;
43 void callback(const test_roscpp::TestArrayConstPtr&)
44 {
45  ++g_count;
46 }
47 
48 TEST(SubscribeRetryTCP, localDisconnect)
49 {
50  g_count = 0;
51  ros::NodeHandle nh;
52  ros::Subscriber sub = nh.subscribe("roscpp/pubsub_test", 0, callback);
53  // wait for initial messages to arrive
55  while (g_count == 0 && ros::WallTime::now() - start < ros::WallDuration(5.0))
56  {
57  ros::WallDuration(0.01).sleep();
58  ros::spinOnce();
59  }
60 
61  ASSERT_GT(g_count, 0);
62 
64 
65  // spin to make sure all previous messages have arrived
66  ros::spinOnce();
67 
68  g_count = 0;
69  // wait for reconnect/new messages to arrive
71  while (g_count == 0 && ros::WallTime::now() - start < ros::WallDuration(5.0))
72  {
73  ros::WallDuration(0.01).sleep();
74  ros::spinOnce();
75  }
76 
77  ASSERT_GT(g_count, 0);
78 }
79 
80 TEST(SubscribeRetryTCP, localDisconnectNonTransportDisconnect)
81 {
82  g_count = 0;
83  ros::NodeHandle nh;
84  ros::Subscriber sub = nh.subscribe("roscpp/pubsub_test", 0, callback);
85  // wait for initial messages to arrive
87  while (g_count == 0 && ros::WallTime::now() - start < ros::WallDuration(5.0))
88  {
89  ros::WallDuration(0.01).sleep();
90  ros::spinOnce();
91  }
92 
93  ASSERT_GT(g_count, 0);
94 
96 
97  // spin for a bit to make sure all previous messages have arrived
99  while (ros::WallTime::now() - start < ros::WallDuration(1.0))
100  {
101  ros::WallDuration(0.01).sleep();
102  ros::spinOnce();
103  }
104 
105  g_count = 0;
106  // make sure we did not reconnect
108  while (g_count == 0 && ros::WallTime::now() - start < ros::WallDuration(5.0))
109  {
110  ros::WallDuration(0.01).sleep();
111  ros::spinOnce();
112  }
113 
114  ASSERT_EQ(g_count, 0);
115 }
116 
117 TEST(SubscribeRetryTCP, remoteDisconnect)
118 {
119  g_count = 0;
120  ros::NodeHandle nh;
121  ros::Subscriber sub = nh.subscribe("roscpp/pubsub_test", 0, callback);
122  // wait for initial messages to arrive
124  while (g_count == 0 && ros::WallTime::now() - start < ros::WallDuration(5.0))
125  {
126  ros::WallDuration(0.01).sleep();
127  ros::spinOnce();
128  }
129 
130  ASSERT_GT(g_count, 0);
131 
132  roscpp::Empty::Request req;
133  roscpp::Empty::Response resp;
134  ros::service::call("/publish_constantly/debug/close_all_connections", req, resp);
135 
136  // spin to make sure all previous messages have arrived
137  ros::spinOnce();
138 
139  g_count = 0;
140  // wait for reconnect/new messages to arrive
142  while (g_count == 0 && ros::WallTime::now() - start < ros::WallDuration(5.0))
143  {
144  ros::WallDuration(0.01).sleep();
145  ros::spinOnce();
146  }
147 
148  ASSERT_GT(g_count, 0);
149 }
150 
151 int main(int argc, char** argv)
152 {
153  testing::InitGoogleTest(&argc, argv);
154  ros::init(argc, argv, "subscribe_retry_tcp");
155  ros::NodeHandle nh;
156  return RUN_ALL_TESTS();
157 }
ros::WallDuration::sleep
bool sleep() const
ros::service::call
bool call(const std::string &service_name, MReq &req, MRes &res)
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
ros::Connection::HeaderError
HeaderError
main
int main(int argc, char **argv)
Definition: subscribe_retry_tcp.cpp:151
ros.h
callback
void callback(const test_roscpp::TestArrayConstPtr &)
Definition: subscribe_retry_tcp.cpp:43
ros::spinOnce
ROSCPP_DECL void spinOnce()
connection_manager.h
ros::Connection::TransportDisconnect
TransportDisconnect
ros::WallTime::now
static WallTime now()
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
ros::WallTime
g_count
int32_t g_count
Definition: subscribe_retry_tcp.cpp:42
start
ROSCPP_DECL void start()
TEST
TEST(SubscribeRetryTCP, localDisconnect)
Definition: subscribe_retry_tcp.cpp:48
ros::ConnectionManager::instance
static const ConnectionManagerPtr & instance()
ros::WallDuration
ros::NodeHandle
ros::Subscriber


test_roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas , Jacob Perron
autogenerated on Thu Nov 23 2023 04:02:02