topic_service_test.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2018, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL SOUTHWEST RESEARCH INSTITUTE BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 #include <gtest/gtest.h>
30 
31 #include <ros/ros.h>
32 
33 #include <swri_roscpp/TestTopicServiceRequest.h>
34 #include <swri_roscpp/TestTopicServiceResponse.h>
35 
38 
39 namespace swri_roscpp
40 {
41  /*
42  * This class would normally be auto-generated by the add_topic_service_files
43  * CMake macro, but we can't use that in swri_roscpp's own tests because the
44  * .cmake file will not have been generated and installed at the time the
45  * tests are run.
46  */
48  {
49  public:
50  typedef TestTopicServiceResponse Response;
51  typedef TestTopicServiceRequest Request;
52 
55  };
56 
57  static const std::string topic_name = "/test_topic_service";
58 
59  static const size_t value_count = 5;
60  static const int test_values[] = {5, 10, 100, 10000, 50000};
61 
62  /*
63  * Class used by the server to respond to service requests
64  */
66  {
67  public:
69  call_count_(0),
70  error_(false),
71  is_running_(true)
72  {}
73 
76  {
77  ROS_INFO("TopicServiceHandler::handleTopicServiceRequest");
78  resp.response_value = req.request_value;
79 
80  if (call_count_ >= value_count || (test_values[call_count_] != req.request_value))
81  {
82  error_ = true;
83  return false;
84  }
85 
86  if (req.request_value == test_values[value_count-1])
87  {
88  is_running_ = false;
89  }
90 
91  call_count_++;
92 
93  return is_running_;
94  }
95 
97  bool error_;
99  };
100 }
101 
102 TEST(TopicServiceClientTests, testTopicServiceClient)
103 {
104  ros::NodeHandle nh("~");
105 
107  client.initialize(nh, swri_roscpp::topic_name, "test_client");
108 
109  ros::Duration one_second(1.0);
110  int checks = 0;
111  // Wait up to 20s for the server to exist (it should be much faster than that)
112  while (!client.exists() && checks < 20)
113  {
114  ROS_INFO("Waiting for server to exist...");
115  one_second.sleep();
116  checks++;
117  }
118  ASSERT_TRUE(client.exists());
119 
121 
122  // Iterate through our tests values and test submitting all of them
123  for (size_t i = 0; i < swri_roscpp::value_count; i++)
124  {
125  srv.request.request_value = swri_roscpp::test_values[i];
126  bool result = client.call(srv);
127 
128  if (i + 1 < swri_roscpp::value_count)
129  {
130  ASSERT_TRUE(result);
131  }
132  else
133  {
134  // The very last value should cause the server to return false
135  ASSERT_FALSE(result);
136  }
137  ASSERT_EQ(swri_roscpp::test_values[i], srv.response.response_value);
138  }
139 }
140 
141 TEST(TopicServiceServerTests, testTopicServiceServer)
142 {
143  ros::NodeHandle nh("~");
145 
147 
148  ROS_INFO("Initializing server.");
149 
150  server.initialize(
151  nh,
154  &handler);
155 
156  ros::Rate rate(50);
158  // Wait up to 20s for the client to complete; it should be much faster than that
159  while (handler.is_running_ && (ros::Time::now() - start) < ros::Duration(20))
160  {
161  // If the server encounters any errors, it will set error_ to true
162  ASSERT_FALSE(handler.error_);
163  ros::spinOnce();
164  rate.sleep();
165  }
166 
167  ROS_INFO("Server is exiting.");
168 }
169 
170 int main(int argc, char** argv)
171 {
172  ros::init(argc, argv, "topic_service_test", ros::init_options::AnonymousName);
173 
174  int retval = 0;
175  ros::start();
176  testing::InitGoogleTest(&argc, argv);
177  retval = RUN_ALL_TESTS();
178  ros::shutdown();
179 
180  return retval;
181 }
ros::init_options::AnonymousName
AnonymousName
topic_service_server.h
swri_roscpp::TopicServiceHandler::handleTopicServiceRequest
bool handleTopicServiceRequest(const swri_roscpp::TestTopicService::Request &req, swri_roscpp::TestTopicService::Response &resp)
Definition: topic_service_test.cpp:74
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
swri::TopicServiceClient::call
bool call(MReq &req)
Definition: topic_service_client.h:172
ros.h
main
int main(int argc, char **argv)
Definition: topic_service_test.cpp:170
ros::spinOnce
ROSCPP_DECL void spinOnce()
swri_roscpp::TestTopicService::request
Request request
Definition: topic_service_test.cpp:53
swri::TopicServiceServer::initialize
void initialize(ros::NodeHandle &nh, const std::string &service, bool(T::*srv_func)(const MReq &, MRes &), T *obj)
Definition: topic_service_server.h:111
ros::shutdown
ROSCPP_DECL void shutdown()
topic_service_client.h
swri::TopicServiceClient::exists
bool exists()
Definition: topic_service_client.h:167
swri_roscpp::TopicServiceHandler::TopicServiceHandler
TopicServiceHandler()
Definition: topic_service_test.cpp:68
swri_roscpp::TestTopicService
Definition: topic_service_test.cpp:47
swri_roscpp::TopicServiceHandler::is_running_
bool is_running_
Definition: topic_service_test.cpp:98
swri_roscpp::TestTopicService::Response
TestTopicServiceResponse Response
Definition: topic_service_test.cpp:50
swri_roscpp::test_values
static const int test_values[]
Definition: topic_service_test.cpp:60
swri_roscpp::value_count
static const size_t value_count
Definition: topic_service_test.cpp:59
ros::Rate::sleep
bool sleep()
start
ROSCPP_DECL void start()
swri::TopicServiceClient
Definition: topic_service_client.h:146
swri::TopicServiceServer
Definition: topic_service_server.h:87
TEST
TEST(TopicServiceClientTests, testTopicServiceClient)
Definition: topic_service_test.cpp:102
swri_roscpp::TestTopicService::response
Response response
Definition: topic_service_test.cpp:54
ros::Time
swri_roscpp::TestTopicService::Request
TestTopicServiceRequest Request
Definition: topic_service_test.cpp:51
swri_roscpp::topic_name
static const std::string topic_name
Definition: topic_service_test.cpp:57
swri_roscpp::TopicServiceHandler::call_count_
int call_count_
Definition: topic_service_test.cpp:96
swri::TopicServiceClient::initialize
void initialize(ros::NodeHandle &nh, const std::string &service, const std::string &client_name="")
Definition: topic_service_client.h:152
ros::Rate
ros::Duration::sleep
bool sleep() const
swri_roscpp::TopicServiceHandler::error_
bool error_
Definition: topic_service_test.cpp:97
ROS_INFO
#define ROS_INFO(...)
swri_roscpp
Definition: topic_service_test.cpp:39
swri_roscpp::TopicServiceHandler
Definition: topic_service_test.cpp:65
ros::Duration
ros::NodeHandle
ros::Time::now
static Time now()


swri_roscpp
Author(s): P. J. Reed
autogenerated on Fri Aug 2 2024 08:39:15