Add retry attempt property to check if it is the last aptempt

implement RetryAttempt.is_last property to check if
  RetryAttempt.check_limits() would raise a RetryLimitError

Change-Id: I92502fba58fac64d796280333da67c746bf6f417
This commit is contained in:
Federico Ressi 2021-07-08 14:17:54 +02:00
parent 8f9080a986
commit 87b5eedb72
2 changed files with 25 additions and 5 deletions

View File

@ -104,8 +104,7 @@ class RetryAttempt(object):
@property
def details(self) -> str:
details = []
details.append(f"number={self.number}")
details = [f"number={self.number}"]
if self.count is not None:
details.append(f"count={self.count}")
details.append(f"elapsed_time={self.elapsed_time}")
@ -115,13 +114,22 @@ class RetryAttempt(object):
details.append(f"interval={self.interval}")
return ', '.join(details)
@property
def is_last(self):
try:
self.check_limits()
except RetryLimitError:
return True
else:
return False
def __repr__(self):
return f"retry_attempt({self.details})"
def retry_attempt(number: int,
start_time: float,
elapsed_time: float,
def retry_attempt(number: int = 0,
start_time: float = 0.,
elapsed_time: float = 0.,
count: typing.Optional[int] = None,
timeout: _time.Seconds = None,
interval: _time.Seconds = None) -> RetryAttempt:

View File

@ -358,3 +358,15 @@ class RetryTest(unit.TobikoUnitTest):
self.assertEqual([], result.errors)
self.assertEqual({"Not the right day!": [test_case]},
result.skip_reasons)
def test_attempt_is_last_with_count(self):
self.assertFalse(tobiko.retry_attempt(number=2,
count=3).is_last)
self.assertTrue(tobiko.retry_attempt(number=2,
count=2).is_last)
def test_attempt_is_last_with_timeout(self):
self.assertFalse(tobiko.retry_attempt(elapsed_time=2.,
timeout=3.).is_last)
self.assertTrue(tobiko.retry_attempt(elapsed_time=2.,
timeout=2.).is_last)