From e3057d1ab99dd5cac19c375d07fd0f53e9b14939 Mon Sep 17 00:00:00 2001 From: luccabb Date: Wed, 6 Mar 2024 00:54:29 -0800 Subject: [PATCH 1/3] monotonic time --- backoff/_async.py | 9 +++++---- backoff/_sync.py | 9 ++++----- tests/test_backoff.py | 30 ++++++++++++++---------------- 3 files changed, 23 insertions(+), 25 deletions(-) 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..5fd7d38 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 @@ -479,7 +479,6 @@ def emptiness(*args, **kwargs): assert len(logger.giveups) == 1 details = dict(logger.giveups[0]) - print(details) elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), @@ -568,7 +567,6 @@ def success(*args, **kwargs): for i in range(2): details = backoffs[i] - print(details) elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), From fd4ff9a2aabef1259f7bca729ae2e8de9b1fea3f Mon Sep 17 00:00:00 2001 From: luccabb Date: Wed, 6 Mar 2024 01:00:15 -0800 Subject: [PATCH 2/3] add print --- tests/test_backoff.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_backoff.py b/tests/test_backoff.py index 5fd7d38..8d58daa 100644 --- a/tests/test_backoff.py +++ b/tests/test_backoff.py @@ -479,6 +479,7 @@ def emptiness(*args, **kwargs): assert len(logger.giveups) == 1 details = dict(logger.giveups[0]) + print(details) elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), @@ -568,6 +569,7 @@ def success(*args, **kwargs): for i in range(2): details = backoffs[i] elapsed = details.pop('elapsed') + print(details) assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2}, From 5c8caaab70029909c5328f7a2a3fac9ea2e1b2c6 Mon Sep 17 00:00:00 2001 From: luccabb Date: Wed, 6 Mar 2024 01:01:00 -0800 Subject: [PATCH 3/3] add print --- tests/test_backoff.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_backoff.py b/tests/test_backoff.py index 8d58daa..9556cd0 100644 --- a/tests/test_backoff.py +++ b/tests/test_backoff.py @@ -568,8 +568,8 @@ def success(*args, **kwargs): for i in range(2): details = backoffs[i] - elapsed = details.pop('elapsed') print(details) + elapsed = details.pop('elapsed') assert isinstance(elapsed, float) assert details == {'args': (1, 2, 3), 'kwargs': {'foo': 1, 'bar': 2},