Skip to content

Commit

Permalink
fix: update utils.exponential_backoff to broker in ints only
Browse files Browse the repository at this point in the history
Python 3.12 finally made good on their promise to start throwing
errors when trying to use `random` with non-ints, so make a good-faith
effort to do that in all versions.

Remove a now-useless test for passing floats that should now be
impossible to hit.

Partial fix for Issue #29
  • Loading branch information
nisimond committed Feb 28, 2024
1 parent d16c80d commit 95b860c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 5 deletions.
4 changes: 2 additions & 2 deletions spinach/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def exponential_backoff(attempt: int, cap: int=1200) -> timedelta:
:arg cap: maximum delay, defaults to 20 minutes
"""
base = 3
temp = min(base * 2 ** attempt, cap)
return timedelta(seconds=temp / 2 + random.randint(0, temp / 2))
temp = min(base * 2 ** attempt, cap) // 2
return timedelta(seconds=temp + random.randint(0, temp))


@contextlib.contextmanager
Expand Down
3 changes: 0 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ def func():


def test_exponential_backoff():
with pytest.raises(ValueError):
utils.exponential_backoff(0)

assert (
timedelta(seconds=3) <= utils.exponential_backoff(1)
<= timedelta(seconds=6)
Expand Down

0 comments on commit 95b860c

Please sign in to comment.