mirror of
https://github.com/simon987/hexlib.git
synced 2025-04-18 01:16:43 +00:00
Fix @retry
This commit is contained in:
parent
58d150279f
commit
f914759b71
@ -11,13 +11,16 @@ last_time_called = dict()
|
|||||||
|
|
||||||
def retry(attempts, callback=None):
|
def retry(attempts, callback=None):
|
||||||
def decorate(func):
|
def decorate(func):
|
||||||
retries = attempts
|
def wrapper(*args, **kwargs):
|
||||||
while retries > 0:
|
retries = attempts
|
||||||
try:
|
while retries > 0:
|
||||||
func()
|
try:
|
||||||
except Exception as e:
|
return func(*args, **kwargs)
|
||||||
if callback:
|
except Exception as e:
|
||||||
callback(e)
|
if callback:
|
||||||
|
callback(e)
|
||||||
|
retries -= 1
|
||||||
|
return wrapper
|
||||||
return decorate
|
return decorate
|
||||||
|
|
||||||
|
|
||||||
|
2
setup.py
2
setup.py
@ -2,7 +2,7 @@ from setuptools import setup
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="hexlib",
|
name="hexlib",
|
||||||
version="1.22",
|
version="1.23",
|
||||||
description="Misc utility methods",
|
description="Misc utility methods",
|
||||||
author="simon987",
|
author="simon987",
|
||||||
author_email="me@simon987.net",
|
author_email="me@simon987.net",
|
||||||
|
27
test/test_retry.py
Normal file
27
test/test_retry.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
from unittest import TestCase
|
||||||
|
from hexlib.db import VolatileState, VolatileBooleanState
|
||||||
|
from hexlib.misc import retry
|
||||||
|
|
||||||
|
|
||||||
|
class TestRetry(TestCase):
|
||||||
|
|
||||||
|
def test_simple(self):
|
||||||
|
@retry(attempts=3)
|
||||||
|
def a(i):
|
||||||
|
return i + 1
|
||||||
|
|
||||||
|
self.assertEqual(a(1), 2)
|
||||||
|
|
||||||
|
def test_error(self):
|
||||||
|
arr = []
|
||||||
|
|
||||||
|
def cb(e):
|
||||||
|
arr.append(e)
|
||||||
|
|
||||||
|
@retry(attempts=3, callback=cb)
|
||||||
|
def a(i):
|
||||||
|
raise Exception("err")
|
||||||
|
|
||||||
|
a(1)
|
||||||
|
|
||||||
|
self.assertEqual(3, len(arr))
|
Loading…
x
Reference in New Issue
Block a user