service_call.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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 Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * 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 THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /* Author: Brian Gerkey */
31 
32 /*
33  * Call a service
34  */
35 
36 #include <string>
37 
38 #include <gtest/gtest.h>
39 
40 #include "ros/ros.h"
41 #include "ros/time.h"
42 #include "ros/service.h"
43 #include "ros/connection.h"
44 #include "ros/service_client.h"
45 #include <test_roscpp/TestStringString.h>
46 #include <test_roscpp/BadTestStringString.h>
47 
48 TEST(SrvCall, callSrv)
49 {
50  test_roscpp::TestStringString::Request req;
51  test_roscpp::TestStringString::Response res;
52 
53  req.str = std::string("case_FLIP");
54 
55  ASSERT_TRUE(ros::service::waitForService("service_adv"));
56  ASSERT_TRUE(ros::service::call("service_adv", req, res));
57 
58  ASSERT_STREQ(res.str.c_str(), "CASE_flip");
59 }
60 
61 TEST(SrvCall, callSrvUnicode)
62 {
63  test_roscpp::TestStringString::Request req;
64  test_roscpp::TestStringString::Response res;
65 
66  req.str = std::string("ロボット");
67 
68  ASSERT_TRUE(ros::service::waitForService("service_adv"));
69  ASSERT_TRUE(ros::service::call("service_adv", req, res));
70 
71  ASSERT_STREQ(res.str.c_str(), "ロボット");
72 }
73 
74 TEST(SrvCall, callSrvMultipleTimes)
75 {
76  test_roscpp::TestStringString::Request req;
77  test_roscpp::TestStringString::Response res;
78 
79  req.str = std::string("case_FLIP");
80 
81  ASSERT_TRUE(ros::service::waitForService("service_adv"));
82 
84 
85  for (int i = 0; i < 100; ++i)
86  {
87  ASSERT_TRUE(ros::service::call("service_adv", req, res));
88  }
89 
90  ros::Time end = ros::Time::now();
91  ros::Duration d = end - start;
92  printf("100 calls took %f secs\n", d.toSec());
93 
94  ASSERT_STREQ(res.str.c_str(), "CASE_flip");
95 }
96 
97 TEST(SrvCall, callSrvWithWrongType)
98 {
99  test_roscpp::BadTestStringString::Request req;
100  test_roscpp::BadTestStringString::Response res;
101 
102  ASSERT_TRUE(ros::service::waitForService("service_adv"));
103 
104  for ( int i = 0; i < 4; ++i )
105  {
106  bool call_result = ros::service::call("service_adv", req, res);
107  ASSERT_FALSE(call_result);
108  }
109 }
110 
111 TEST(SrvCall, callSrvHandle)
112 {
113  test_roscpp::TestStringString::Request req;
114  test_roscpp::TestStringString::Response res;
115 
116  req.str = std::string("case_FLIP");
117 
118  std::map<std::string, std::string> header;
119  header["test1"] = "testing 1";
120  header["test2"] = "testing 2";
121  ros::NodeHandle nh;
122  ros::ServiceClient handle = nh.serviceClient<test_roscpp::TestStringString>("service_adv", false, header);
123  ASSERT_TRUE(handle.waitForExistence());
124 
126 
127  for (int i = 0; i < 100; ++i)
128  {
129  ASSERT_TRUE(handle.call(req, res));
130  }
131 
132  ros::Time end = ros::Time::now();
133  ros::Duration d = end - start;
134  printf("100 calls took %f secs\n", d.toSec());
135 
136  ASSERT_STREQ(res.str.c_str(), "CASE_flip");
137 }
138 
139 TEST(SrvCall, callSrvPersistentHandle)
140 {
141  test_roscpp::TestStringString::Request req;
142  test_roscpp::TestStringString::Response res;
143 
144  req.str = std::string("case_FLIP");
145 
146  ASSERT_TRUE(ros::service::waitForService("service_adv"));
147 
148  std::map<std::string, std::string> header;
149  header["test1"] = "testing 1";
150  header["test2"] = "testing 2";
151  ros::NodeHandle nh;
152  ros::ServiceClient handle = nh.serviceClient<test_roscpp::TestStringString>("service_adv", true, header);
153 
155 
156  for (int i = 0; i < 10000; ++i)
157  {
158  ASSERT_TRUE(handle.call(req, res));
159  }
160 
161  ros::Time end = ros::Time::now();
162  ros::Duration d = end - start;
163  printf("10000 calls took %f secs\n", d.toSec());
164 
165  ASSERT_STREQ(res.str.c_str(), "CASE_flip");
166 }
167 
168 TEST(SrvCall, callSrvLongRunning)
169 {
170  test_roscpp::TestStringString::Request req;
171  test_roscpp::TestStringString::Response res;
172 
173  req.str = std::string("case_FLIP");
174 
175  ASSERT_TRUE(ros::service::waitForService("service_adv_long"));
176  ASSERT_TRUE(ros::service::call("service_adv_long", req, res));
177 
178  ASSERT_STREQ(res.str.c_str(), "CASE_flip");
179 }
180 
181 TEST(SrvCall, callSrvWhichUnadvertisesInCallback)
182 {
183  test_roscpp::TestStringString::Request req;
184  test_roscpp::TestStringString::Response res;
185 
186  req.str = std::string("case_FLIP");
187 
188  ASSERT_TRUE(ros::service::waitForService("service_adv_unadv_in_callback"));
189  ASSERT_FALSE(ros::service::call("service_adv_unadv_in_callback", req, res));
190 }
191 
192 TEST(SrvCall, handleValid)
193 {
194  test_roscpp::TestStringString::Request req;
195  test_roscpp::TestStringString::Response res;
196 
197  req.str = std::string("case_FLIP");
198 
199  ASSERT_TRUE(ros::service::waitForService("service_adv"));
200 
201  std::map<std::string, std::string> header;
202  header["test1"] = "testing 1";
203  header["test2"] = "testing 2";
204  ros::NodeHandle nh;
205  ros::ServiceClient handle = nh.serviceClient<test_roscpp::TestStringString>("service_adv", true, header);
206  ASSERT_TRUE(handle.call(req, res));
207  ASSERT_TRUE(handle.isValid());
208  handle.shutdown();
209  ASSERT_FALSE(handle.isValid());
210 
211  ASSERT_STREQ(res.str.c_str(), "CASE_flip");
212 }
213 
214 TEST(SrvCall, waitForServiceTimeout)
215 {
216  ros::NodeHandle nh;
217  ASSERT_FALSE(ros::service::waitForService("iojergoiwjoiewg", 1000));
218  ASSERT_FALSE(ros::service::waitForService("iojergoiwjoiewg", ros::Duration(1)));
219 
220  ros::ServiceClient handle = nh.serviceClient<test_roscpp::TestStringString>("migowiowejowieuhwejg", false);
221  ASSERT_FALSE(handle.waitForExistence(ros::Duration(1)));
222 }
223 
224 int
225 main(int argc, char** argv)
226 {
227  testing::InitGoogleTest(&argc, argv);
228 
229  ros::init(argc, argv, "service_call");
230  ros::NodeHandle nh;
231 
232  int ret = RUN_ALL_TESTS();
233 
234 
235 
236  return ret;
237 }
238 
239 
d
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
ROSCPP_DECL void start()
bool call(const std::string &service_name, MReq &req, MRes &res)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
bool call(MReq &req, MRes &res)
std_msgs::Header * header(M &m)
TEST(SrvCall, callSrv)
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
bool isValid() const
static Time now()
int main(int argc, char **argv)
ROSCPP_DECL bool waitForService(const std::string &service_name, int32_t timeout)


test_roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:46