[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:
parent
72b37e2bbd
commit
ea167ad7ab
|
@ -199,7 +199,7 @@ class Registry(Mapping):
|
||||||
def leave_test_mode(self):
|
def leave_test_mode(self):
|
||||||
""" Leave the test mode. """
|
""" Leave the test mode. """
|
||||||
assert self.test_cr is not None
|
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
|
self.test_cr = None
|
||||||
RegistryManager.leave_test_mode()
|
RegistryManager.leave_test_mode()
|
||||||
|
|
||||||
|
@ -207,13 +207,14 @@ class Registry(Mapping):
|
||||||
""" Return a new cursor for the database. The cursor itself may be used
|
""" Return a new cursor for the database. The cursor itself may be used
|
||||||
as a context manager to commit/rollback and close automatically.
|
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
|
# While in test mode, we use one special cursor across requests. The
|
||||||
# test cursor uses a reentrant lock to serialize accesses. The lock
|
# test cursor uses a reentrant lock to serialize accesses. The lock
|
||||||
# is granted here by cursor(), and automatically released by the
|
# is granted here by cursor(), and automatically released by the
|
||||||
# cursor itself in its method close().
|
# cursor itself in its method close().
|
||||||
self.test_cr.acquire()
|
cr.acquire()
|
||||||
return self.test_cr
|
return cr
|
||||||
return self._db.cursor()
|
return self._db.cursor()
|
||||||
|
|
||||||
class DummyRLock(object):
|
class DummyRLock(object):
|
||||||
|
|
|
@ -399,10 +399,11 @@ class TestCursor(Cursor):
|
||||||
def release(self):
|
def release(self):
|
||||||
self._lock.release()
|
self._lock.release()
|
||||||
|
|
||||||
def close(self, force=False):
|
def force_close(self):
|
||||||
if force:
|
|
||||||
super(TestCursor, self).close()
|
super(TestCursor, self).close()
|
||||||
elif not self._closed:
|
|
||||||
|
def close(self):
|
||||||
|
if not self._closed:
|
||||||
self.rollback() # for stuff that has not been committed
|
self.rollback() # for stuff that has not been committed
|
||||||
self.release()
|
self.release()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue