$search
00001 #!/usr/bin/env python 00002 # Software License Agreement (BSD License) 00003 # 00004 # Copyright (c) 2008, 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 Willow Garage, Inc. 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 # Revision $Id: test_rospy_core.py 4218 2009-04-16 21:09:31Z sfkwc $ 00035 00036 import roslib; roslib.load_manifest('test_rospy') 00037 00038 import os 00039 import sys 00040 import unittest 00041 import time 00042 00043 import rostest 00044 00045 from threading import Thread 00046 00047 class TestRospyTimerOnline(unittest.TestCase): 00048 00049 def __init__(self, *args): 00050 unittest.TestCase.__init__(self, *args) 00051 import rospy 00052 rospy.init_node('test_rospy_timer_online') 00053 self.timer_callbacks = 0 00054 self.timer_event = None 00055 00056 def test_sleep(self): 00057 import rospy 00058 import time 00059 t = time.time() 00060 rospy.sleep(0.1) 00061 dur = time.time() - t 00062 # #2842 raising bounds from .01 to .03 for amazon VM 00063 00064 # make sure sleep is approximately right 00065 self.assert_(abs(dur - 0.1) < 0.03, dur) 00066 00067 t = time.time() 00068 rospy.sleep(rospy.Duration.from_sec(0.1)) 00069 dur = time.time() - t 00070 # make sure sleep is approximately right 00071 self.assert_(abs(dur - 0.1) < 0.03, dur) 00072 00073 # sleep for neg duration 00074 t = time.time() 00075 rospy.sleep(rospy.Duration.from_sec(-10.)) 00076 dur = time.time() - t 00077 # make sure returned immediately 00078 self.assert_(abs(dur) < 0.1, dur) 00079 00080 def test_Rate(self): 00081 import rospy 00082 import time 00083 t = time.time() 00084 count = 0 00085 r = rospy.Rate(10) 00086 for x in xrange(10): 00087 r.sleep() 00088 dur = time.time() - t 00089 # make sure sleep is approximately right 00090 self.assert_(abs(dur - 1.0) < 0.5, dur) 00091 00092 def _Timer_callback(self, event): 00093 self.timer_callbacks += 1 00094 self.timer_event = event 00095 00096 def callback(event): 00097 print 'last_expected: ', event.last_expected 00098 print 'last_real: ', event.last_real 00099 print 'current_expected: ', event.current_expected 00100 print 'current_real: ', event.current_real 00101 print 'current_error: ', (event.current_real - event.current_expected).to_sec() 00102 print 'profile.last_duration:', event.last_duration 00103 if event.last_real: 00104 print 'last_error: ', (event.last_real - event.last_expected).to_sec(), 'secs' 00105 00106 def test_Timer(self): 00107 import rospy 00108 timer = rospy.Timer(rospy.Duration(1), self._Timer_callback) 00109 time.sleep(10) 00110 timer.shutdown() 00111 00112 # make sure we got an approximately correct number of callbacks 00113 self.assert_(abs(self.timer_callbacks - 10) < 2) 00114 # make sure error is approximately correct. the Timer 00115 # implementation tracks error in accumulated real time. 00116 ev = self.timer_event 00117 self.assert_(ev is not None) 00118 self.assert_(abs((ev.current_real - ev.current_expected).to_sec()) < 2.) 00119 00120 if __name__ == '__main__': 00121 rostest.unitrun('test_rospy', sys.argv[0], TestRospyTimerOnline, coverage_packages=['rospy.timer'])