34 #include <gtest/gtest.h>
39 #define DBG_MSG( ... ) printf( __VA_ARGS__ ); printf("\n");
40 #define DBG_MSG_STREAM( ... ) std::cout << __VA_ARGS__ << std::endl;
63 std::string server_id;
67 std::vector<std::string> expect_reset_calls;
68 std::vector<int> expect_init_seq_num;
69 std::vector<int> expect_update_seq_num;
77 typedef visualization_msgs::InteractiveMarkerInitConstPtr
InitConstPtr;
86 recv_init_msgs.clear();
87 recv_update_msgs.clear();
88 recv_reset_calls.clear();
94 recv_update_msgs.push_back( *msg );
100 recv_init_msgs.push_back( *msg );
104 const std::string& server_id,
105 const std::string& msg )
107 std::string status_string[]={
"INFO",
"WARN",
"ERROR"};
108 ASSERT_TRUE( (
unsigned)status < 3 );
109 DBG_MSG_STREAM(
"(" << status_string[(
unsigned)status] <<
") " << server_id <<
": " << msg );
115 recv_reset_calls.push_back(server_id);
119 void test( std::vector<Msg> messages )
125 visualization_msgs::InteractiveMarker int_marker;
126 int_marker.pose.orientation.w=1;
128 std::string topic_ns =
"im_client_test";
137 std::map< int, visualization_msgs::InteractiveMarkerInit > sent_init_msgs;
138 std::map< int, visualization_msgs::InteractiveMarkerUpdate > sent_update_msgs;
140 for (
size_t i=0; i<messages.size(); i++ )
144 Msg& msg = messages[i];
146 int_marker.
header.frame_id=msg.frame_id;
147 int_marker.header.stamp=msg.stamp;
148 int_marker.pose.orientation.w = 1.0;
150 std::ostringstream
s;
152 int_marker.name=
s.str();
158 DBG_MSG_STREAM( i <<
" INIT: seq_num=" << msg.seq_num <<
" frame=" << msg.frame_id <<
" stamp=" << msg.stamp );
159 visualization_msgs::InteractiveMarkerInitPtr init_msg_out(
new visualization_msgs::InteractiveMarkerInit() );
160 init_msg_out->server_id=msg.server_id;
161 init_msg_out->seq_num=msg.seq_num;
162 init_msg_out->markers.push_back( int_marker );
164 sent_init_msgs[msg.seq_num]=*init_msg_out;
167 case Msg::KEEP_ALIVE:
170 visualization_msgs::InteractiveMarkerUpdatePtr update_msg_out(
new visualization_msgs::InteractiveMarkerUpdate() );
171 update_msg_out->server_id=msg.server_id;
172 update_msg_out->type = visualization_msgs::InteractiveMarkerUpdate::KEEP_ALIVE;
173 update_msg_out->seq_num=msg.seq_num;
176 sent_update_msgs[msg.seq_num]=*update_msg_out;
181 DBG_MSG_STREAM( i <<
" UPDATE: seq_num=" << msg.seq_num <<
" frame=" << msg.frame_id <<
" stamp=" << msg.stamp );
182 visualization_msgs::InteractiveMarkerUpdatePtr update_msg_out(
new visualization_msgs::InteractiveMarkerUpdate() );
183 update_msg_out->server_id=msg.server_id;
184 update_msg_out->type = visualization_msgs::InteractiveMarkerUpdate::UPDATE;
185 update_msg_out->seq_num=msg.seq_num;
187 update_msg_out->markers.push_back( int_marker );
189 sent_update_msgs[msg.seq_num]=*update_msg_out;
194 DBG_MSG_STREAM( i <<
" POSE: seq_num=" << msg.seq_num <<
" frame=" << msg.frame_id <<
" stamp=" << msg.stamp );
195 visualization_msgs::InteractiveMarkerUpdatePtr update_msg_out(
new visualization_msgs::InteractiveMarkerUpdate() );
196 update_msg_out->server_id=msg.server_id;
197 update_msg_out->type = visualization_msgs::InteractiveMarkerUpdate::UPDATE;
198 update_msg_out->seq_num=msg.seq_num;
200 visualization_msgs::InteractiveMarkerPose pose;
201 pose.header=int_marker.header;
202 pose.name=int_marker.name;
203 pose.pose=int_marker.pose;
204 update_msg_out->poses.push_back( pose );
206 sent_update_msgs[msg.seq_num]=*update_msg_out;
212 visualization_msgs::InteractiveMarkerUpdatePtr update_msg_out(
new visualization_msgs::InteractiveMarkerUpdate() );
213 update_msg_out->server_id=
"/im_client_test/test_server";
214 update_msg_out->type = visualization_msgs::InteractiveMarkerUpdate::UPDATE;
215 update_msg_out->seq_num=msg.seq_num;
217 update_msg_out->erases.push_back( int_marker.name );
219 sent_update_msgs[msg.seq_num]=*update_msg_out;
225 geometry_msgs::TransformStamped stf;
226 stf.header.frame_id = msg.frame_id;
227 stf.header.stamp = msg.stamp;
229 stf.transform.rotation.w = 1.0;
243 ASSERT_EQ( msg.expect_update_seq_num.size(), recv_update_msgs.size() );
244 ASSERT_EQ( msg.expect_init_seq_num.size(), recv_init_msgs.size() );
245 ASSERT_EQ( msg.expect_reset_calls.size(), recv_reset_calls.size() );
247 for (
size_t u=0; u<msg.expect_update_seq_num.size(); u++ )
249 ASSERT_TRUE( sent_update_msgs.find(msg.expect_update_seq_num[u]) != sent_update_msgs.end() );
251 visualization_msgs::InteractiveMarkerUpdate sent_msg = sent_update_msgs[msg.expect_update_seq_num[u]];
252 visualization_msgs::InteractiveMarkerUpdate recv_msg = recv_update_msgs[ u ];
255 ASSERT_EQ( sent_msg.seq_num, msg.expect_update_seq_num[u] );
258 ASSERT_EQ( recv_msg.seq_num, msg.expect_update_seq_num[u] );
261 ASSERT_EQ( recv_msg.markers.size(), sent_msg.markers.size() );
262 ASSERT_EQ( recv_msg.poses.size(), sent_msg.poses.size() );
263 ASSERT_EQ( recv_msg.erases.size(), sent_msg.erases.size() );
267 for (
size_t m=0; m<sent_msg.markers.size(); m++ )
269 ASSERT_EQ( recv_msg.markers[m].name, sent_msg.markers[m].name );
270 ASSERT_EQ( recv_msg.markers[m].header.stamp, sent_msg.markers[m].header.stamp );
271 if ( sent_msg.markers[m].header.stamp ==
ros::Time(0) )
273 ASSERT_EQ(
target_frame, sent_msg.markers[m].header.frame_id );
277 ASSERT_EQ(
target_frame, recv_msg.markers[m].header.frame_id );
280 for (
size_t p=0; p<sent_msg.poses.size(); p++ )
282 ASSERT_EQ( recv_msg.poses[p].name, sent_msg.poses[p].name );
283 ASSERT_EQ( recv_msg.poses[p].header.stamp, sent_msg.poses[p].header.stamp );
284 if ( sent_msg.poses[p].header.stamp ==
ros::Time(0) )
286 ASSERT_EQ(
target_frame, sent_msg.poses[p].header.frame_id );
290 ASSERT_EQ(
target_frame, recv_msg.poses[p].header.frame_id );
293 for (
size_t e=0; e<sent_msg.erases.size(); e++ )
295 ASSERT_EQ( recv_msg.erases[e], sent_msg.erases[e] );
300 for (
size_t u=0; u<msg.expect_init_seq_num.size(); u++ )
302 ASSERT_TRUE( sent_init_msgs.find(msg.expect_init_seq_num[u]) != sent_init_msgs.end() );
304 visualization_msgs::InteractiveMarkerInit sent_msg = sent_init_msgs[msg.expect_init_seq_num[u]];
305 visualization_msgs::InteractiveMarkerInit recv_msg = recv_init_msgs[ u ];
308 ASSERT_EQ( sent_msg.seq_num, msg.expect_init_seq_num[u] );
311 ASSERT_EQ( recv_msg.seq_num, msg.expect_init_seq_num[u] );
314 ASSERT_EQ( recv_msg.markers.size(), sent_msg.markers.size() );
318 for (
size_t m=0; m<sent_msg.markers.size(); m++ )
320 ASSERT_EQ( recv_msg.markers[m].name, sent_msg.markers[m].name );
321 ASSERT_EQ( recv_msg.markers[m].header.stamp, sent_msg.markers[m].header.stamp );
322 if ( sent_msg.markers[m].header.stamp ==
ros::Time(0) )
330 ASSERT_EQ(
target_frame, recv_msg.markers[m].header.frame_id );
342 std::vector<Msg> seq;
346 msg.server_id=
"server1";
350 msg.type=Msg::KEEP_ALIVE;
351 msg.expect_init_seq_num.push_back(0);
362 std::vector<Msg> seq;
366 msg.server_id=
"server1";
370 msg.type=Msg::UPDATE;
371 msg.expect_init_seq_num.push_back(0);
383 std::vector<Msg> seq;
385 for (
int i=0; i<200; i++ )
389 msg.server_id=
"server1";
395 msg.type=Msg::UPDATE;
396 msg.expect_init_seq_num.push_back(msg.seq_num);
401 msg.expect_init_seq_num.clear();
402 msg.expect_update_seq_num.push_back(msg.seq_num);
413 std::vector<Msg> seq;
415 for (
int i=0; i<200; i++ )
417 msg.type=Msg::UPDATE;
419 msg.server_id=
"server1";
426 msg.expect_init_seq_num.push_back(msg.seq_num);
427 for (
int i=191; i<200; i++ )
429 msg.expect_update_seq_num.push_back(i);
441 std::vector<Msg> seq;
445 msg.server_id=
"server1";
446 msg.frame_id=
"invalid_frame";
451 msg.server_id=
"server1";
455 msg.type=Msg::KEEP_ALIVE;
456 msg.expect_init_seq_num.push_back(1);
467 std::vector<Msg> seq;
470 msg.type=Msg::TF_INFO;
471 msg.server_id=
"server1";
472 msg.frame_id=
"wait_frame";
482 msg.type=Msg::KEEP_ALIVE;
486 msg.type=Msg::TF_INFO;
487 msg.expect_init_seq_num.push_back(0);
491 msg.type=Msg::UPDATE;
494 msg.expect_init_seq_num.clear();
498 msg.type=Msg::TF_INFO;
499 msg.expect_update_seq_num.push_back(1);
506 msg.expect_update_seq_num.clear();
510 msg.type=Msg::TF_INFO;
511 msg.expect_update_seq_num.push_back(2);
523 std::vector<Msg> seq;
527 msg.server_id=
"server1";
528 msg.frame_id=
"wait_frame";
533 msg.type=Msg::KEEP_ALIVE;
537 msg.type=Msg::TF_INFO;
539 msg.expect_init_seq_num.push_back(0);
555 std::vector<Msg> seq;
558 msg.type=Msg::TF_INFO;
559 msg.server_id=
"server1";
560 msg.frame_id=
"wait_frame";
574 msg.type=Msg::KEEP_ALIVE;
578 msg.type=Msg::UPDATE;
583 msg.type=Msg::UPDATE;
588 msg.type=Msg::UPDATE;
593 msg.type=Msg::TF_INFO;
597 msg.type=Msg::TF_INFO;
601 msg.type=Msg::TF_INFO;
608 msg.expect_init_seq_num.push_back(1);
609 msg.expect_update_seq_num.push_back(2);
610 msg.expect_update_seq_num.push_back(3);
625 std::vector<Msg> seq;
628 msg.type=Msg::TF_INFO;
629 msg.server_id=
"server1";
630 msg.frame_id=
"wait_frame";
638 msg.type=Msg::KEEP_ALIVE;
640 msg.expect_init_seq_num.push_back(0);
643 msg.expect_init_seq_num.clear();
647 msg.type=Msg::UPDATE;
652 msg.type=Msg::UPDATE;
657 msg.type=Msg::UPDATE;
662 msg.type=Msg::TF_INFO;
666 msg.type=Msg::TF_INFO;
671 msg.type=Msg::TF_INFO;
673 msg.expect_update_seq_num.push_back(1);
674 msg.expect_update_seq_num.push_back(2);
675 msg.expect_update_seq_num.push_back(3);
687 std::vector<Msg> seq;
691 msg.server_id=
"server1";
695 msg.type=Msg::KEEP_ALIVE;
696 msg.expect_init_seq_num.push_back(0);
699 msg.expect_init_seq_num.clear();
701 msg.type=Msg::KEEP_ALIVE;
703 msg.expect_reset_calls.push_back(msg.server_id);
714 std::vector<Msg> seq;
718 msg.server_id=
"server1";
722 msg.type=Msg::KEEP_ALIVE;
723 msg.expect_init_seq_num.push_back(1);
726 msg.expect_init_seq_num.clear();
728 msg.type=Msg::KEEP_ALIVE;
730 msg.expect_reset_calls.push_back(msg.server_id);
741 std::vector<Msg> seq;
745 msg.server_id=
"server1";
749 msg.type=Msg::KEEP_ALIVE;
750 msg.expect_init_seq_num.push_back(1);
753 msg.expect_init_seq_num.clear();
755 msg.type=Msg::UPDATE;
757 msg.expect_reset_calls.push_back(msg.server_id);
768 std::vector<Msg> seq;
772 msg.server_id=
"server1";
776 msg.type=Msg::KEEP_ALIVE;
777 msg.expect_init_seq_num.push_back(1);
780 msg.expect_init_seq_num.clear();
782 msg.type=Msg::UPDATE;
784 msg.expect_update_seq_num.push_back(2);
787 msg.expect_update_seq_num.clear();
789 msg.type=Msg::UPDATE;
791 msg.expect_reset_calls.push_back(msg.server_id);
802 std::vector<Msg> seq;
806 msg.server_id=
"server1";
810 msg.type=Msg::KEEP_ALIVE;
811 msg.expect_init_seq_num.push_back(1);
814 msg.expect_init_seq_num.clear();
816 msg.type=Msg::UPDATE;
818 msg.expect_update_seq_num.push_back(2);
821 msg.expect_update_seq_num.clear();
823 msg.type=Msg::UPDATE;
825 msg.expect_reset_calls.push_back(msg.server_id);
836 std::vector<Msg> seq;
840 msg.server_id=
"server1";
844 msg.type=Msg::KEEP_ALIVE;
845 msg.expect_init_seq_num.push_back(1);
848 msg.expect_init_seq_num.clear();
850 msg.type=Msg::UPDATE;
852 msg.expect_update_seq_num.push_back(2);
855 msg.expect_update_seq_num.clear();
857 msg.type=Msg::UPDATE;
859 msg.expect_reset_calls.push_back(msg.server_id);
871 std::vector<Msg> seq;
875 msg.server_id=
"server1";
879 msg.type=Msg::KEEP_ALIVE;
880 msg.expect_init_seq_num.push_back(0);
883 msg.expect_init_seq_num.clear();
885 msg.type=Msg::UPDATE;
887 msg.expect_update_seq_num.push_back(1);
890 msg.expect_update_seq_num.clear();
894 msg.server_id=
"server2";
898 msg.type=Msg::KEEP_ALIVE;
899 msg.expect_init_seq_num.push_back(0);
902 msg.expect_init_seq_num.clear();
904 msg.type=Msg::UPDATE;
906 msg.expect_update_seq_num.push_back(1);
915 int main(
int argc,
char **argv)
917 testing::InitGoogleTest(&argc, argv);
920 return RUN_ALL_TESTS();