Skip to content

Instantly share code, notes, and snippets.

@k-okada
Created July 28, 2021 11:15
Show Gist options
  • Save k-okada/d930a741d1f0eb0fddb4e0c615c24b44 to your computer and use it in GitHub Desktop.
Save k-okada/d930a741d1f0eb0fddb4e0c615c24b44 to your computer and use it in GitHub Desktop.
<launch>
<node pkg="test_fast_call" type="add_two_ints_server"
name="add_two_ints_server" />
<test pkg="test_fast_call" type="add_two_ints_client"
test-name="add_two_ints_client"
time-limit="60000" />
</launch>
/*
* Copyright (C) 2020, Kei Okada
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the names of copyright holder Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "ros/ros.h"
#include "roscpp_tutorials/TwoInts.h"
#include <cstdlib>
#include <gtest/gtest.h>
using namespace std;
typedef enum {same_request, different_request} request_type;
string request_name[] = {"same_request", "different_request"};
class AddTwoIntsClient : public testing::TestWithParam<tuple<bool, request_type, int>>
{
protected:
request_type type;
int rate;
int loop;
bool persistent;
public:
int run() {
ros::NodeHandle n;
ros::ServiceClient client = n.serviceClient<roscpp_tutorials::TwoInts>("add_two_ints", persistent);
std::cerr << "Started service call with persistent " << persistent << std::endl;;
int i = 0;
ros::Rate loop_rate(this->rate);
while ( ros::ok() && i < this->loop ) {
roscpp_tutorials::TwoInts srv;
switch ( type ) {
case same_request:
srv.request.a = 0;
srv.request.b = 1;
break;
case different_request:
srv.request.a = i;
srv.request.b = i+1;
break;
default:
std::cerr << "Wrong request type : " << type << std::endl;
return -1;
}
if (client.call(srv))
{
//ROS_INFO("Sum: %ld", (long int)srv.response.sum);
int answer;
switch ( type ) {
case same_request:
answer = 1;
break;
case different_request:
answer = i + i+1;
break;
}
if ( srv.response.sum != answer ) {
ROS_ERROR("Sum: %ld + %ld != %ld",
(long int)srv.request.a,
(long int)srv.request.b,
(long int)srv.response.sum);
}
}
else
{
std::cerr << "Failed to call service add_two_ints : " << i << std::endl;
return -1;
}
ros::spinOnce();
loop_rate.sleep();
i++;
if ( (i % 5000) == 0 ) {
std::cerr << "Call services for " << i << "/" << this->loop << " times" << std::endl;
}
}
std::cerr << "Successfully call services for " << i << " times" << std::endl;
return 0;
}
};
TEST_P(AddTwoIntsClient, ServiceCallTest) {
persistent = get<0>(GetParam());
type = get<1>(GetParam());
rate = get<2>(GetParam());
loop = 10000;
std::cerr << "Testing on persistent = " << (persistent?"true":"false") << ", with " << request_name[type] << ", " << rate << std::endl;
for(int i = 0; i < 20; i++ ) {
int r = run();
if ( r < 0 ) {
FAIL() << "Failed to run service call in " << i << "th time";
} else {
SUCCEED();
}
}
}
INSTANTIATE_TEST_CASE_P
(
ParameteriedTest, AddTwoIntsClient,
testing::Combine
(
testing::Values(false, true),
testing::Values(same_request, different_request),
testing::Values(200, 1000)
));
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
ros::init(argc, argv, "add_two_ints_client");
ros::service::waitForService("add_two_ints");
return RUN_ALL_TESTS();
}
/*
* Copyright (C) 2008, Morgan Quigley and Willow Garage, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "ros/ros.h"
#include "roscpp_tutorials/TwoInts.h"
bool add(roscpp_tutorials::TwoInts::Request &req,
roscpp_tutorials::TwoInts::Response &res )
{
res.sum = req.a + req.b;
ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
ROS_INFO(" sending back response: [%ld]", (long int)res.sum);
return true;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "add_two_ints_server");
ros::NodeHandle n;
// %Tag(SERVICE_SERVER)%
ros::ServiceServer service = n.advertiseService("add_two_ints", add);
// %EndTag(SERVICE_SERVER)%
ros::spin();
return 0;
}
cmake_minimum_required(VERSION 3.0.2)
project(test_fast_call)
find_package(catkin REQUIRED COMPONENTS
roscpp rostest
)
catkin_package()
include_directories(${catkin_INCLUDE_DIRS})
add_executable(add_two_ints_server add_two_ints_server.cpp)
target_link_libraries(add_two_ints_server ${catkin_LIBRARIES})
# add_rostest_gtest(add_two_ints_client add_two_ints.test add_two_ints_client.cpp)
add_executable(add_two_ints_client add_two_ints_client.cpp)
target_link_libraries(add_two_ints_client ${catkin_LIBRARIES} gtest)
<?xml version="1.0"?>
<package format="2">
<name>test_fast_call</name>
<version>0.0.0</version>
<description>The test_fast_call package</description>
<maintainer email="[email protected]">Kei Okada</maintainer>
<license>GPL</license>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<test_depend>rostest</test_depend>
<export></export>
</package>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment