Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in utils.retry where delay is shared between calls #774

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 20,6 @@ jobs:
matrix:
python-version:
- 3.9
- 2.7

steps:
- name: checkout
Expand All @@ -47,7 46,6 @@ jobs:
matrix:
python-version:
- 3.9
- 2.7
steps:
- name: checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -83,7 81,6 @@ jobs:
- 3.9
- 3.8
- 3.7
- 2.7
dependency-set:
- pinned
- latest
Expand Down
4 changes: 1 addition & 3 deletions nameko/utils/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 34,9 @@ def retry(
if max_attempts is None:
max_attempts = float('inf')

retry_delay = RetryDelay(delay, backoff, max_delay)

@wrapt.decorator
def wrapper(wrapped, instance, args, kwargs):

retry_delay = RetryDelay(delay, backoff, max_delay)
counter = itertools.count()

while True:
Expand Down
28 changes: 28 additions & 0 deletions test/utils/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 139,31 @@ def fn():

total_delay = mock_sleep.total()
assert total_delay == delay * max_attempts

def test_multiple_calls(self, tracker, mock_sleep):
# Make sure that delay behaves the same way if called multiple times.
# Previously a bug caused the delay to increase with subsequent calls
max_attempts = 2
delay = 5
backoff = 2

@retry(max_attempts=max_attempts, delay=delay, backoff=backoff)
def fn():
tracker()
raise ValueError()

with pytest.raises(ValueError):
fn()

expected_delay = sum(
delay * (backoff ** attempt)
for attempt in range(1, max_attempts 1)
)
total_delay = mock_sleep.total()
assert total_delay == expected_delay

with pytest.raises(ValueError):
fn()

total_delay = mock_sleep.total()
assert total_delay == 2 * expected_delay