test_empty_service.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2008, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 ## Integration test for empty services to test serializers
35 ## and transport
36 
37 PKG = 'test_rospy'
38 NAME = 'empty_service'
39 
40 import sys, time
41 import unittest
42 
43 import rospy, rostest
44 from test_rospy.srv import *
45 
46 EMPTY_SERVICE = 'empty_service'
47 EMPTY_RETURN_SERVICE = 'empty_return_service'
48 EMPTY_REQ_SERVICE = 'empty_req_service'
49 EMPTY_RESP_SERVICE = 'empty_resp_service'
50 
51 FAKE_SECRET = 123456
52 
53 WAIT_TIMEOUT = 10.0 #s
54 
55 def handle_empty(req):
56  print("Returning empty")
57  return EmptySrvResponse()
58 
60  "print returning empty list"
61  return []
62 
63 ## handle empty request
65  print("Returning fake_secret")
66  return EmptyReqSrvResponse(FAKE_SECRET)
67 ## handle empty response
69  if req.fake_secret == FAKE_SECRET:
70  print("Request validated, returning empty")
71  return EmptyRespSrvResponse()
72  else:
73  print("Request did not validate, returning None")
74 
76  rospy.init_node(NAME)
77  s1 = rospy.Service(EMPTY_SERVICE, EmptySrv, handle_empty)
78  s2 = rospy.Service(EMPTY_REQ_SERVICE, EmptyReqSrv, handle_empty_req)
79  s3 = rospy.Service(EMPTY_RESP_SERVICE, EmptyRespSrv, handle_empty_resp)
80  s4 = rospy.Service(EMPTY_RETURN_SERVICE, EmptySrv, handle_return_empty)
81  rospy.spin()
82 
83 class TestEmptyServiceClient(unittest.TestCase):
84 
85  def _test(self, name, srv, req):
86  rospy.wait_for_service(name, WAIT_TIMEOUT)
87  s = rospy.ServiceProxy(name, srv)
88  resp = s.call(req)
89  self.assert_(resp is not None)
90  return resp
91 
92  # test that __call__ and s.call() work with no-args on an empty request
93  def test_call_empty(self):
94  rospy.wait_for_service(EMPTY_REQ_SERVICE, WAIT_TIMEOUT)
95  s = rospy.ServiceProxy(EMPTY_REQ_SERVICE, EmptyReqSrv)
96  resp = s()
97  self.assertEquals(FAKE_SECRET, resp.fake_secret,
98  "fake_secret fields is not set as expected")
99  resp = s.call()
100  self.assertEquals(FAKE_SECRET, resp.fake_secret,
101  "fake_secret fields is not set as expected")
102 
103  def test_empty(self):
104  self._test(EMPTY_SERVICE, EmptySrv, EmptySrvRequest())
105  # test that an empty return service handler can return an empty list
106  def test_return_empty(self):
107  self._test(EMPTY_RETURN_SERVICE, EmptySrv, EmptySrvRequest())
108  def test_empty_req(self):
109  resp = self._test(EMPTY_REQ_SERVICE, EmptyReqSrv,
110  EmptyReqSrvRequest())
111  self.assertEquals(FAKE_SECRET, resp.fake_secret,
112  "fake_secret fields is not set as expected")
113  def test_empty_resp(self):
114  self._test(EMPTY_RESP_SERVICE, EmptyRespSrv,
115  EmptyRespSrvRequest(FAKE_SECRET))
116 
117 if __name__ == '__main__':
118  if '--service' in sys.argv:
119  empty_service()
120  else:
121  rostest.run(PKG, 'rospy_empty_service', TestEmptyServiceClient, sys.argv)
def handle_empty_resp(req)
handle empty response
def handle_empty_req(req)
handle empty request


test_rospy
Author(s): Ken Conley
autogenerated on Sun Feb 3 2019 03:30:22