[IMP] test cursor: code cleanup to make it more robust to concurrent accesses

bzr revid: rco@openerp.com-20140414075906-2d0i4qhe4x7czsao
This commit is contained in:
Raphael Collet 2014-04-14 09:59:06 +02:00
parent 72b37e2bbd
commit ea167ad7ab
2 changed files with 11 additions and 9 deletions

View File

@ -199,7 +199,7 @@ class Registry(Mapping):
def leave_test_mode(self):
""" Leave the test mode. """
assert self.test_cr is not None
self.test_cr.close(force=True) # close the cursor for real
self.test_cr.force_close()
self.test_cr = None
RegistryManager.leave_test_mode()
@ -207,13 +207,14 @@ class Registry(Mapping):
""" Return a new cursor for the database. The cursor itself may be used
as a context manager to commit/rollback and close automatically.
"""
if self.test_cr is not None:
cr = self.test_cr
if cr is not None:
# While in test mode, we use one special cursor across requests. The
# test cursor uses a reentrant lock to serialize accesses. The lock
# is granted here by cursor(), and automatically released by the
# cursor itself in its method close().
self.test_cr.acquire()
return self.test_cr
cr.acquire()
return cr
return self._db.cursor()
class DummyRLock(object):

View File

@ -399,12 +399,13 @@ class TestCursor(Cursor):
def release(self):
self._lock.release()
def close(self, force=False):
if force:
super(TestCursor, self).close()
elif not self._closed:
def force_close(self):
super(TestCursor, self).close()
def close(self):
if not self._closed:
self.rollback() # for stuff that has not been committed
self.release()
self.release()
def autocommit(self, on):
_logger.debug("TestCursor.autocommit(%r) does nothing", on)