Skip to content

Instantly share code, notes, and snippets.

@wolegeyun
Forked from dkargin/test_client.cpp
Created May 18, 2022 05:22
Show Gist options
  • Select an option

  • Save wolegeyun/d1237c66c9906ee998112091a67a3a44 to your computer and use it in GitHub Desktop.

Select an option

Save wolegeyun/d1237c66c9906ee998112091a67a3a44 to your computer and use it in GitHub Desktop.

Revisions

  1. @dkargin dkargin created this gist Feb 6, 2016.
    45 changes: 45 additions & 0 deletions test_client.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #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;
    }
    38 changes: 38 additions & 0 deletions test_server.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    #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;
    }