test_rospy_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 import os
35 import sys
36 import struct
37 import unittest
38 import time
39 
40 import rospy
41 import rospy.service
42 
43 class MockServiceClass(object):
44  _request_class = rospy.AnyMsg
45  _response_class = rospy.AnyMsg
46 
47 class TestRospyService(unittest.TestCase):
48 
50  self.assert_(isinstance(rospy.service.ServiceException(), Exception))
51 
53  class MockService(rospy.service._Service):
54  def __init__(self, name, service_class, uri):
55  rospy.service._Service.__init__(self, name, service_class)
56  self.uri = uri
57 
58  from rospy.service import ServiceManager
59  sm = ServiceManager()
60  self.assertEquals({}, sm.map)
61  try:
62  sm.lock.acquire()
63  finally:
64  sm.lock.release()
65  self.assertEquals([], sm.get_services())
66  sm.unregister_all()
67  self.assertEquals([], sm.get_services())
68 
69  # test actual registration
70  mock = MockService('/serv', MockServiceClass, "rosrpc://uri:1")
71  mock2 = MockService('/serv', MockServiceClass, "rosrpc://uri:2")
72 
73  sm.register('/serv', mock)
74  self.assertEquals(mock, sm.get_service('/serv'))
75  self.assertEquals([('/serv', mock.uri)], sm.get_services())
76  try:
77  sm.register('/serv', mock2)
78  self.fail("duplicate reg should fail")
79  except rospy.service.ServiceException: pass
80 
81  sm.unregister_all()
82  self.assertEquals([], sm.get_services())
83 
84  # - register two services
85  sm.register('/serv', mock)
86  sm.unregister('/serv', mock2)
87  self.assertEquals(mock, sm.get_service('/serv'))
88 
89  sm.register('/serv2', mock2)
90  self.assertEquals(mock, sm.get_service('/serv'))
91  self.assertEquals(mock2, sm.get_service('/serv2'))
92  self.assert_(('/serv', mock.uri) in sm.get_services())
93  self.assert_(('/serv2', mock2.uri) in sm.get_services())
94 
95  sm.unregister('/serv', mock)
96  self.assertEquals([('/serv2', mock2.uri)], sm.get_services())
97  sm.unregister('/serv2', mock2)
98  self.assertEquals([], sm.get_services())


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