Skip to content

Commit

Permalink
Allow to ignore time.monotonic
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Šebek committed Aug 29, 2017
1 parent 2c1a724 commit d165c16
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ FreezeGun is a library that allows your python tests to travel through time by m
Usage
-----

Once the decorator or context manager have been invoked, all calls to datetime.datetime.now(), datetime.datetime.utcnow(), datetime.date.today(), time.time(), time.localtime(), time.gmtime(), and time.strftime() will return the time that has been frozen.
Once the decorator or context manager have been invoked, all calls to datetime.datetime.now(), datetime.datetime.utcnow(), datetime.date.today(), time.time(), time.localtime(), time.gmtime(), time.strftime() and time.monotonic() will return the time that has been frozen.

Decorator
~~~~~~~~~
Expand Down Expand Up @@ -173,6 +173,7 @@ Freezegun allows moving time to specific dates.
Parameter for ``move_to`` can be any valid ``freeze_time`` date (string, date, datetime).

Note that for ``tick``, manual tick and ``move_to`` there is by default moved frozen time.monotonic(). If you need to turn this off just add ``'time.monotonic'`` to ``ignore`` parameter.

Default Arguments
~~~~~~~~~~~~~~~~~
Expand Down
10 changes: 8 additions & 2 deletions freezegun/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def start(self):
('real_strftime', real_strftime, 'FakeStrfTime', fake_strftime),
('real_time', real_time, 'FakeTime', fake_time),
]
if real_monotonic is not None:
if real_monotonic is not None and 'time.monotonic' not in self.ignore:
fake_monotonic = FakeMonotonic(time_to_freeze, time.monotonic)
time.monotonic = fake_monotonic
to_patch.append(('real_monotonic', real_monotonic, 'FakeMonotonic', fake_monotonic))
Expand All @@ -449,6 +449,9 @@ def start(self):
for module_attribute in dir(module):
if module_attribute in real_names:
continue
if '{}.{}'.format(mod_name, module_attribute) in self.ignore:
continue

try:
attribute_value = getattr(module, module_attribute)
except (ImportError, AttributeError, TypeError):
Expand Down Expand Up @@ -499,6 +502,9 @@ def stop(self):

if module_attribute in self.fake_names:
continue
if '{}.{}'.format(mod_name, module_attribute) in self.ignore:
continue

try:
attribute_value = getattr(module, module_attribute)
except (ImportError, AttributeError, TypeError):
Expand All @@ -513,7 +519,7 @@ def stop(self):
time.gmtime = time.gmtime.previous_gmtime_function
time.localtime = time.localtime.previous_localtime_function
time.strftime = time.strftime.previous_strftime_function
if real_monotonic is not None:
if real_monotonic is not None and 'time.monotonic' not in self.ignore:
time.monotonic = time.monotonic.previous_monotonic_function

uuid._uuid_generate_time = real_uuid_generate_time
Expand Down
21 changes: 21 additions & 0 deletions tests/test_datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,27 @@ def test_move_to_monotonic():
assert time.monotonic() == initial_monotonic


def test_monotonic_can_be_ignored():
if sys.version_info[0] != 3:
raise skip.SkipTest("test target is Python3")

initial_datetime = datetime.datetime(year=1, month=7, day=12,
hour=15, minute=6, second=3)

other_datetime = datetime.datetime(year=2, month=8, day=13,
hour=14, minute=5, second=0)
# time.monotonic() will not affected by freeze_time
with freeze_time(initial_datetime, ignore=['time.monotonic']) as frozen_datetime:
initial_monotonic = time.monotonic()

frozen_datetime.move_to(other_datetime)
second_monotonic = time.monotonic()
assert second_monotonic > initial_monotonic

frozen_datetime.move_to(initial_datetime)
assert time.monotonic() > second_monotonic


def test_bad_time_argument():
try:
freeze_time("2012-13-14", tz_offset=-4)
Expand Down

0 comments on commit d165c16

Please sign in to comment.