server_client_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, 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 the 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 #include <ros/ros.h>
32 #include <ros/console.h>
33 
34 #include <gtest/gtest.h>
35 
36 #include <tf/transform_listener.h>
37 
40 
41 #define DBG_MSG( ... ) printf( __VA_ARGS__ ); printf("\n");
42 #define DBG_MSG_STREAM( ... ) std::cout << __VA_ARGS__ << std::endl;
43 
44 using namespace interactive_markers;
45 
50 
51 
52 std::string reset_server_id;
53 
54 typedef visualization_msgs::InteractiveMarkerInitConstPtr InitConstPtr;
55 typedef visualization_msgs::InteractiveMarkerUpdateConstPtr UpdateConstPtr;
56 
59 
61 {
62  update_calls = 0;
63  init_calls = 0;
64  reset_calls = 0;
65  status_calls = 0;
66  reset_server_id = "";
67  init_msg.reset();
68  update_msg.reset();
69 }
70 
71 void updateCb( const UpdateConstPtr& msg )
72 {
73  DBG_MSG("updateCb called");
74  update_calls++;
75  update_msg = msg;
76 }
77 
78 void initCb( const InitConstPtr& msg )
79 {
80  DBG_MSG("initCb called");
81  init_calls++;
82  init_msg = msg;
83 }
84 
86  const std::string& server_id,
87  const std::string& msg )
88 {
89  DBG_MSG("statusCb called");
90  status_calls++;
91  DBG_MSG_STREAM( (int)status << " " << server_id << ": " << msg );
92 }
93 
94 void resetCb( const std::string& server_id )
95 {
96  DBG_MSG("resetCb( %s ) called", server_id.c_str() );
97  reset_calls++;
98  reset_server_id = server_id;
99 }
100 
101 void waitMsg()
102 {
103  for(int i=0;i<10;i++)
104  {
105  ros::spinOnce();
106  usleep(1000);
107  }
108 }
109 
110 TEST(InteractiveMarkerServerAndClient, connect_tf_error)
111 {
113 
114  // create an interactive marker server on the topic namespace simple_marker
115  interactive_markers::InteractiveMarkerServer server("im_server_client_test","test_server",false);
116  visualization_msgs::InteractiveMarker int_marker;
117  int_marker.name = "marker1";
118  int_marker.header.frame_id = "valid_frame";
119 
120  waitMsg();
121 
123 
124  interactive_markers::InteractiveMarkerClient client(tf, "valid_frame", "im_server_client_test");
125  client.setInitCb( &initCb );
126  client.setStatusCb( &statusCb );
127  client.setResetCb( &resetCb );
128  client.setUpdateCb( &updateCb );
129 
130  // Add one marker -> init should get called once
131  DBG_MSG("----------------------------------------");
132 
133  server.insert(int_marker);
134  server.applyChanges();
135  waitMsg();
136  client.update();
137 
138  ASSERT_EQ( 0, update_calls );
139  ASSERT_EQ( 1, init_calls );
140  ASSERT_EQ( 0, reset_calls );
141  ASSERT_TRUE( init_msg );
142  ASSERT_EQ( 1, init_msg->markers.size() );
143  ASSERT_EQ( "marker1", init_msg->markers[0].name );
144  ASSERT_EQ( "valid_frame", init_msg->markers[0].header.frame_id );
145 
146  // Add another marker -> update should get called once
147  DBG_MSG("----------------------------------------");
148 
150 
151  int_marker.name = "marker2";
152 
153  server.insert(int_marker);
154  server.applyChanges();
155  waitMsg();
156  client.update();
157 
158  ASSERT_EQ( 1, update_calls );
159  ASSERT_EQ( 0, init_calls );
160  ASSERT_EQ( 0, reset_calls );
161  ASSERT_TRUE( update_msg );
162  ASSERT_EQ( 1, update_msg->markers.size() );
163  ASSERT_EQ( 0, update_msg->poses.size() );
164  ASSERT_EQ( 0, update_msg->erases.size() );
165  ASSERT_EQ( "marker2", update_msg->markers[0].name );
166 
167  // Make marker tf info invalid -> connection should be reset
168  DBG_MSG("----------------------------------------");
169 
171 
172  int_marker.header.frame_id = "invalid_frame";
173 
174  server.insert(int_marker);
175  server.applyChanges();
176  waitMsg();
177  client.update();
178 
179  ASSERT_EQ( 0, update_calls );
180  ASSERT_EQ( 0, init_calls );
181  ASSERT_EQ( 1, reset_calls );
182  ASSERT_TRUE( reset_server_id.find("/test_server") != std::string::npos );
183 
184  // Make marker tf info valid again -> connection should be successfully initialized again
185  DBG_MSG("----------------------------------------");
186 
187  usleep(2000000);
188  waitMsg();
189  client.update();
190 
192 
193  int_marker.header.frame_id = "valid_frame";
194 
195  server.insert(int_marker);
196  server.applyChanges();
197  waitMsg();
198  client.update();
199 
200  ASSERT_EQ( 0, update_calls );
201  ASSERT_EQ( 1, init_calls );
202  ASSERT_EQ( 0, reset_calls );
203 
204  // Erase marker
205  DBG_MSG("----------------------------------------");
206 
208 
209  server.erase("marker1");
210  server.applyChanges();
211  waitMsg();
212  client.update();
213 
214  ASSERT_EQ( 1, update_calls );
215  ASSERT_EQ( 0, init_calls );
216  ASSERT_EQ( 0, reset_calls );
217  ASSERT_TRUE( update_msg );
218  ASSERT_EQ( 0, update_msg->markers.size() );
219  ASSERT_EQ( 0, update_msg->poses.size() );
220  ASSERT_EQ( 1, update_msg->erases.size() );
221  ASSERT_EQ( "marker1", update_msg->erases[0] );
222 
223  // Update pose
224  DBG_MSG("----------------------------------------");
225 
227 
228  server.setPose( "marker2", int_marker.pose, int_marker.header );
229  server.applyChanges();
230  waitMsg();
231  client.update();
232 
233  ASSERT_EQ( 1, update_calls );
234  ASSERT_EQ( 0, init_calls );
235  ASSERT_EQ( 0, reset_calls );
236  ASSERT_TRUE( update_msg );
237  ASSERT_EQ( 0, update_msg->markers.size() );
238  ASSERT_EQ( 1, update_msg->poses.size() );
239  ASSERT_EQ( 0, update_msg->erases.size() );
240  ASSERT_EQ( "marker2", update_msg->poses[0].name );
241 }
242 
243 
244 // Run all the tests that were declared with TEST()
245 int main(int argc, char **argv)
246 {
247  ros::init(argc, argv, "im_server_client_test");
248  testing::InitGoogleTest(&argc, argv);
249  return RUN_ALL_TESTS();
250 }
int status_calls
int main(int argc, char **argv)
UpdateConstPtr update_msg
void resetReceivedMsgs()
void statusCb(InteractiveMarkerClient::StatusT status, const std::string &server_id, const std::string &msg)
int reset_calls
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
boost::shared_ptr< interactive_markers::InteractiveMarkerServer > server
Definition: bursty_tf.cpp:40
void setInitCb(const InitCallback &cb)
Set callback for init messages.
std::string reset_server_id
TEST(InteractiveMarkerServerAndClient, connect_tf_error)
int init_calls
void setResetCb(const ResetCallback &cb)
Set callback for resetting one server connection.
void setUpdateCb(const UpdateCallback &cb)
Set callback for update messages.
void setStatusCb(const StatusCallback &cb)
Set callback for status updates.
visualization_msgs::InteractiveMarkerUpdateConstPtr UpdateConstPtr
void waitMsg()
void initCb(const InitConstPtr &msg)
InitConstPtr init_msg
void insert(const visualization_msgs::InteractiveMarker &int_marker)
void update()
Update tf info, call callbacks.
void resetCb(const std::string &server_id)
int update_calls
bool setPose(const std::string &name, const geometry_msgs::Pose &pose, const std_msgs::Header &header=std_msgs::Header())
ROSCPP_DECL void spinOnce()
#define DBG_MSG(...)
void updateCb(const UpdateConstPtr &msg)
#define DBG_MSG_STREAM(...)
visualization_msgs::InteractiveMarkerInitConstPtr InitConstPtr


interactive_markers
Author(s): David Gossow
autogenerated on Sun Feb 3 2019 03:24:09