Skip to content

Commit

Permalink
Fix #19 attempts didn't stop on success
Browse files Browse the repository at this point in the history
Uses fix from https://github.com/zopefoundation/transaction/pull/20/files
but different test changes.
  • Loading branch information
Jim Fulton committed Nov 7, 2016
1 parent 3bb84c7 commit 5a2351e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
15 changes: 8 additions & 7 deletions docs/convenience.rst
Expand Up @@ -125,7 +125,7 @@ Of course, other errors are propagated directly:
>>> for attempt in transaction.manager.attempts():
... with attempt:
... ntry += 1
... if ntry == 3:
... if ntry % 3:
... raise ValueError(ntry)
Traceback (most recent call last):
...
Expand All @@ -135,6 +135,7 @@ We can use the default transaction manager:

.. doctest::

>>> ntry = 0
>>> for attempt in transaction.attempts():
... with attempt as t:
... t.note('test')
Expand All @@ -143,9 +144,9 @@ We can use the default transaction manager:
... dm['ntry'] = ntry
... if ntry % 3:
... raise Retry(ntry)
3 3
3 4
3 5
3 0
3 1
3 2

Sometimes, a data manager doesn't raise exceptions directly, but
wraps other other systems that raise exceptions outside of it's
Expand All @@ -172,9 +173,9 @@ attempted again.
... dm2['ntry'] = ntry
... if ntry % 3:
... raise ValueError('we really should retry this')
6 0
6 1
6 2
3 0
3 1
3 2

>>> dm2['ntry']
3
9 changes: 8 additions & 1 deletion transaction/_manager.py
Expand Up @@ -144,7 +144,10 @@ def attempts(self, number=3):
while number:
number -= 1
if number:
yield Attempt(self)
attempt = Attempt(self)
yield attempt
if attempt.sucess:
break
else:
yield self

Expand All @@ -167,6 +170,8 @@ class ThreadTransactionManager(TransactionManager, threading.local):

class Attempt(object):

sucess = False

def __init__(self, manager):
self.manager = manager

Expand All @@ -186,5 +191,7 @@ def __exit__(self, t, v, tb):
self.manager.commit()
except:
return self._retry_or_raise(*sys.exc_info())
else:
self.sucess = True
else:
return self._retry_or_raise(t, v, tb)
10 changes: 10 additions & 0 deletions transaction/tests/test__manager.py
Expand Up @@ -236,6 +236,16 @@ def test_attempts_w_valid_count(self):
self.assertEqual(len(found), 1)
self.assertTrue(found[0] is tm)

def test_attempts_stop_on_success(self):
tm = self._makeOne()

i = 0
for attempt in tm.attempts():
with attempt:
i += 1

self.assertEqual(i, 1)

def test_attempts_w_default_count(self):
from transaction._manager import Attempt
tm = self._makeOne()
Expand Down

0 comments on commit 5a2351e

Please sign in to comment.