Skip to content

Commit

Permalink
cleanup and testcase for retry
Browse files Browse the repository at this point in the history
  • Loading branch information
leifj committed Sep 8, 2014
1 parent 8268e81 commit 513deef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/pyff/decorators.py
Expand Up @@ -10,15 +10,15 @@
from pyff.logs import log


def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=log):
def retry(ex, tries=4, delay=3, backoff=2, logger=log):
"""Retry calling the decorated function using exponential backoff based on
* http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
* http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
:param ExceptionToCheck: the exception to check. may be a tuple of
:param ex: the exception to check. may be a tuple of
excpetions to check
:type ExceptionToCheck: Exception or tuple
:type ex: Exception or tuple
:param tries: number of times to try (not retry) before giving up
:type tries: int
:param delay: initial delay between retries in seconds
Expand All @@ -37,7 +37,7 @@ def f_retry(*args, **kwargs):
while mtries > 1:
try:
return f(*args, **kwargs)
except ExceptionToCheck, e:
except ex, e:
msg = "%s, Retrying in %d seconds..." % (str(e), mdelay)
if log:
log.warn(msg)
Expand Down
27 changes: 27 additions & 0 deletions src/pyff/test/test_decorators.py
@@ -0,0 +1,27 @@
from unittest import TestCase
from pyff.decorators import retry


class TestRetry(TestCase):

def test_retry_nop(self):
status = [False]

@retry(None, delay=1, backoff=1)
def runs_ok():
status[0] = True
runs_ok()
assert(status[0])

def test_retry_fail(self):
status = [False]
@retry(ValueError, delay=1, backoff=1)
def fails():
raise ValueError("nope")

try:
fails()
assert False
except ValueError, ex:
pass
assert(not status[0])

0 comments on commit 513deef

Please sign in to comment.