Commit 5a2351e2 authored by Jim Fulton's avatar Jim Fulton

Fix #19 attempts didn't stop on success

Uses fix from https://github.com/zopefoundation/transaction/pull/20/files
but different test changes.
parent 3bb84c7b
......@@ -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):
...
......@@ -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')
......@@ -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
......@@ -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
......@@ -144,7 +144,10 @@ class TransactionManager(object):
while number:
number -= 1
if number:
yield Attempt(self)
attempt = Attempt(self)
yield attempt
if attempt.sucess:
break
else:
yield self
......@@ -167,6 +170,8 @@ class ThreadTransactionManager(TransactionManager, threading.local):
class Attempt(object):
sucess = False
def __init__(self, manager):
self.manager = manager
......@@ -186,5 +191,7 @@ class Attempt(object):
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)
......@@ -236,6 +236,16 @@ class TransactionManagerTests(unittest.TestCase):
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()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment