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: ...@@ -125,7 +125,7 @@ Of course, other errors are propagated directly:
>>> for attempt in transaction.manager.attempts(): >>> for attempt in transaction.manager.attempts():
... with attempt: ... with attempt:
... ntry += 1 ... ntry += 1
... if ntry == 3: ... if ntry % 3:
... raise ValueError(ntry) ... raise ValueError(ntry)
Traceback (most recent call last): Traceback (most recent call last):
... ...
...@@ -135,6 +135,7 @@ We can use the default transaction manager: ...@@ -135,6 +135,7 @@ We can use the default transaction manager:
.. doctest:: .. doctest::
>>> ntry = 0
>>> for attempt in transaction.attempts(): >>> for attempt in transaction.attempts():
... with attempt as t: ... with attempt as t:
... t.note('test') ... t.note('test')
...@@ -143,9 +144,9 @@ We can use the default transaction manager: ...@@ -143,9 +144,9 @@ We can use the default transaction manager:
... dm['ntry'] = ntry ... dm['ntry'] = ntry
... if ntry % 3: ... if ntry % 3:
... raise Retry(ntry) ... raise Retry(ntry)
3 3 3 0
3 4 3 1
3 5 3 2
Sometimes, a data manager doesn't raise exceptions directly, but Sometimes, a data manager doesn't raise exceptions directly, but
wraps other other systems that raise exceptions outside of it's wraps other other systems that raise exceptions outside of it's
...@@ -172,9 +173,9 @@ attempted again. ...@@ -172,9 +173,9 @@ attempted again.
... dm2['ntry'] = ntry ... dm2['ntry'] = ntry
... if ntry % 3: ... if ntry % 3:
... raise ValueError('we really should retry this') ... raise ValueError('we really should retry this')
6 0 3 0
6 1 3 1
6 2 3 2
>>> dm2['ntry'] >>> dm2['ntry']
3 3
...@@ -144,7 +144,10 @@ class TransactionManager(object): ...@@ -144,7 +144,10 @@ class TransactionManager(object):
while number: while number:
number -= 1 number -= 1
if number: if number:
yield Attempt(self) attempt = Attempt(self)
yield attempt
if attempt.sucess:
break
else: else:
yield self yield self
...@@ -167,6 +170,8 @@ class ThreadTransactionManager(TransactionManager, threading.local): ...@@ -167,6 +170,8 @@ class ThreadTransactionManager(TransactionManager, threading.local):
class Attempt(object): class Attempt(object):
sucess = False
def __init__(self, manager): def __init__(self, manager):
self.manager = manager self.manager = manager
...@@ -186,5 +191,7 @@ class Attempt(object): ...@@ -186,5 +191,7 @@ class Attempt(object):
self.manager.commit() self.manager.commit()
except: except:
return self._retry_or_raise(*sys.exc_info()) return self._retry_or_raise(*sys.exc_info())
else:
self.sucess = True
else: else:
return self._retry_or_raise(t, v, tb) return self._retry_or_raise(t, v, tb)
...@@ -236,6 +236,16 @@ class TransactionManagerTests(unittest.TestCase): ...@@ -236,6 +236,16 @@ class TransactionManagerTests(unittest.TestCase):
self.assertEqual(len(found), 1) self.assertEqual(len(found), 1)
self.assertTrue(found[0] is tm) 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): def test_attempts_w_default_count(self):
from transaction._manager import Attempt from transaction._manager import Attempt
tm = self._makeOne() 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