From 87b5eedb722a46e8d3630ea9a0f872a076b8c59a Mon Sep 17 00:00:00 2001 From: Federico Ressi Date: Thu, 8 Jul 2021 14:17:54 +0200 Subject: [PATCH] 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 --- tobiko/common/_retry.py | 18 +++++++++++++----- tobiko/tests/unit/test_retry.py | 12 ++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/tobiko/common/_retry.py b/tobiko/common/_retry.py index 525b6133a..ef99d0bb4 100644 --- a/tobiko/common/_retry.py +++ b/tobiko/common/_retry.py @@ -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: diff --git a/tobiko/tests/unit/test_retry.py b/tobiko/tests/unit/test_retry.py index 321a8152c..7565e0a12 100644 --- a/tobiko/tests/unit/test_retry.py +++ b/tobiko/tests/unit/test_retry.py @@ -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)