00001 #!/usr/bin/env python 00002 # 00003 # Software License Agreement (BSD License) 00004 # 00005 # Copyright (c) 2017, TNO IVS, Helmond, Netherlands 00006 # All rights reserved. 00007 # 00008 # Redistribution and use in source and binary forms, with or without 00009 # modification, are permitted provided that the following conditions 00010 # are met: 00011 # 00012 # * Redistributions of source code must retain the above copyright 00013 # notice, this list of conditions and the following disclaimer. 00014 # * Redistributions in binary form must reproduce the above 00015 # copyright notice, this list of conditions and the following 00016 # disclaimer in the documentation and/or other materials provided 00017 # with the distribution. 00018 # * Neither the name of the TNO IVS nor the names of its 00019 # contributors may be used to endorse or promote products derived 00020 # from this software without specific prior written permission. 00021 # 00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 # POSSIBILITY OF SUCH DAMAGE. 00034 00035 # \author Rein Appeldoorn 00036 00037 00038 import unittest 00039 import rospy 00040 import rostest 00041 from diagnostic_msgs.msg import DiagnosticArray 00042 00043 00044 class TestCPUMonitor(unittest.TestCase): 00045 00046 def diagnostics_callback(self, msg): 00047 self._msg = msg 00048 00049 def test_cpu_monitor_diagnostics(self): 00050 rospy.init_node('test_cpu_monitor') 00051 self._expected_level = None 00052 if rospy.has_param('~expected_level'): 00053 self._expected_level = rospy.get_param('~expected_level') 00054 00055 self._subscriber = rospy.Subscriber('diagnostics', DiagnosticArray, self.diagnostics_callback) 00056 self._msg = None 00057 00058 while not self._msg: 00059 rospy.sleep(.1) 00060 self._subscriber.unregister() 00061 00062 self.assertEqual(len(self._msg.status), 1) 00063 status = self._msg.status[0] 00064 00065 for v in status.values: 00066 percentage = float(v.value) 00067 self.assertGreaterEqual(percentage, 0) 00068 self.assertLessEqual(percentage, 100) 00069 00070 if self._expected_level: 00071 self.assertEqual(self._expected_level, status.level) 00072 00073 00074 PKG = 'diagnostics_common_diagnostics' 00075 NAME = 'test_cpu_monitor' 00076 if __name__ == '__main__': 00077 rostest.unitrun(PKG, NAME, TestCPUMonitor)