fail_init_test.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2010, Willow Garage, Inc.
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of the Willow Garage nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 #
00034 
00035 ##\author Kevin Watts
00036 
00037 ##\brief Tests that analyzer that fails to load will produce diagnostic error
00038 
00039 from __future__ import with_statement
00040 
00041 DURATION = 5
00042 PKG = 'test_diagnostic_aggregator'
00043 import roslib; roslib.load_manifest(PKG)
00044 import rospy, rostest, unittest
00045 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus, KeyValue
00046 from time import sleep
00047 import sys
00048 import threading
00049 
00050 from optparse import OptionParser
00051 
00052 
00053 def get_raw_name(agg_name):
00054     return agg_name.split('/')[-1]
00055 
00056 def get_header_name(agg_name):
00057     return '/'.join(agg_name.split('/')[1:-1])
00058 
00059 class TestFailInit(unittest.TestCase):
00060     def __init__(self, *args):
00061         super(TestFailInit, self).__init__(*args)
00062 
00063         parser = OptionParser(usage="usage ./%prog [options]", prog="fail_init_test.py")
00064         parser.add_option('--ns', action="store", default=None,
00065                           dest="ns", metavar="NAMESPACE",
00066                           help="Expected namespace that analyzer will fail in")
00067         # Option comes with rostest, will fail w/o this line
00068         parser.add_option('--gtest_output', action="store",
00069                           dest="gtest")
00070 
00071         options, args = parser.parse_args(rospy.myargv())
00072 
00073         if not options.ns:
00074             parser.error("Option --ns is mandatory. Unable to parse args")
00075 
00076         self._ns = options.ns
00077         self._item = None
00078 
00079         self._mutex = threading.Lock()
00080 
00081 
00082         rospy.init_node('test_fail_init')
00083         self._starttime = rospy.get_time()
00084 
00085         sub_agg = rospy.Subscriber("/diagnostics_agg", DiagnosticArray, self.diag_agg_cb)
00086         
00087     def diag_agg_cb(self, msg):
00088         with self._mutex:
00089             for stat in msg.status:
00090                 if stat.name.find(self._ns) > 0:
00091                     self._item = stat
00092                     break
00093 
00094     def test_fail_init(self):
00095         while not rospy.is_shutdown():
00096             sleep(1.0)
00097             if rospy.get_time() - self._starttime > DURATION:
00098                 break
00099         
00100         self.assert_(not rospy.is_shutdown(), "Rospy shutdown!")
00101 
00102         with self._mutex:
00103             self.assert_(self._ns, "Namespace is none. Option --ns not given")
00104             self.assert_(self._item, "No item with name %s found in diag_agg" % self._ns)
00105             self.assert_(self._item.level == 3, "Item failed to initialize, but was not stale. Level: %d" % self._item.level)
00106             
00107             
00108 if __name__ == '__main__':
00109     if False:
00110         suite = unittest.TestSuite()
00111         suite.addTest(TestFailInit('test_fail_init'))
00112         unittest.TextTestRunner(verbosity = 2).run(suite)
00113     else:
00114         rostest.run(PKG, sys.argv[0], TestFailInit, sys.argv)


test_diagnostic_aggregator
Author(s): Kevin Watts
autogenerated on Fri Jan 3 2014 11:19:12