test_loader.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Copyright (c) 2009, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are met:
00008 #
00009 #     * Redistributions of source code must retain the above copyright
00010 #       notice, this list of conditions and the following disclaimer.
00011 #     * Redistributions in binary form must reproduce the above copyright
00012 #       notice, this list of conditions and the following disclaimer in the
00013 #       documentation and/or other materials provided with the distribution.
00014 #     * Neither the name of the Willow Garage, Inc. nor the names of its
00015 #       contributors may be used to endorse or promote products derived from
00016 #       this software without specific prior written permission.
00017 #
00018 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00022 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 # POSSIBILITY OF SUCH DAMAGE.
00029 
00030 
00031 import roslib; roslib.load_manifest('test_nodelet')
00032 import rospy
00033 import unittest
00034 import rostest
00035 import random
00036 import subprocess
00037 
00038 from nodelet.srv import *
00039 
00040 class TestLoader(unittest.TestCase):
00041     def test_loader(self):
00042         load = rospy.ServiceProxy('/nodelet_manager/load_nodelet', NodeletLoad)
00043         unload = rospy.ServiceProxy('/nodelet_manager/unload_nodelet', NodeletUnload)
00044         list = rospy.ServiceProxy('/nodelet_manager/list', NodeletList)
00045 
00046         load.wait_for_service()
00047         unload.wait_for_service()
00048         list.wait_for_service()
00049 
00050         req = NodeletLoadRequest()
00051         req.name = "/my_nodelet"
00052         req.type = "test_nodelet/Plus"
00053 
00054         res = load.call(req)
00055         self.assertTrue(res.success)
00056 
00057         req = NodeletListRequest()
00058         res = list.call(req)
00059         self.assertTrue("/my_nodelet" in res.nodelets)
00060 
00061         req = NodeletUnloadRequest()
00062         req.name = "/my_nodelet"
00063         res = unload.call(req)
00064         self.assertTrue(res.success)
00065 
00066     def test_loader_failed_initialization(self):
00067         load = rospy.ServiceProxy('/nodelet_manager/load_nodelet', NodeletLoad)
00068         unload = rospy.ServiceProxy('/nodelet_manager/unload_nodelet', NodeletUnload)
00069         list = rospy.ServiceProxy('/nodelet_manager/list', NodeletList)
00070 
00071         load.wait_for_service()
00072         unload.wait_for_service()
00073         list.wait_for_service()
00074 
00075         req = NodeletLoadRequest()
00076         req.name = "/my_nodelet"
00077         req.type = "test_nodelet/FailingNodelet"
00078         self.assertRaises(rospy.ServiceException, load.call, req)
00079 
00080         # If the Nodelet failed to initialize, it shouldn't be stored in the
00081         # nodelet map
00082         req = NodeletListRequest()
00083         res = list.call(req)
00084         self.assertFalse("/my_nodelet" in res.nodelets)
00085 
00086         req = NodeletUnloadRequest()
00087         req.name = "/my_nodelet"
00088         self.assertRaises(rospy.ServiceException, unload.call, req)
00089 
00090     def test_loader_error_message(self):
00091         proc = subprocess.Popen(["rosrun", "test_nodelet", "create_instance_cb_error"],
00092             stderr=subprocess.PIPE
00093         )
00094 
00095         # The load should fail
00096         self.assertEqual(proc.wait(), 1)
00097 
00098         # And the loader should print our error message
00099         out = proc.stderr.read()
00100         self.assertIn('NODELET_TEST_FAILURE', out)
00101 
00102 if __name__ == '__main__':
00103     rospy.init_node('test_loader')
00104     rostest.unitrun('test_loader', 'test_loader', TestLoader)
00105 


test_nodelet
Author(s): Tully Foote
autogenerated on Sun Feb 17 2019 03:43:56