Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <gtest/gtest.h>
00030
00031 #include <ros/ros.h>
00032
00033 #include <swri_roscpp/TestTopicServiceRequest.h>
00034 #include <swri_roscpp/TestTopicServiceResponse.h>
00035
00036 #include <swri_roscpp/topic_service_client.h>
00037 #include <swri_roscpp/topic_service_server.h>
00038
00039 namespace swri_roscpp
00040 {
00041
00042
00043
00044
00045
00046
00047 class TestTopicService
00048 {
00049 public:
00050 typedef TestTopicServiceResponse Response;
00051 typedef TestTopicServiceRequest Request;
00052
00053 Request request;
00054 Response response;
00055 };
00056
00057 static const std::string topic_name = "/test_topic_service";
00058
00059 static const size_t value_count = 5;
00060 static const int test_values[] = {5, 10, 100, 10000, 50000};
00061
00062
00063
00064
00065 class TopicServiceHandler
00066 {
00067 public:
00068 TopicServiceHandler() :
00069 call_count_(0),
00070 error_(false),
00071 is_running_(true)
00072 {}
00073
00074 bool handleTopicServiceRequest(const swri_roscpp::TestTopicService::Request& req,
00075 swri_roscpp::TestTopicService::Response& resp)
00076 {
00077 ROS_INFO("TopicServiceHandler::handleTopicServiceRequest");
00078 resp.response_value = req.request_value;
00079
00080 if (call_count_ >= value_count || (test_values[call_count_] != req.request_value))
00081 {
00082 error_ = true;
00083 return false;
00084 }
00085
00086 if (req.request_value == test_values[value_count-1])
00087 {
00088 is_running_ = false;
00089 }
00090
00091 call_count_++;
00092
00093 return is_running_;
00094 }
00095
00096 int call_count_;
00097 bool error_;
00098 bool is_running_;
00099 };
00100 }
00101
00102 TEST(TopicServiceClientTests, testTopicServiceClient)
00103 {
00104 ros::NodeHandle nh("~");
00105
00106 swri::TopicServiceClient<swri_roscpp::TestTopicService> client;
00107 client.initialize(nh, swri_roscpp::topic_name, "test_client");
00108
00109 ros::Duration one_second(1.0);
00110 int checks = 0;
00111
00112 while (!client.exists() && checks < 20)
00113 {
00114 ROS_INFO("Waiting for server to exist...");
00115 one_second.sleep();
00116 checks++;
00117 }
00118 ASSERT_TRUE(client.exists());
00119
00120 swri_roscpp::TestTopicService srv;
00121
00122
00123 for (size_t i = 0; i < swri_roscpp::value_count; i++)
00124 {
00125 srv.request.request_value = swri_roscpp::test_values[i];
00126 bool result = client.call(srv);
00127
00128 if (i + 1 < swri_roscpp::value_count)
00129 {
00130 ASSERT_TRUE(result);
00131 }
00132 else
00133 {
00134
00135 ASSERT_FALSE(result);
00136 }
00137 ASSERT_EQ(swri_roscpp::test_values[i], srv.response.response_value);
00138 }
00139 }
00140
00141 TEST(TopicServiceServerTests, testTopicServiceServer)
00142 {
00143 ros::NodeHandle nh("~");
00144 swri_roscpp::TopicServiceHandler handler;
00145
00146 swri::TopicServiceServer server;
00147
00148 ROS_INFO("Initializing server.");
00149
00150 server.initialize(
00151 nh,
00152 swri_roscpp::topic_name,
00153 &swri_roscpp::TopicServiceHandler::handleTopicServiceRequest,
00154 &handler);
00155
00156 ros::Rate rate(50);
00157 ros::Time start = ros::Time::now();
00158
00159 while (handler.is_running_ && (ros::Time::now() - start) < ros::Duration(20))
00160 {
00161
00162 ASSERT_FALSE(handler.error_);
00163 ros::spinOnce();
00164 rate.sleep();
00165 }
00166
00167 ROS_INFO("Server is exiting.");
00168 }
00169
00170 int main(int argc, char** argv)
00171 {
00172 ros::init(argc, argv, "topic_service_test", ros::init_options::AnonymousName);
00173
00174 int retval = 0;
00175 ros::start();
00176 testing::InitGoogleTest(&argc, argv);
00177 retval = RUN_ALL_TESTS();
00178 ros::shutdown();
00179
00180 return retval;
00181 }