fail_init_test.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2010, 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 the Willow Garage 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 
35 ##\author Kevin Watts
36 
37 ##\brief Tests that analyzer that fails to load will produce diagnostic error
38 
39 from __future__ import with_statement
40 
41 DURATION = 5
42 PKG = 'test_diagnostic_aggregator'
43 import rospy, rostest, unittest
44 from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus, KeyValue
45 from time import sleep
46 import sys
47 import threading
48 
49 from optparse import OptionParser
50 
51 
52 def get_raw_name(agg_name):
53  return agg_name.split('/')[-1]
54 
55 def get_header_name(agg_name):
56  return '/'.join(agg_name.split('/')[1:-1])
57 
58 class TestFailInit(unittest.TestCase):
59  def __init__(self, *args):
60  super(TestFailInit, self).__init__(*args)
61 
62  parser = OptionParser(usage="usage ./%prog [options]", prog="fail_init_test.py")
63  parser.add_option('--ns', action="store", default=None,
64  dest="ns", metavar="NAMESPACE",
65  help="Expected namespace that analyzer will fail in")
66  # Option comes with rostest, will fail w/o this line
67  parser.add_option('--gtest_output', action="store",
68  dest="gtest")
69 
70  options, args = parser.parse_args(rospy.myargv())
71 
72  if not options.ns:
73  parser.error("Option --ns is mandatory. Unable to parse args")
74 
75  self._ns = options.ns
76  self._item = None
77 
78  self._mutex = threading.Lock()
79 
80 
81  rospy.init_node('test_fail_init')
82  self._starttime = rospy.get_time()
83 
84  sub_agg = rospy.Subscriber("/diagnostics_agg", DiagnosticArray, self.diag_agg_cb)
85 
86  def diag_agg_cb(self, msg):
87  with self._mutex:
88  for stat in msg.status:
89  if stat.name.find(self._ns) > 0:
90  self._item = stat
91  break
92 
93  def test_fail_init(self):
94  while not rospy.is_shutdown():
95  sleep(1.0)
96  if rospy.get_time() - self._starttime > DURATION:
97  break
98 
99  self.assert_(not rospy.is_shutdown(), "Rospy shutdown!")
100 
101  with self._mutex:
102  self.assert_(self._ns, "Namespace is none. Option --ns not given")
103  self.assert_(self._item, "No item with name %s found in diag_agg" % self._ns)
104  self.assert_(self._item.level == 3, "Item failed to initialize, but was not stale. Level: %d" % self._item.level)
105 
106 
107 if __name__ == '__main__':
108  if False:
109  suite = unittest.TestSuite()
110  suite.addTest(TestFailInit('test_fail_init'))
111  unittest.TextTestRunner(verbosity = 2).run(suite)
112  else:
113  rostest.run(PKG, sys.argv[0], TestFailInit, sys.argv)
def get_header_name(agg_name)
def get_raw_name(agg_name)


test_diagnostic_aggregator
Author(s): Kevin Watts, Brice Rebsamen , Kevin Watts
autogenerated on Thu Oct 8 2020 03:21:52