Go to the documentation of this file.00001 #include "ros/ros.h"
00002 #include "std_msgs/String.h"
00003
00004 namespace rosserial {
00005 #include "rosserial_test/ros.h"
00006 #include "rosserial/std_msgs/String.h"
00007 }
00008
00009 #include <gtest/gtest.h>
00010 #include "rosserial_test/fixture.h"
00011 #include "rosserial_test/helpers.h"
00012
00017 TEST_F(SingleClientFixture, single_publish) {
00018
00019 rosserial::std_msgs::String string_msg;
00020 rosserial::ros::Publisher client_pub("chatter", &string_msg);
00021 client_nh.advertise(client_pub);
00022 client_nh.initNode();
00023 char s[] = "from-rosserial-client";
00024 string_msg.data = s;
00025
00026
00027 StringCallback str_callback;
00028 ros::Subscriber check_sub = nh.subscribe("chatter", 1, &StringCallback::callback, &str_callback);
00029
00030 for(int attempt = 0; attempt < 50; attempt++) {
00031 client_pub.publish(&string_msg);
00032 client_nh.spinOnce();
00033 ros::spinOnce();
00034 if (str_callback.times_called > 0) break;
00035 ros::Duration(0.1).sleep();
00036 }
00037 EXPECT_GT(str_callback.times_called, 0);
00038 EXPECT_STREQ(s, str_callback.last_msg.data.c_str());
00039 }
00040
00041 int rosserial_string_cb_count = 0;
00042 std::string last_string;
00043
00044 static void rosserial_string_cb(const rosserial::std_msgs::String& msg)
00045 {
00046 rosserial_string_cb_count++;
00047 last_string = std::string(msg.data);
00048 }
00049
00054 TEST_F(SingleClientFixture, single_subscribe) {
00055 rosserial::ros::Subscriber<rosserial::std_msgs::String> client_sub("chatter", rosserial_string_cb);
00056 client_nh.subscribe(client_sub);
00057 client_nh.initNode();
00058
00059 ros::Publisher pub = nh.advertise<std_msgs::String>("chatter", 1);
00060
00061 std_msgs::String string_msg;
00062 string_msg.data = "to-rosserial-client";
00063 for(int attempt = 0; attempt < 50; attempt++) {
00064 pub.publish(string_msg);
00065 ros::spinOnce();
00066 client_nh.spinOnce();
00067 if (rosserial_string_cb_count > 0) break;
00068 ros::Duration(0.1).sleep();
00069 }
00070 EXPECT_GT(rosserial_string_cb_count, 0);
00071 EXPECT_EQ(string_msg.data, last_string);
00072 }
00073
00074 int main(int argc, char **argv){
00075 ros::init(argc, argv, "test_publish_subscribe");
00076 ros::start();
00077 testing::InitGoogleTest(&argc, argv);
00078 return RUN_ALL_TESTS();
00079 }