Skip to content

Commit

Permalink
fix issue where multiple calls to same func fail
Browse files Browse the repository at this point in the history
  • Loading branch information
angstwad committed Jun 4, 2018
1 parent a246137 commit 2464f1a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
14 changes: 14 additions & 0 deletions tests/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ def func():
assert mock.call_count is 3


def test_multiple_calls():
local = {'runs': 0}

@retry
def foo():
local['runs'] += 1
if local['runs'] % 2 == 0:
return True
raise ValueError('test')

assert foo()
assert foo()


def test_retry_exp_backoff(mocker):
mocker.patch('trythatagain.waiters.time')

Expand Down
2 changes: 1 addition & 1 deletion trythatagain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
MILLISECONDS, SECONDS, retry, retry_exp_backoff, retry_linear_backoff
)

__version__ = '0.1.2'
__version__ = '0.1.3'
23 changes: 15 additions & 8 deletions trythatagain/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ def retry(func=None, self=None, retries=3, raise_for=None, reraise=True,
retries; requires `wait_func`
"""
local = {
"tries": 0,
"exc": None
}

if raise_for is None:
raise_for = NoneType

Expand All @@ -43,15 +38,27 @@ def retry(func=None, self=None, retries=3, raise_for=None, reraise=True,

@wraps(func)
def wrapped(*args, **kwargs):
local = {
"tries": 0,
"exc": None
}

def reset():
local['tries'] = 0
local['exc'] = None

while local['tries'] < retries or retries == 0:
local['tries'] += 1
try:
if self is not None:
return func(self, *args, **kwargs)
retval = func(self, *args, **kwargs)
else:
return func(*args, **kwargs)
retval = func(*args, **kwargs)
reset()
return retval
except Exception as e:
local['exc'] = e
if local['exc'] is None:
local['exc'] = e

try:
if isinstance(e, raise_for):
Expand Down

0 comments on commit 2464f1a

Please sign in to comment.