00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <gtest/gtest.h>
00035
00036 #include <ros/ros.h>
00037 #include <ros/connection_manager.h>
00038
00039 #include "test_roscpp/TestEmpty.h"
00040 #include "test_roscpp/TestArray.h"
00041
00042 #include <std_srvs/Empty.h>
00043
00044 class AnyMessage
00045 {
00046 };
00047 typedef boost::shared_ptr<AnyMessage> AnyMessagePtr;
00048 typedef boost::shared_ptr<AnyMessage const> AnyMessageConstPtr;
00049
00050 namespace ros
00051 {
00052 namespace message_traits
00053 {
00054
00055 template<>
00056 struct MD5Sum<AnyMessage>
00057 {
00058 static const char* value() { return "*"; }
00059 static const char* value(const AnyMessage&) { return "*"; }
00060 };
00061
00062 template<>
00063 struct DataType<AnyMessage>
00064 {
00065 static const char* value() { return "*"; }
00066 static const char* value(const AnyMessage&) { return "*"; }
00067 };
00068
00069 template<>
00070 struct Definition<AnyMessage>
00071 {
00072 };
00073
00074 }
00075
00076 namespace serialization
00077 {
00078 template<>
00079 struct Serializer<AnyMessage>
00080 {
00081 template<typename Stream, typename T>
00082 static void allInOne(Stream s, T t)
00083 {
00084 }
00085
00086 ROS_DECLARE_ALLINONE_SERIALIZER;
00087 };
00088 }
00089 }
00090
00091 struct AnyHelper
00092 {
00093 AnyHelper()
00094 : count(0)
00095 {
00096 }
00097
00098 void cb(const AnyMessageConstPtr& msg)
00099 {
00100 ++count;
00101 }
00102
00103 uint32_t count;
00104 };
00105
00106
00107 TEST(SubscribeStar, simpleSubFirstIntra)
00108 {
00109 ros::NodeHandle nh;
00110 AnyHelper h;
00111 ros::Subscriber sub = nh.subscribe("test_star_intra", 0, &AnyHelper::cb, &h);
00112 ros::Publisher pub = nh.advertise<test_roscpp::TestEmpty>("test_star_intra", 0);
00113
00114 EXPECT_EQ(pub.getNumSubscribers(), 1U);
00115 EXPECT_EQ(sub.getNumPublishers(), 1U);
00116
00117 AnyMessagePtr msg(new AnyMessage);
00118 pub.publish(msg);
00119 ros::spinOnce();
00120 EXPECT_EQ(h.count, 1U);
00121 }
00122
00123 TEST(SubscribeStar, simplePubFirstIntra)
00124 {
00125 ros::NodeHandle nh;
00126 AnyHelper h;
00127 ros::Publisher pub = nh.advertise<test_roscpp::TestEmpty>("test_star_intra", 0);
00128 ros::Subscriber sub = nh.subscribe("test_star_intra", 0, &AnyHelper::cb, &h);
00129
00130 EXPECT_EQ(pub.getNumSubscribers(), 1U);
00131 EXPECT_EQ(sub.getNumPublishers(), 1U);
00132
00133 AnyMessagePtr msg(new AnyMessage);
00134 pub.publish(msg);
00135 ros::spinOnce();
00136 EXPECT_EQ(h.count, 1U);
00137 }
00138
00139 void emptyCallback(const test_roscpp::TestEmptyConstPtr&)
00140 {
00141
00142 }
00143
00144 TEST(SubscribeStar, multipleSubsStarFirstIntra)
00145 {
00146 ros::NodeHandle nh;
00147 AnyHelper h;
00148 ros::Subscriber sub = nh.subscribe("test_star_intra", 0, &AnyHelper::cb, &h);
00149 ros::Subscriber sub2 = nh.subscribe("test_star_intra", 0, emptyCallback);
00150
00151 ros::Publisher pub = nh.advertise<test_roscpp::TestEmpty>("test_star_intra", 0);
00152 EXPECT_EQ(pub.getNumSubscribers(), 1U);
00153 EXPECT_EQ(sub.getNumPublishers(), 1U);
00154 EXPECT_EQ(sub2.getNumPublishers(), 1U);
00155
00156 pub.shutdown();
00157 pub = nh.advertise<test_roscpp::TestArray>("test_star_intra", 0);
00158 EXPECT_EQ(pub.getNumSubscribers(), 0U);
00159 EXPECT_EQ(sub.getNumPublishers(), 0U);
00160 EXPECT_EQ(sub2.getNumPublishers(), 0U);
00161 }
00162
00163 TEST(SubscribeStar, multipleSubsConcreteFirstIntra)
00164 {
00165 ros::NodeHandle nh;
00166 AnyHelper h;
00167 ros::Subscriber sub2 = nh.subscribe("test_star_intra", 0, emptyCallback);
00168 ros::Subscriber sub = nh.subscribe("test_star_intra", 0, &AnyHelper::cb, &h);
00169
00170 ros::Publisher pub = nh.advertise<test_roscpp::TestEmpty>("test_star_intra", 0);
00171 EXPECT_EQ(pub.getNumSubscribers(), 1U);
00172 EXPECT_EQ(sub.getNumPublishers(), 1U);
00173 EXPECT_EQ(sub2.getNumPublishers(), 1U);
00174
00175 pub.shutdown();
00176 pub = nh.advertise<test_roscpp::TestArray>("test_star_intra", 0);
00177 EXPECT_EQ(pub.getNumSubscribers(), 0U);
00178 EXPECT_EQ(sub.getNumPublishers(), 0U);
00179 EXPECT_EQ(sub2.getNumPublishers(), 0U);
00180 }
00181
00182 TEST(SubscribeStar, multipleShutdownConcreteIntra)
00183 {
00184 ros::NodeHandle nh;
00185 AnyHelper h;
00186 ros::Subscriber sub = nh.subscribe("test_star_intra", 0, &AnyHelper::cb, &h);
00187 ros::Subscriber sub2 = nh.subscribe("test_star_intra", 0, emptyCallback);
00188 sub2.shutdown();
00189
00190 ros::Publisher pub = nh.advertise<test_roscpp::TestEmpty>("test_star_intra", 0);
00191 EXPECT_EQ(pub.getNumSubscribers(), 1U);
00192 EXPECT_EQ(sub.getNumPublishers(), 1U);
00193
00194 pub.shutdown();
00195 pub = nh.advertise<test_roscpp::TestArray>("test_star_intra", 0);
00196 EXPECT_EQ(pub.getNumSubscribers(), 0U);
00197 EXPECT_EQ(sub.getNumPublishers(), 0U);
00198 }
00199
00200 TEST(SubscribeStar, simpleInter)
00201 {
00202 ros::NodeHandle nh;
00203 AnyHelper h;
00204 ros::Subscriber sub = nh.subscribe("test_star_inter", 0, &AnyHelper::cb, &h);
00205
00206 ros::WallDuration(1.0).sleep();
00207 ros::spinOnce();
00208
00209 EXPECT_EQ(sub.getNumPublishers(), 1U);
00210 EXPECT_GT(h.count, 0U);
00211 }
00212
00213 TEST(SubscribeStar, simpleInterUDP)
00214 {
00215 ros::NodeHandle nh;
00216 AnyHelper h;
00217 ros::Subscriber sub = nh.subscribe("test_star_inter", 0, &AnyHelper::cb, &h, ros::TransportHints().udp());
00218
00219 ros::WallDuration(1.0).sleep();
00220 ros::spinOnce();
00221
00222 EXPECT_EQ(sub.getNumPublishers(), 1U);
00223 EXPECT_GT(h.count, 0U);
00224 }
00225
00226
00227 TEST(SubscribeStar, switchTypeInter)
00228 {
00229 ros::NodeHandle nh;
00230 AnyHelper h;
00231 ros::Subscriber sub = nh.subscribe("test_star_inter", 0, &AnyHelper::cb, &h);
00232 ros::Subscriber sub2 = nh.subscribe("test_star_inter", 0, emptyCallback);
00233
00234 ros::WallDuration(1.0).sleep();
00235 ros::spinOnce();
00236
00237 ASSERT_EQ(sub.getNumPublishers(), 1U);
00238 ASSERT_EQ(sub2.getNumPublishers(), 1U);
00239
00240 std_srvs::Empty srv;
00241
00242 ASSERT_TRUE(ros::service::call("switch_publisher_type", srv));
00243
00244 ros::WallDuration(1.0).sleep();
00245 ros::spinOnce();
00246
00247 ASSERT_EQ(sub.getNumPublishers(), 0U);
00248 ASSERT_EQ(sub2.getNumPublishers(), 0U);
00249 }
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 int main(int argc, char** argv)
00277 {
00278 testing::InitGoogleTest(&argc, argv);
00279 ros::init(argc, argv, "subscribe_star");
00280 ros::NodeHandle nh;
00281 return RUN_ALL_TESTS();
00282 }