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:
parent
8f9080a986
commit
87b5eedb72
@ -104,8 +104,7 @@ class RetryAttempt(object):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def details(self) -> str:
|
def details(self) -> str:
|
||||||
details = []
|
details = [f"number={self.number}"]
|
||||||
details.append(f"number={self.number}")
|
|
||||||
if self.count is not None:
|
if self.count is not None:
|
||||||
details.append(f"count={self.count}")
|
details.append(f"count={self.count}")
|
||||||
details.append(f"elapsed_time={self.elapsed_time}")
|
details.append(f"elapsed_time={self.elapsed_time}")
|
||||||
@ -115,13 +114,22 @@ class RetryAttempt(object):
|
|||||||
details.append(f"interval={self.interval}")
|
details.append(f"interval={self.interval}")
|
||||||
return ', '.join(details)
|
return ', '.join(details)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_last(self):
|
||||||
|
try:
|
||||||
|
self.check_limits()
|
||||||
|
except RetryLimitError:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"retry_attempt({self.details})"
|
return f"retry_attempt({self.details})"
|
||||||
|
|
||||||
|
|
||||||
def retry_attempt(number: int,
|
def retry_attempt(number: int = 0,
|
||||||
start_time: float,
|
start_time: float = 0.,
|
||||||
elapsed_time: float,
|
elapsed_time: float = 0.,
|
||||||
count: typing.Optional[int] = None,
|
count: typing.Optional[int] = None,
|
||||||
timeout: _time.Seconds = None,
|
timeout: _time.Seconds = None,
|
||||||
interval: _time.Seconds = None) -> RetryAttempt:
|
interval: _time.Seconds = None) -> RetryAttempt:
|
||||||
|
@ -358,3 +358,15 @@ class RetryTest(unit.TobikoUnitTest):
|
|||||||
self.assertEqual([], result.errors)
|
self.assertEqual([], result.errors)
|
||||||
self.assertEqual({"Not the right day!": [test_case]},
|
self.assertEqual({"Not the right day!": [test_case]},
|
||||||
result.skip_reasons)
|
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user