Skip to content

Commit

Permalink
Don't retry on success when deactivated
Browse files Browse the repository at this point in the history
Fixes #53
  • Loading branch information
hynek committed Jan 3, 2024
1 parent fc57086 commit 844639a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/

## [Unreleased](https://github.com/hynek/stamina/compare/23.3.0...HEAD)

### Fixed

- Blocks used to be retried when *stamina* was set inactive when they succeeded – and only then.


## [23.3.0](https://github.com/hynek/stamina/compare/23.2.0...23.3.0) - 2023-12-05

Expand Down
6 changes: 3 additions & 3 deletions src/stamina/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ def with_name(

def __iter__(self) -> Iterator[Attempt]:
if not CONFIG.is_active:
for r in _t.Retrying(
reraise=True, stop=_STOP_NO_RETRY
): # pragma: no cover -- it's always once + GeneratorExit
for r in _t.Retrying(reraise=True, stop=_STOP_NO_RETRY):
yield Attempt(r)

return

for r in _t.Retrying(
before_sleep=_make_before_sleep(
self._name, CONFIG, self._args, self._kw
Expand Down
32 changes: 32 additions & 0 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ async def f():
assert 1 == num_called


async def test_retry_inactive_block():
"""
If inactive, don't retry.
"""
num_called = 0

stamina.set_active(False)

with pytest.raises(Exception, match="passed"): # noqa: PT012
async for attempt in stamina.retry_context(on=ValueError):
with attempt:
num_called += 1
raise Exception("passed")

assert 1 == num_called


async def test_retry_inactive_ok():
"""
If inactive, the happy path still works.
Expand All @@ -145,6 +162,21 @@ async def f():
assert 1 == num_called


async def test_retry_inactive_block_ok():
"""
If inactive, the happy path still works.
"""
num_called = 0

stamina.set_active(False)

async for attempt in stamina.retry_context(on=ValueError):
with attempt:
num_called += 1

assert 1 == num_called


async def test_retry_block():
"""
Async retry_context blocks are retried.
Expand Down
32 changes: 32 additions & 0 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ def f():
assert 1 == num_called


def test_retry_inactive_block():
"""
If inactive, don't retry.
"""
num_called = 0

stamina.set_active(False)

with pytest.raises(Exception, match="passed"): # noqa: PT012
for attempt in stamina.retry_context(on=ValueError):
with attempt:
num_called += 1
raise Exception("passed")

assert 1 == num_called


def test_retry_inactive_ok():
"""
If inactive, the happy path still works.
Expand All @@ -115,6 +132,21 @@ def f():
assert 1 == num_called


def test_retry_inactive_block_ok():
"""
If inactive, the happy path still works.
"""
num_called = 0

stamina.set_active(False)

for attempt in stamina.retry_context(on=ValueError):
with attempt:
num_called += 1

assert 1 == num_called


def test_retry_block():
"""
Sync retry_context blocks are retried.
Expand Down

0 comments on commit 844639a

Please sign in to comment.