Skip to content

Commit

Permalink
Handle negative attempts and delay
Browse files Browse the repository at this point in the history
If test author try to use attempts or delay with a value less than 1,
ValueError is raised.
  • Loading branch information
pieqq committed Sep 11, 2024
1 parent 2937c82 commit 73481d7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions checkbox-support/checkbox_support/helpers/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ def run_with_retry(f, max_attempts, delay, *args, **kwargs):
"""
initial_delay = 1
backoff_factor = 2
if max_attempts < 1:
raise ValueError(
"max_attempts should be at least 1 ({} was used)".format(
max_attempts
)
)
if delay < 1:
raise ValueError(
"delay should be at least 1 ({} was used)".format(delay)
)
for attempt in range(1, max_attempts + 1):
attempt_string = "Attempt {}/{}".format(attempt, max_attempts)
print()
Expand Down
16 changes: 16 additions & 0 deletions checkbox-support/checkbox_support/tests/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ def f():
self.assertIn("Attempt 7 failed", mock_stdout.getvalue())
self.assertNotIn("Attempt 8 failed", mock_stdout.getvalue())

def test_decorator_wrong_max_attempts(self):
@retry(-1, 10)
def f():
return 1 / 0

with self.assertRaises(ValueError):
f()

def test_decorator_wrong_delay(self):
@retry(2, -1)
def f():
return 1 / 0

with self.assertRaises(ValueError):
f()

def test_identity(self):
def k(*args, **kwargs):
return (args, kwargs)
Expand Down

0 comments on commit 73481d7

Please sign in to comment.