sub_pub.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 /* Author: Brian Gerkey */
00031 
00032 /*
00033  * Subscribe to a topic, expecting to get a single message.
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 <ros/callback_queue.h>
00045 #include <test_roscpp/TestArray.h>
00046 
00047 int g_msg_count;
00048 ros::Duration g_dt;
00049 uint32_t g_options;
00050 
00051 class Subscriptions : public testing::Test
00052 {
00053   public:
00054     bool success;
00055     bool failure;
00056     int msg_i;
00057     ros::Publisher pub_;
00058 
00059     void messageCallback(const test_roscpp::TestArrayConstPtr& msg)
00060     {
00061       if(failure || success)
00062         return;
00063 
00064       //printf("received message %d\n", msg.counter);
00065       if (msg_i == -1)
00066       {
00067         msg_i = msg->counter - 1;
00068       }
00069 
00070       msg_i++;
00071       //ROS_INFO("msg_i=%d, counter=%d", msg_i, msg->counter);
00072       if(msg_i != msg->counter)
00073       {
00074         failure = true;
00075         ROS_INFO("failed");
00076       }
00077       else if(msg_i == (g_msg_count-1))
00078       {
00079         success = true;
00080         ROS_INFO("success");
00081       }
00082       else
00083       {
00084         pub_.publish(msg);
00085       }
00086     }
00087 
00088     void subscriberCallback(const ros::SingleSubscriberPublisher&)
00089     {
00090       test_roscpp::TestArray msg;
00091       msg.counter = 0;
00092       pub_.publish(msg);
00093     }
00094 
00095   protected:
00096     void SetUp()
00097     {
00098       success = false;
00099       failure = false;
00100 
00101       msg_i = -1;
00102     }
00103     void TearDown()
00104     {
00105 
00106     }
00107 };
00108 
00109 TEST_F(Subscriptions, subPub)
00110 {
00111   ros::NodeHandle nh;
00112   ros::Subscriber sub = nh.subscribe("roscpp/pubsub_test", 0, &Subscriptions::messageCallback, (Subscriptions*)this);
00113   ASSERT_TRUE(sub);
00114   pub_ = nh.advertise<test_roscpp::TestArray>("roscpp/subpub_test", 0, boost::bind(&Subscriptions::subscriberCallback, this, _1));
00115   ASSERT_TRUE(pub_);
00116   ros::Time t1(ros::Time::now()+g_dt);
00117 
00118   while(ros::Time::now() < t1 && !success && !failure)
00119   {
00120     ros::getGlobalCallbackQueue()->callAvailable(ros::WallDuration(0.1));
00121   }
00122 
00123   if(success)
00124     SUCCEED();
00125   else
00126     FAIL();
00127 }
00128 
00129 #define USAGE "USAGE: sub_pub <count> <time>"
00130 
00131 int
00132 main(int argc, char** argv)
00133 {
00134   testing::InitGoogleTest(&argc, argv);
00135   ros::init(argc, argv, "sub_pub");
00136 
00137   if(argc != 3)
00138   {
00139     puts(USAGE);
00140     return -1;
00141   }
00142   g_msg_count = atoi(argv[1]);
00143   g_dt.fromSec(atof(argv[2]));
00144 
00145   ros::NodeHandle nh;
00146 
00147   return RUN_ALL_TESTS();
00148 }


test_roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Tue Mar 7 2017 03:45:23