Skip to content

Commit

Permalink
Get max_tries/max_time values for every call fixes #160
Browse files Browse the repository at this point in the history
  • Loading branch information
jvrsantacruz committed Jun 7, 2022
1 parent 92e7b6e commit 732eaa3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
22 changes: 8 additions & 14 deletions backoff/_sync.py
Expand Up @@ -28,11 +28,8 @@ def retry_predicate(target, wait_gen, predicate,

@functools.wraps(target)
def retry(*args, **kwargs):

# update variables from outer function args
nonlocal max_tries, max_time
max_tries = _maybe_call(max_tries)
max_time = _maybe_call(max_time)
max_tries_value = _maybe_call(max_tries)
max_time_value = _maybe_call(max_time)

tries = 0
start = datetime.datetime.now()
Expand All @@ -50,9 +47,9 @@ def retry(*args, **kwargs):

ret = target(*args, **kwargs)
if predicate(ret):
max_tries_exceeded = (tries == max_tries)
max_tries_exceeded = (tries == max_tries_value)
max_time_exceeded = (max_time is not None and
elapsed >= max_time)
elapsed >= max_time_value)

if max_tries_exceeded or max_time_exceeded:
_call_handlers(on_giveup, **details, value=ret)
Expand Down Expand Up @@ -86,11 +83,8 @@ def retry_exception(target, wait_gen, exception,

@functools.wraps(target)
def retry(*args, **kwargs):

# update variables from outer function args
nonlocal max_tries, max_time
max_tries = _maybe_call(max_tries)
max_time = _maybe_call(max_time)
max_tries_value = _maybe_call(max_tries)
max_time_value = _maybe_call(max_time)

tries = 0
start = datetime.datetime.now()
Expand All @@ -109,9 +103,9 @@ def retry(*args, **kwargs):
try:
ret = target(*args, **kwargs)
except exception as e:
max_tries_exceeded = (tries == max_tries)
max_tries_exceeded = (tries == max_tries_value)
max_time_exceeded = (max_time is not None and
elapsed >= max_time)
elapsed >= max_time_value)

if giveup(e) or max_tries_exceeded or max_time_exceeded:
_call_handlers(on_giveup, **details)
Expand Down
23 changes: 23 additions & 0 deletions tests/test_backoff.py
Expand Up @@ -548,6 +548,29 @@ def exceptor():
assert len(log) == 3


def test_on_exception_callable_max_tries_reads_every_time(monkeypatch):
monkeypatch.setattr('time.sleep', lambda x: None)

lookups = []
def lookup_max_tries():
lookups.append(True)
return 3

@backoff.on_exception(backoff.constant,
ValueError,
max_tries=lookup_max_tries)
def exceptor():
raise ValueError()

with pytest.raises(ValueError):
exceptor()

with pytest.raises(ValueError):
exceptor()

assert len(lookups) == 2


def test_on_exception_callable_gen_kwargs():

def lookup_foo():
Expand Down
25 changes: 25 additions & 0 deletions tests/test_backoff_async.py
Expand Up @@ -571,6 +571,31 @@ async def exceptor():
assert len(log) == 3


@pytest.mark.asyncio
async def test_on_exception_callable_max_tries_reads_every_time(monkeypatch):
monkeypatch.setattr('asyncio.sleep', _await_none)

lookups = []
def lookup_max_tries():
lookups.append(True)
return 3

@backoff.on_exception(backoff.constant,
ValueError,
max_tries=lookup_max_tries)
def exceptor():
raise ValueError()

with pytest.raises(ValueError):
exceptor()

with pytest.raises(ValueError):
exceptor()

assert len(lookups) == 2



@pytest.mark.asyncio
async def test_on_exception_callable_gen_kwargs():

Expand Down

0 comments on commit 732eaa3

Please sign in to comment.