test_rospy_timer_online.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2008, 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 Willow Garage, Inc. 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 from __future__ import print_function
35 
36 import os
37 import sys
38 import unittest
39 import time
40 
41 import rosunit
42 
43 from threading import Thread
44 
45 class TestRospyTimerOnline(unittest.TestCase):
46 
47  def __init__(self, *args):
48  unittest.TestCase.__init__(self, *args)
49  import rospy
50  rospy.init_node('test_rospy_timer_online')
51  self.timer_callbacks = 0
52  self.timer_event = None
53 
54  def test_sleep(self):
55  import rospy
56  import time
57  t = time.time()
58  rospy.sleep(0.1)
59  dur = time.time() - t
60  # #2842 raising bounds from .01 to .03 for amazon VM
61 
62  # make sure sleep is approximately right
63  self.assert_(abs(dur - 0.1) < 0.03, dur)
64 
65  t = time.time()
66  rospy.sleep(rospy.Duration.from_sec(0.1))
67  dur = time.time() - t
68  # make sure sleep is approximately right
69  self.assert_(abs(dur - 0.1) < 0.03, dur)
70 
71  # sleep for neg duration
72  t = time.time()
73  rospy.sleep(rospy.Duration.from_sec(-10.))
74  dur = time.time() - t
75  # make sure returned immediately
76  self.assert_(abs(dur) < 0.1, dur)
77 
78  def test_Rate(self):
79  import rospy
80  import time
81  t = time.time()
82  count = 0
83  r = rospy.Rate(10)
84  for x in range(10):
85  r.sleep()
86  dur = time.time() - t
87  # make sure sleep is approximately right
88  self.assert_(abs(dur - 1.0) < 0.5, dur)
89 
90  def _Timer_callback(self, event):
91  self.timer_callbacks += 1
92  self.timer_event = event
93 
94  def callback(event):
95  print('last_expected: ', event.last_expected)
96  print('last_real: ', event.last_real)
97  print('current_expected: ', event.current_expected)
98  print('current_real: ', event.current_real)
99  print('current_error: ', (event.current_real - event.current_expected).to_sec())
100  print('profile.last_duration:', event.last_duration)
101  if event.last_real:
102  print('last_error: ', (event.last_real - event.last_expected).to_sec(), 'secs')
103 
104  def test_Timer(self):
105  import rospy
106  timer = rospy.Timer(rospy.Duration(1), self._Timer_callback)
107  time.sleep(10)
108  timer.shutdown()
109 
110  # make sure we got an approximately correct number of callbacks
111  self.assert_(abs(self.timer_callbacks - 10) < 2)
112  # make sure error is approximately correct. the Timer
113  # implementation tracks error in accumulated real time.
114  ev = self.timer_event
115  self.assert_(ev is not None)
116  self.assert_(abs((ev.current_real - ev.current_expected).to_sec()) < 2.)
117 
118 if __name__ == '__main__':
119  rosunit.unitrun('test_rospy', sys.argv[0], TestRospyTimerOnline, coverage_packages=['rospy.timer'])


test_rospy
Author(s): Ken Conley
autogenerated on Sun Feb 3 2019 03:30:22