service_adv_unadv.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  * Advertise a service
00034  */
00035 
00036 #include <gtest/gtest.h>
00037 
00038 #include "ros/ros.h"
00039 #include <test_roscpp/TestStringString.h>
00040 
00041 
00042 static int g_argc;
00043 static char** g_argv;
00044 
00045 class ServiceAdvertiser : public testing::Test
00046 {
00047   public:
00048     ros::NodeHandle nh_;
00049     ros::ServiceServer srv_;
00050 
00051     bool advertised_;
00052     bool failure_;
00053 
00054     bool srvCallback(test_roscpp::TestStringString::Request  &req,
00055                      test_roscpp::TestStringString::Response &res)
00056     {
00057       ROS_INFO("in callback");
00058       if(!advertised_)
00059       {
00060         ROS_INFO("but not advertised!");
00061         failure_ = true;
00062       }
00063       return true;
00064     }
00065   protected:
00066     ServiceAdvertiser() {}
00067     void SetUp()
00068     {
00069       failure_ = false;
00070       advertised_ = false;
00071 
00072       ASSERT_TRUE(g_argc == 1);
00073     }
00074 
00075     bool adv()
00076     {
00077       ROS_INFO("advertising");
00078       srv_ = nh_.advertiseService("service_adv", &ServiceAdvertiser::srvCallback, this);
00079       ROS_INFO("advertised");
00080       return srv_;
00081     }
00082     bool unadv()
00083     {
00084       ROS_INFO("unadvertising");
00085       srv_.shutdown();
00086       ROS_INFO("unadvertised");
00087       return true;
00088     }
00089 };
00090 
00091 TEST_F(ServiceAdvertiser, advUnadv)
00092 {
00093   advertised_ = true;
00094   ASSERT_TRUE(adv());
00095 
00096   for(int i=0;i<100;i++)
00097   {
00098     if(advertised_)
00099     {
00100       ASSERT_TRUE(unadv());
00101       advertised_ = false;
00102     }
00103     else
00104     {
00105       advertised_ = true;
00106       ASSERT_TRUE(adv());
00107     }
00108 
00109     ros::WallDuration(0.01).sleep();
00110   }
00111 
00112   if(failure_)
00113     FAIL();
00114   else
00115     SUCCEED();
00116 }
00117 
00118 int
00119 main(int argc, char** argv)
00120 {
00121   ros::init(argc, argv, "service_adv_unadv");
00122   testing::InitGoogleTest(&argc, argv);
00123   g_argc = argc;
00124   g_argv = argv;
00125   return RUN_ALL_TESTS();
00126 }
00127 


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