Skip to content

Commit

Permalink
Test retry logic, fix an off-by-one bug, declare number of retry atte…
Browse files Browse the repository at this point in the history
…mpts.
  • Loading branch information
ieure committed Jan 27, 2010
1 parent 4fb3f7d commit ff11274
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lazyboy/connection.py
Expand Up @@ -28,11 +28,11 @@

_SERVERS = {}
_CLIENTS = {}

RETRY_ATTEMPTS = 5

def _retry_default_callback(attempt, exc_):
"""Retry an attempt five times, then give up."""
return attempt < 5
return attempt < RETRY_ATTEMPTS


def retry(callback=None):
Expand All @@ -44,7 +44,7 @@ def retry(callback=None):
def __closure__(func):

def __inner__(*args, **kwargs):
attempt = 0
attempt = 1
while True:
try:
return func(*args, **kwargs)
Expand Down
23 changes: 23 additions & 0 deletions lazyboy/tests/test_connection.py
Expand Up @@ -221,5 +221,28 @@ def test_get_client(self):
self.assert_(exc.args[0] != "")


class TestRetry(unittest.TestCase):

"""Test retry logic."""

def test_retry_default_callback(self):
"""Make sure retry_default_callback works."""
for x in range(conn.RETRY_ATTEMPTS):
self.assert_(conn._retry_default_callback(x, None))

self.assert_(not conn._retry_default_callback(x + 1, None))

def test_retry(self):
"""Test retry."""
retries = []
def bad_func():
retries.append(True)
raise Exception("Whoops.")

retry_func = conn.retry()(bad_func)
self.assertRaises(Exception, retry_func)
self.assert_(len(retries) == conn.RETRY_ATTEMPTS)


if __name__ == '__main__':
unittest.main()

0 comments on commit ff11274

Please sign in to comment.