subscribe_empty.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 number of messages based on the first command line argument
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/TestEmpty.h>
00045 
00046 int g_argc;
00047 char** g_argv;
00048 
00049 class Subscriptions : public testing::Test
00050 {
00051   public:
00052     test_roscpp::TestEmpty msg;
00053     bool success;
00054     bool failure;
00055     int msg_count;
00056     int msg_i;
00057     ros::Duration dt;
00058 
00059     void messageCallback(const test_roscpp::TestEmptyConstPtr& msg)
00060     {
00061       if(failure || success)
00062         return;
00063 
00064       msg_i++;
00065       printf("received message %d\n", msg_i);
00066       if(msg_i == (msg_count-1))
00067       {
00068         success = true;
00069         puts("success");
00070       }
00071     }
00072 
00073   protected:
00074     Subscriptions() {}
00075     void SetUp()
00076     {
00077       success = false;
00078       failure = false;
00079 
00080       msg_i = -1;
00081       ASSERT_TRUE(g_argc == 3);
00082       msg_count = atoi(g_argv[1]);
00083       dt.fromSec(atof(g_argv[2]));
00084     }
00085     void TearDown()
00086     {
00087     }
00088 };
00089 
00090 TEST_F(Subscriptions, emptyMsg)
00091 {
00092   ros::NodeHandle nh;
00093   ros::Subscriber sub = nh.subscribe("roscpp/pubsub_test", msg_count, &Subscriptions::messageCallback, (Subscriptions*)this);
00094   ros::Time t1(ros::Time::now()+dt);
00095 
00096   while(ros::Time::now() < t1 && !success)
00097   {
00098     ros::WallDuration(0.01).sleep();
00099     ros::spinOnce();
00100   }
00101 
00102   if(success)
00103     SUCCEED();
00104   else
00105     FAIL();
00106 }
00107 
00108 int main(int argc, char** argv)
00109 {
00110   ros::init(argc, argv, "subscribe_empty");
00111   ros::NodeHandle nh;
00112 
00113   testing::InitGoogleTest(&argc, argv);
00114   g_argc = argc;
00115   g_argv = argv;
00116   return RUN_ALL_TESTS();
00117 }


test_roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Fri Aug 28 2015 12:33:59