Skip to content
Snippets Groups Projects
Commit bb0b011a authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

new features for ExponentialBackoff

parent f92ec3c6
No related branches found
No related tags found
No related merge requests found
......@@ -421,13 +421,23 @@ class ExponentialBackoff:
return self.unavailable_until - t
@property
def available(self) -> bool:
def ready_for_next_check(self) -> bool:
"""
:return: Is the service healthy, ie. last time that instance was informed a
:meth:`~satella.time.ExponentialBackoff.success` was called rather
than :meth::meth:`~satella.time.ExponentialBackoff.failure`?
:return: Has :meth:`~satella.time.ExponentialBackoff.failure` been called only
in the last waiting period?
"""
return self.unavailable_until is None
if self.unavailable_until is None:
return True
elif time.monotonic() > self.unavailable_until:
self.unavailable_until = None
return True
else:
return False
@property
def available(self) -> bool:
"""Was the status of the last call :code:`success`?"""
return self.counter == 0
def success(self):
"""
......
......@@ -21,6 +21,7 @@ class TestTime(unittest.TestCase):
with measure() as m:
eb.wait_until_available(2.5)
self.assertTrue(eb.available)
self.assertLessEqual(m(), 1.5)
eb.failed()
do_test()
......@@ -32,10 +33,13 @@ class TestTime(unittest.TestCase):
eb.failed()
self.assertFalse(eb.available)
self.assertFalse(eb.ready_for_next_check)
with measure() as m:
eb.wait_until_available()
self.assertTrue(eb.ready_for_next_check)
self.assertGreaterEqual(m(), 2)
eb.success()
self.assertTrue(eb.ready_for_next_check)
self.assertTrue(eb.available)
eb.failed()
self.assertFalse(eb.available)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment