Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monotonic time #216

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions backoff/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions backoff/_sync.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# coding:utf-8
import datetime
import functools
import time
from datetime import timedelta
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
28 changes: 14 additions & 14 deletions tests/test_backoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down