Go to the documentation of this file.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
00035
00036 #include <string>
00037
00038 #include <gtest/gtest.h>
00039
00040 #include <time.h>
00041 #include <stdlib.h>
00042
00043 #include "ros/ros.h"
00044 #include <test_roscpp/TestArray.h>
00045
00046 int g_argc;
00047 char** g_argv;
00048
00049 class Subscriptions : public testing::Test
00050 {
00051 public:
00052
00053 ros::NodeHandle n;
00054 bool success;
00055 bool failure;
00056 std::string transport;
00057 bool reliable;
00058 int msgs_expected;
00059 int msgs_received;
00060 ros::Duration dt;
00061
00062 void MsgCallback(const test_roscpp::TestArray::ConstPtr& msg)
00063 {
00064 if (failure || success)
00065 return;
00066
00067 printf("received message %d\n", msg->counter);
00068 msgs_received++;
00069 if (reliable)
00070 {
00071 if (msgs_received != msg->counter)
00072 {
00073 failure = true;
00074 puts("failed");
00075 }
00076 if (msgs_received == (msgs_expected - 1))
00077 {
00078 success = true;
00079 puts("success");
00080 }
00081 }
00082 else
00083 {
00084 if (msgs_received > msg->counter)
00085 {
00086 failure = true;
00087 printf("failed: %d > %d\n", msgs_received, msg->counter);
00088 }
00089 if (msgs_received > (0.01 * msgs_expected))
00090 {
00091 success = true;
00092 puts("success");
00093 }
00094 }
00095
00096 }
00097
00098 protected:
00099 Subscriptions() {}
00100 void SetUp()
00101 {
00102 success = false;
00103 failure = false;
00104
00105 msgs_received = -1;
00106 ASSERT_TRUE(g_argc == 4);
00107 transport = g_argv[1];
00108 msgs_expected = atoi(g_argv[2]);
00109 dt.fromSec(atof(g_argv[3]));
00110 if (transport == "tcp")
00111 reliable = true;
00112 else if (transport == "udp")
00113 reliable = false;
00114 else
00115 {
00116 ROS_ERROR("Unknown transport: %s", transport.c_str());
00117 FAIL();
00118 }
00119 }
00120 void TearDown()
00121 {
00122 }
00123 };
00124
00125 TEST_F(Subscriptions, pubSubNFast)
00126 {
00127 ros::TransportHints hints;
00128 if (reliable)
00129 hints.reliable();
00130 else
00131 hints.unreliable();
00132
00133 ros::Subscriber sub = n.subscribe("roscpp/pubsub_test", msgs_expected, &Subscriptions::MsgCallback, (Subscriptions *)this, hints);
00134
00135 ASSERT_TRUE(sub);
00136
00137 ros::Time t1(ros::Time::now() + dt);
00138
00139 while(ros::Time::now() < t1 && !success)
00140 {
00141 ros::spinOnce();
00142 ros::WallDuration(0.01).sleep();
00143 }
00144
00145 printf("msgs_received == %d\n", msgs_received);
00146 if(success)
00147 SUCCEED();
00148 else
00149 FAIL();
00150 }
00151
00152 int
00153 main(int argc, char** argv)
00154 {
00155 testing::InitGoogleTest(&argc, argv);
00156 ros::init(argc, argv, "subscriber");
00157 g_argc = argc;
00158 g_argv = argv;
00159 return RUN_ALL_TESTS();
00160 }