diff --git a/backoff/_async.py b/backoff/_async.py index 82fd477..e3fac73 100644 --- a/backoff/_async.py +++ b/backoff/_async.py @@ -2,6 +2,7 @@ import datetime import functools import asyncio +import time from datetime import timedelta from backoff._common import (_init_wait_gen, _maybe_call, _next_wait) @@ -60,11 +61,11 @@ async def retry(*args, **kwargs): max_time_value = _maybe_call(max_time) tries = 0 - start = datetime.datetime.now() + start = time.monotonic() wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 - elapsed = timedelta.total_seconds(datetime.datetime.now() - start) + elapsed = time.monotonic() - start details = { "target": target, "args": args, @@ -134,11 +135,11 @@ async def retry(*args, **kwargs): max_time_value = _maybe_call(max_time) tries = 0 - start = datetime.datetime.now() + start = time.monotonic() wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 - elapsed = timedelta.total_seconds(datetime.datetime.now() - start) + elapsed = time.monotonic() - start details = { "target": target, "args": args, diff --git a/backoff/_sync.py b/backoff/_sync.py index 4371e16..0ed6860 100644 --- a/backoff/_sync.py +++ b/backoff/_sync.py @@ -1,5 +1,4 @@ # coding:utf-8 -import datetime import functools import time from datetime import timedelta @@ -32,11 +31,11 @@ def retry(*args, **kwargs): max_time_value = _maybe_call(max_time) tries = 0 - start = datetime.datetime.now() + start = time.monotonic() wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 - elapsed = timedelta.total_seconds(datetime.datetime.now() - start) + elapsed = time.monotonic() - start details = { "target": target, "args": args, @@ -88,11 +87,11 @@ def retry(*args, **kwargs): max_time_value = _maybe_call(max_time) tries = 0 - start = datetime.datetime.now() + start = time.monotonic() wait = _init_wait_gen(wait_gen, wait_gen_kwargs) while True: tries += 1 - elapsed = timedelta.total_seconds(datetime.datetime.now() - start) + elapsed = time.monotonic() - start details = { "target": target, "args": args, diff --git a/tests/test_backoff.py b/tests/test_backoff.py index cd33b63..9556cd0 100644 --- a/tests/test_backoff.py +++ b/tests/test_backoff.py @@ -46,19 +46,19 @@ def return_true(log, n): def test_on_predicate_max_time(monkeypatch): nows = [ - datetime.datetime(2018, 1, 1, 12, 0, 10, 5), - datetime.datetime(2018, 1, 1, 12, 0, 9, 0), - datetime.datetime(2018, 1, 1, 12, 0, 1, 0), - datetime.datetime(2018, 1, 1, 12, 0, 0, 0), + 10.000005, + 9, + 1, + 0 ] - class Datetime: + class Time: @staticmethod - def now(): + def monotonic(): return nows.pop() monkeypatch.setattr('time.sleep', lambda x: None) - monkeypatch.setattr('datetime.datetime', Datetime) + monkeypatch.setattr('time.monotonic', Time.monotonic) def giveup(details): assert details['tries'] == 3 @@ -79,19 +79,19 @@ def return_true(log, n): def test_on_predicate_max_time_callable(monkeypatch): nows = [ - datetime.datetime(2018, 1, 1, 12, 0, 10, 5), - datetime.datetime(2018, 1, 1, 12, 0, 9, 0), - datetime.datetime(2018, 1, 1, 12, 0, 1, 0), - datetime.datetime(2018, 1, 1, 12, 0, 0, 0), + 10.000005, + 9, + 1, + 0 ] - class Datetime: + class Time: @staticmethod - def now(): + def monotonic(): return nows.pop() monkeypatch.setattr('time.sleep', lambda x: None) - monkeypatch.setattr('datetime.datetime', Datetime) + monkeypatch.setattr('time.monotonic', Time.monotonic) def giveup(details): assert details['tries'] == 3