test_rospy_rostime.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 import os
35 import sys
36 import struct
37 import unittest
38 try:
39  from cStringIO import StringIO
40 except ImportError:
41  from io import StringIO
42 import time
43 import random
44 
45 import rospy.rostime
46 
47 try:
48  import cPickle as pickle
49 except ImportError:
50  import pickle
51 
52 # currently tests rospy.rostime, rospy.simtime, and some parts of rospy.client
53 
54 class TestRospyTime(unittest.TestCase):
55 
56  def setUp(self):
57  rospy.rostime.set_rostime_initialized(True)
58 
60  # trip wire test, make sure the module is loading
61  import rospy.impl.simtime
62  # can't actually do unit tests of simtime, requires rosunit
63 
65  rospy.rostime.switch_to_wallclock()
66  self.assertAlmostEqual(time.time(), rospy.get_time(), 1)
67 
69  # use deepcopy to test getstate/setstate
70  import copy, random
71  a = rospy.Time(random.randint(0, 10000), random.randint(0, 10000))
72  b = copy.deepcopy(a)
73  self.assertEquals(a.secs, b.secs)
74  self.assertEquals(a.nsecs, b.nsecs)
75 
76  buff = StringIO()
77  pickle.dump(a, buff)
78  buff.seek(0)
79  c = pickle.load(buff)
80  self.assertEquals(a.secs, c.secs)
81  self.assertEquals(a.nsecs, c.nsecs)
82 
84  # use deepcopy to test getstate/setstate
85  import copy, random
86  a = rospy.Duration(random.randint(0, 10000), random.randint(0, 10000))
87  b = copy.deepcopy(a)
88  self.assertEquals(a.secs, b.secs)
89  self.assertEquals(a.nsecs, b.nsecs)
90 
91  buff = StringIO()
92  pickle.dump(a, buff)
93  buff.seek(0)
94  c = pickle.load(buff)
95  self.assertEquals(a.secs, c.secs)
96  self.assertEquals(a.nsecs, c.nsecs)
97 
98  def test_Time(self):
99  # This is a copy of test_roslib_rostime
100  from rospy import Time, Duration
101  # #1600 Duration > Time should fail
102  failed = False
103  try:
104  v = Duration.from_sec(0.1) > Time.from_sec(0.5)
105  failed = True
106  except: pass
107  self.failIf(failed, "should have failed to compare")
108  try:
109  v = Time.from_sec(0.4) > Duration.from_sec(0.1)
110  failed = True
111  except: pass
112  self.failIf(failed, "should have failed to compare")
113 
114 
115  try: # neg time fails
116  Time(-1)
117  failed = True
118  except: pass
119  self.failIf(failed, "negative time not allowed")
120  try:
121  Time(1, -1000000001)
122  failed = True
123  except: pass
124  self.failIf(failed, "negative time not allowed")
125 
126  # test Time.now() is within 10 seconds of actual time (really generous)
127  import time
128  t = time.time()
129  v = Time.from_sec(t)
130  self.assertEquals(v.to_sec(), t)
131  # test from_sec()
132  self.assertEquals(Time.from_sec(0), Time())
133  self.assertEquals(Time.from_sec(1.), Time(1))
134  self.assertEquals(Time.from_sec(v.to_sec()), v)
135  self.assertEquals(v.from_sec(v.to_sec()), v)
136 
137  # test to_time()
138  self.assertEquals(v.to_sec(), v.to_time())
139 
140  # test addition
141  # - time + time fails
142  try:
143  v = Time(1,0) + Time(1, 0)
144  failed = True
145  except: pass
146  self.failIf(failed, "Time + Time must fail")
147 
148  # - time + duration
149  v = Time(1,0) + Duration(1, 0)
150  self.assertEquals(Time(2, 0), v)
151  v = Duration(1, 0) + Time(1,0)
152  self.assertEquals(Time(2, 0), v)
153  v = Time(1,1) + Duration(1, 1)
154  self.assertEquals(Time(2, 2), v)
155  v = Duration(1, 1) + Time(1,1)
156  self.assertEquals(Time(2, 2), v)
157 
158  v = Time(1) + Duration(0, 1000000000)
159  self.assertEquals(Time(2), v)
160  v = Duration(1) + Time(0, 1000000000)
161  self.assertEquals(Time(2), v)
162 
163  v = Time(100, 100) + Duration(300)
164  self.assertEquals(Time(400, 100), v)
165  v = Duration(300) + Time(100, 100)
166  self.assertEquals(Time(400, 100), v)
167 
168  v = Time(100, 100) + Duration(300, 300)
169  self.assertEquals(Time(400, 400), v)
170  v = Duration(300, 300) + Time(100, 100)
171  self.assertEquals(Time(400, 400), v)
172 
173  v = Time(100, 100) + Duration(300, -101)
174  self.assertEquals(Time(399, 999999999), v)
175  v = Duration(300, -101) + Time(100, 100)
176  self.assertEquals(Time(399, 999999999), v)
177 
178  # test subtraction
179  try:
180  v = Time(1,0) - 1
181  failed = True
182  except: pass
183  self.failIf(failed, "Time - non Duration must fail")
184  class Foob(object): pass
185  try:
186  v = Time(1,0) - Foob()
187  failed = True
188  except: pass
189  self.failIf(failed, "Time - non TVal must fail")
190 
191  # - Time - Duration
192  v = Time(1,0) - Duration(1, 0)
193  self.assertEquals(Time(), v)
194 
195  v = Time(1,1) - Duration(-1, -1)
196  self.assertEquals(Time(2, 2), v)
197  v = Time(1) - Duration(0, 1000000000)
198  self.assertEquals(Time(), v)
199  v = Time(2) - Duration(0, 1000000000)
200  self.assertEquals(Time(1), v)
201  v = Time(400, 100) - Duration(300)
202  self.assertEquals(Time(100, 100), v)
203  v = Time(100, 100) - Duration(0, 101)
204  self.assertEquals(Time(99, 999999999), v)
205 
206  # - Time - Time = Duration
207  v = Time(100, 100) - Time(100, 100)
208  self.assertEquals(Duration(), v)
209  v = Time(100, 100) - Time(100)
210  self.assertEquals(Duration(0,100), v)
211  v = Time(100) - Time(200)
212  self.assertEquals(Duration(-100), v)
213 
214  def test_Duration(self):
215  Duration = rospy.Duration
216 
217  # test from_sec
218  v = Duration(1000)
219  self.assertEquals(v, Duration.from_sec(v.to_sec()))
220  self.assertEquals(v, v.from_sec(v.to_sec()))
221  v = Duration(0,1000)
222  self.assertEquals(v, Duration.from_sec(v.to_sec()))
223  self.assertEquals(v, v.from_sec(v.to_sec()))
224 
225  # test neg
226  v = -Duration(1, -1)
227  self.assertEquals(-1, v.secs)
228  self.assertEquals(1, v.nsecs)
229  v = -Duration(-1, -1)
230  self.assertEquals(1, v.secs)
231  self.assertEquals(1, v.nsecs)
232  v = -Duration(-1, 1)
233  self.assertEquals(0, v.secs)
234  self.assertEquals(999999999, v.nsecs)
235 
236  # test addition
237  failed = False
238  try:
239  v = Duration(1,0) + Time(1, 0)
240  failed = True
241  except: pass
242  self.failIf(failed, "Duration + Time must fail")
243  try:
244  v = Duration(1,0) + 1
245  failed = True
246  except: pass
247  self.failIf(failed, "Duration + int must fail")
248 
249  v = Duration(1,0) + Duration(1, 0)
250  self.assertEquals(2, v.secs)
251  self.assertEquals(0, v.nsecs)
252  self.assertEquals(Duration(2, 0), v)
253  v = Duration(-1,-1) + Duration(1, 1)
254  self.assertEquals(0, v.secs)
255  self.assertEquals(0, v.nsecs)
256  self.assertEquals(Duration(), v)
257  v = Duration(1) + Duration(0, 1000000000)
258  self.assertEquals(2, v.secs)
259  self.assertEquals(0, v.nsecs)
260  self.assertEquals(Duration(2), v)
261  v = Duration(100, 100) + Duration(300)
262  self.assertEquals(Duration(400, 100), v)
263  v = Duration(100, 100) + Duration(300, 300)
264  self.assertEquals(Duration(400, 400), v)
265  v = Duration(100, 100) + Duration(300, -101)
266  self.assertEquals(Duration(399, 999999999), v)
267 
268  # test subtraction
269  try:
270  v = Duration(1,0) - 1
271  failed = True
272  except: pass
273  self.failIf(failed, "Duration - non duration must fail")
274  try:
275  v = Duration(1, 0) - Time(1,0)
276  failed = True
277  except: pass
278  self.failIf(failed, "Duration - Time must fail")
279 
280  v = Duration(1,0) - Duration(1, 0)
281  self.assertEquals(Duration(), v)
282  v = Duration(-1,-1) - Duration(1, 1)
283  self.assertEquals(Duration(-3, 999999998), v)
284  v = Duration(1) - Duration(0, 1000000000)
285  self.assertEquals(Duration(), v)
286  v = Duration(2) - Duration(0, 1000000000)
287  self.assertEquals(Duration(1), v)
288  v = Duration(100, 100) - Duration(300)
289  self.assertEquals(Duration(-200, 100), v)
290  v = Duration(100, 100) - Duration(300, 101)
291  self.assertEquals(Duration(-201, 999999999), v)
292 
293  # test abs
294  self.assertEquals(abs(Duration()), Duration())
295  self.assertEquals(abs(Duration(1)), Duration(1))
296  self.assertEquals(abs(Duration(-1)), Duration(1))
297  self.assertEquals(abs(Duration(0,-1)), Duration(0,1))
298  self.assertEquals(abs(Duration(-1,-1)), Duration(1,1))
299 
300  def test_set_rostime(self):
301  from rospy.rostime import _set_rostime
302  from rospy import Time
303 
304  self.assertAlmostEqual(time.time(), rospy.get_time(), 1)
305 
306  for t in [Time.from_sec(1.0), Time.from_sec(4.0)]:
307  _set_rostime(t)
308  self.assertEquals(t, rospy.get_rostime())
309  self.assertEquals(t.to_time(), rospy.get_time())
310 
311  def test_get_rostime(self):
312  rospy.rostime.switch_to_wallclock()
313  self.assertAlmostEqual(time.time(), rospy.get_time(), 1)
314  self.assertAlmostEqual(time.time(), rospy.get_rostime().to_time(), 1)
315  #rest of get_rostime implicitly tested by update_rostime tests
316 
317  def test_sleep(self):
318  # test wallclock sleep
319  rospy.rostime.switch_to_wallclock()
320  rospy.sleep(0.1)
321  rospy.sleep(rospy.Duration.from_sec(0.1))
322 
323  from rospy.rostime import _set_rostime
324  from rospy import Time
325 
326  t = Time.from_sec(1.0)
327  _set_rostime(t)
328  self.assertEquals(t, rospy.get_rostime())
329  self.assertEquals(t.to_time(), rospy.get_time())
330 
331  import threading
332 
333  #start sleeper
334  self.failIf(test_sleep_done)
335  sleepthread = threading.Thread(target=sleeper, args=())
336  sleepthread.setDaemon(True)
337  sleepthread.start()
338  time.sleep(1.0) #make sure thread is spun up
339  self.failIf(test_sleep_done)
340 
341  t = Time.from_sec(1000000.0)
342  _set_rostime(t)
343  time.sleep(0.5) #give sleeper time to wakeup
344  self.assert_(test_sleep_done, "sleeper did not wake up")
345 
346  #start duration sleeper
347  self.failIf(test_duration_sleep_done)
348  dursleepthread = threading.Thread(target=duration_sleeper, args=())
349  dursleepthread.setDaemon(True)
350  dursleepthread.start()
351  time.sleep(1.0) #make sure thread is spun up
352  self.failIf(test_duration_sleep_done)
353 
354  t = Time.from_sec(2000000.0)
355  _set_rostime(t)
356  time.sleep(0.5) #give sleeper time to wakeup
357  self.assert_(test_sleep_done, "sleeper did not wake up")
358 
359  #start backwards sleeper
360  self.failIf(test_backwards_sleep_done)
361  backsleepthread = threading.Thread(target=backwards_sleeper, args=())
362  backsleepthread.setDaemon(True)
363  backsleepthread.start()
364  time.sleep(1.0) #make sure thread is spun up
365  self.failIf(test_backwards_sleep_done)
366 
367  t = Time.from_sec(1.0)
368  _set_rostime(t)
369  time.sleep(0.5) #give sleeper time to wakeup
370  self.assert_(test_backwards_sleep_done, "backwards sleeper was not given an exception")
371 
372 test_duration_sleep_done = False
374  global test_duration_sleep_done
375  rospy.sleep(rospy.Duration(10000.0))
376  test_duration_sleep_done = True
377 
378 test_sleep_done = False
379 def sleeper():
380  global test_sleep_done
381  rospy.sleep(10000.0)
382  test_sleep_done = True
383 test_backwards_sleep_done = False
385  global test_backwards_sleep_done
386  try:
387  rospy.sleep(10000.0)
388  except rospy.ROSException:
389  test_backwards_sleep_done = True


test_rospy
Author(s): Ken Conley, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:56