diff options
author | kgriffs <kurt.griffiths@rackspace.com> | 2013-09-19 16:10:03 -0500 |
---|---|---|
committer | kgriffs <kurt.griffiths@rackspace.com> | 2013-09-19 16:24:57 -0500 |
commit | 3bc6f79d794aee9d51abc0be10e846b971fae847 (patch) | |
tree | 6b2418fcf88a5060428e27dfea39f8d25951d7b6 | |
parent | 6703c5cc744b183fea69cef42d03d4d3498cd22c (diff) | |
download | oslo-incubator-3bc6f79d794aee9d51abc0be10e846b971fae847.tar.gz |
Fix timeutils.set_override_time not defaulting to current wall time
This patch modifies timeutils.set_override_time so that it does not
default to setting the override to the current time when the function
*definition* is executed, but rather defaults to using the current
time at the moment the function is *called*.
The thinking is that this behavior is more along the lines of what
the developer would expect when using this function. The new behavior
is called out explicitly in the docstring.
Change-Id: I5a2d449c76a392c902d7a89617d63d7f1e896276
-rw-r--r-- | openstack/common/timeutils.py | 7 | ||||
-rw-r--r-- | tests/unit/test_timeutils.py | 14 |
2 files changed, 19 insertions, 2 deletions
diff --git a/openstack/common/timeutils.py b/openstack/common/timeutils.py index 60f02bcb..98d877d5 100644 --- a/openstack/common/timeutils.py +++ b/openstack/common/timeutils.py @@ -117,12 +117,15 @@ def iso8601_from_timestamp(timestamp): utcnow.override_time = None -def set_time_override(override_time=datetime.datetime.utcnow()): +def set_time_override(override_time=None): """Overrides utils.utcnow. Make it return a constant time or a list thereof, one at a time. + + :param override_time: datetime instance or list thereof. If not + given, defaults to the current UTC time. """ - utcnow.override_time = override_time + utcnow.override_time = override_time or datetime.datetime.utcnow() def advance_time_delta(timedelta): diff --git a/tests/unit/test_timeutils.py b/tests/unit/test_timeutils.py index 859e79f9..dfcdcf7c 100644 --- a/tests/unit/test_timeutils.py +++ b/tests/unit/test_timeutils.py @@ -17,9 +17,11 @@ import calendar import datetime +import time import iso8601 import mock +from testtools import matchers from openstack.common import test from openstack.common import timeutils @@ -122,6 +124,18 @@ class TimeUtilsTest(test.BaseTestCase): def test_is_newer_than_str(self): self._test_is_newer_than(timeutils.strtime) + def test_set_time_override_using_default(self): + now = timeutils.utcnow_ts() + + # NOTE(kgriffs): Normally it's bad form to sleep in a unit test, + # but this is the only way to test that set_time_override defaults + # to setting the override to the current time. + time.sleep(1) + + timeutils.set_time_override() + overriden_now = timeutils.utcnow_ts() + self.assertThat(now, matchers.LessThan(overriden_now)) + def test_utcnow_ts(self): skynet_self_aware_ts = 872835240 skynet_dt = datetime.datetime.utcfromtimestamp(skynet_self_aware_ts) |