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
00030
00031
00032
00033
00034 import os
00035 import sys
00036 import struct
00037 import unittest
00038 import time
00039
00040 import rospy
00041 import rospy.service
00042
00043 class MockServiceClass(object):
00044 _request_class = rospy.AnyMsg
00045 _response_class = rospy.AnyMsg
00046
00047 class TestRospyService(unittest.TestCase):
00048
00049 def test_ServiceException(self):
00050 self.assert_(isinstance(rospy.service.ServiceException(), Exception))
00051
00052 def test_ServiceManager(self):
00053 class MockService(rospy.service._Service):
00054 def __init__(self, name, service_class, uri):
00055 rospy.service._Service.__init__(self, name, service_class)
00056 self.uri = uri
00057
00058 from rospy.service import ServiceManager
00059 sm = ServiceManager()
00060 self.assertEquals({}, sm.map)
00061 try:
00062 sm.lock.acquire()
00063 finally:
00064 sm.lock.release()
00065 self.assertEquals([], sm.get_services())
00066 sm.unregister_all()
00067 self.assertEquals([], sm.get_services())
00068
00069
00070 mock = MockService('/serv', MockServiceClass, "rosrpc://uri:1")
00071 mock2 = MockService('/serv', MockServiceClass, "rosrpc://uri:2")
00072
00073 sm.register('/serv', mock)
00074 self.assertEquals(mock, sm.get_service('/serv'))
00075 self.assertEquals([('/serv', mock.uri)], sm.get_services())
00076 try:
00077 sm.register('/serv', mock2)
00078 self.fail("duplicate reg should fail")
00079 except rospy.service.ServiceException: pass
00080
00081 sm.unregister_all()
00082 self.assertEquals([], sm.get_services())
00083
00084
00085 sm.register('/serv', mock)
00086 sm.unregister('/serv', mock2)
00087 self.assertEquals(mock, sm.get_service('/serv'))
00088
00089 sm.register('/serv2', mock2)
00090 self.assertEquals(mock, sm.get_service('/serv'))
00091 self.assertEquals(mock2, sm.get_service('/serv2'))
00092 self.assert_(('/serv', mock.uri) in sm.get_services())
00093 self.assert_(('/serv2', mock2.uri) in sm.get_services())
00094
00095 sm.unregister('/serv', mock)
00096 self.assertEquals([('/serv2', mock2.uri)], sm.get_services())
00097 sm.unregister('/serv2', mock2)
00098 self.assertEquals([], sm.get_services())