Skip to content

Instantly share code, notes, and snippets.

@dkargin
Created February 6, 2016 11:42
Show Gist options
  • Save dkargin/10affd3adcc54b13fa71 to your computer and use it in GitHub Desktop.
Save dkargin/10affd3adcc54b13fa71 to your computer and use it in GitHub Desktop.
roscpp service timeout test
#include <ros/ros.h>
#include <std_srvs/Trigger.h>
typedef std_srvs::Trigger RPC;
void test_call(ros::ServiceClient & client)
{
RPC rpc;
ROS_INFO("Sending request");
if(client.call(rpc))
{
ROS_INFO("RPC is complete, msg=%s, status=%d", rpc.response.message.c_str(), rpc.response.success);
}
else
{
ROS_ERROR("RPC has failed");
}
}
int main(int argc, char * argv[])
{
ros::init(argc, argv, "test_client");
ros::NodeHandle nh;
ros::ServiceClient client = nh.serviceClient<RPC>("crash_srv");
ROS_INFO("Waiting for service");
client.waitForExistence(ros::Duration(1.0));
client.setTimeout(5);
ROS_INFO("Sending first request");
test_call(client);
ROS_INFO("Sending second request");
test_call(client);
ROS_INFO("Test is complete. Can exit now");
return 0;
}
#include <ros/ros.h>
#include <std_srvs/Trigger.h>
int requests = 0;
typedef std_srvs::Trigger RPC;
bool callback(RPC::Request & req, RPC::Response & rep)
{
ROS_INFO("Got request. Crashing here");
sleep(2);
/// Break on second request
if(requests == 1)
{
int * data = NULL;
/// make SIGSEGV here
data[0] = 10;
exit(0);
}
rep.message = "done";
rep.success = ++requests;
return true;
}
int main(int argc, char * argv[])
{
ros::init(argc, argv, "test_server");
ros::NodeHandle nh;
ros::ServiceServer srv = nh.advertiseService("crash_srv", &callback);
ROS_INFO("Initialization is complete. Running service");
ros::spin();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment