2002-11-19 13:47:07 -04:00
|
|
|
"""
|
|
|
|
TestCases for testing the locking sub-system.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
import unittest
|
2008-08-31 11:00:51 -03:00
|
|
|
from test_all import db, test_support, verbose, have_threads, \
|
|
|
|
get_new_environment_path, get_new_database_path
|
2008-07-23 08:38:42 -03:00
|
|
|
|
|
|
|
if have_threads :
|
2008-08-31 11:00:51 -03:00
|
|
|
from threading import Thread
|
|
|
|
import sys
|
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
from threading import currentThread
|
|
|
|
else :
|
|
|
|
from threading import current_thread as currentThread
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class LockingTestCase(unittest.TestCase):
|
2008-05-13 17:57:59 -03:00
|
|
|
import sys
|
|
|
|
if sys.version_info[:3] < (2, 4, 0):
|
|
|
|
def assertTrue(self, expr, msg=None):
|
|
|
|
self.failUnless(expr,msg=msg)
|
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
def setUp(self):
|
2008-05-13 17:57:59 -03:00
|
|
|
self.homeDir = get_new_environment_path()
|
2002-11-19 13:47:07 -04:00
|
|
|
self.env = db.DBEnv()
|
2007-11-04 22:56:31 -04:00
|
|
|
self.env.open(self.homeDir, db.DB_THREAD | db.DB_INIT_MPOOL |
|
|
|
|
db.DB_INIT_LOCK | db.DB_CREATE)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.env.close()
|
2008-02-24 14:47:03 -04:00
|
|
|
test_support.rmtree(self.homeDir)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test01_simple(self):
|
|
|
|
if verbose:
|
|
|
|
print '\n', '-=' * 30
|
|
|
|
print "Running %s.test01_simple..." % self.__class__.__name__
|
|
|
|
|
|
|
|
anID = self.env.lock_id()
|
|
|
|
if verbose:
|
|
|
|
print "locker ID: %s" % anID
|
|
|
|
lock = self.env.lock_get(anID, "some locked thing", db.DB_LOCK_WRITE)
|
|
|
|
if verbose:
|
|
|
|
print "Aquired lock: %s" % lock
|
|
|
|
self.env.lock_put(lock)
|
|
|
|
if verbose:
|
|
|
|
print "Released lock: %s" % lock
|
2008-07-23 08:38:42 -03:00
|
|
|
self.env.lock_id_free(anID)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test02_threaded(self):
|
|
|
|
if verbose:
|
|
|
|
print '\n', '-=' * 30
|
|
|
|
print "Running %s.test02_threaded..." % self.__class__.__name__
|
|
|
|
|
|
|
|
threads = []
|
2002-12-30 16:53:52 -04:00
|
|
|
threads.append(Thread(target = self.theThread,
|
2008-05-13 17:57:59 -03:00
|
|
|
args=(db.DB_LOCK_WRITE,)))
|
2002-12-30 16:53:52 -04:00
|
|
|
threads.append(Thread(target = self.theThread,
|
2008-05-13 17:57:59 -03:00
|
|
|
args=(db.DB_LOCK_READ,)))
|
2002-12-30 16:53:52 -04:00
|
|
|
threads.append(Thread(target = self.theThread,
|
2008-05-13 17:57:59 -03:00
|
|
|
args=(db.DB_LOCK_READ,)))
|
2002-12-30 16:53:52 -04:00
|
|
|
threads.append(Thread(target = self.theThread,
|
2008-05-13 17:57:59 -03:00
|
|
|
args=(db.DB_LOCK_WRITE,)))
|
2002-12-30 16:53:52 -04:00
|
|
|
threads.append(Thread(target = self.theThread,
|
2008-05-13 17:57:59 -03:00
|
|
|
args=(db.DB_LOCK_READ,)))
|
2002-12-30 16:53:52 -04:00
|
|
|
threads.append(Thread(target = self.theThread,
|
2008-05-13 17:57:59 -03:00
|
|
|
args=(db.DB_LOCK_READ,)))
|
2002-12-30 16:53:52 -04:00
|
|
|
threads.append(Thread(target = self.theThread,
|
2008-05-13 17:57:59 -03:00
|
|
|
args=(db.DB_LOCK_WRITE,)))
|
2002-12-30 16:53:52 -04:00
|
|
|
threads.append(Thread(target = self.theThread,
|
2008-05-13 17:57:59 -03:00
|
|
|
args=(db.DB_LOCK_WRITE,)))
|
2002-12-30 16:53:52 -04:00
|
|
|
threads.append(Thread(target = self.theThread,
|
2008-05-13 17:57:59 -03:00
|
|
|
args=(db.DB_LOCK_WRITE,)))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
for t in threads:
|
2008-08-31 11:00:51 -03:00
|
|
|
import sys
|
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
t.setDaemon(True)
|
|
|
|
else :
|
|
|
|
t.daemon = True
|
2002-11-19 13:47:07 -04:00
|
|
|
t.start()
|
|
|
|
for t in threads:
|
|
|
|
t.join()
|
|
|
|
|
2008-05-17 03:12:02 -03:00
|
|
|
def test03_lock_timeout(self):
|
2008-05-13 17:57:59 -03:00
|
|
|
self.env.set_timeout(0, db.DB_SET_LOCK_TIMEOUT)
|
|
|
|
self.env.set_timeout(0, db.DB_SET_TXN_TIMEOUT)
|
|
|
|
self.env.set_timeout(123456, db.DB_SET_LOCK_TIMEOUT)
|
|
|
|
self.env.set_timeout(7890123, db.DB_SET_TXN_TIMEOUT)
|
|
|
|
|
|
|
|
def deadlock_detection() :
|
|
|
|
while not deadlock_detection.end :
|
|
|
|
deadlock_detection.count = \
|
|
|
|
self.env.lock_detect(db.DB_LOCK_EXPIRE)
|
|
|
|
if deadlock_detection.count :
|
|
|
|
while not deadlock_detection.end :
|
|
|
|
pass
|
|
|
|
break
|
|
|
|
time.sleep(0.01)
|
|
|
|
|
|
|
|
deadlock_detection.end=False
|
|
|
|
deadlock_detection.count=0
|
|
|
|
t=Thread(target=deadlock_detection)
|
2008-08-31 11:00:51 -03:00
|
|
|
import sys
|
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
t.setDaemon(True)
|
|
|
|
else :
|
|
|
|
t.daemon = True
|
2008-05-13 17:57:59 -03:00
|
|
|
t.start()
|
|
|
|
self.env.set_timeout(100000, db.DB_SET_LOCK_TIMEOUT)
|
|
|
|
anID = self.env.lock_id()
|
|
|
|
anID2 = self.env.lock_id()
|
|
|
|
self.assertNotEqual(anID, anID2)
|
|
|
|
lock = self.env.lock_get(anID, "shared lock", db.DB_LOCK_WRITE)
|
|
|
|
start_time=time.time()
|
|
|
|
self.assertRaises(db.DBLockNotGrantedError,
|
|
|
|
self.env.lock_get,anID2, "shared lock", db.DB_LOCK_READ)
|
|
|
|
end_time=time.time()
|
|
|
|
deadlock_detection.end=True
|
|
|
|
self.assertTrue((end_time-start_time) >= 0.1)
|
|
|
|
self.env.lock_put(lock)
|
|
|
|
t.join()
|
|
|
|
|
2008-07-23 08:38:42 -03:00
|
|
|
self.env.lock_id_free(anID)
|
|
|
|
self.env.lock_id_free(anID2)
|
2008-05-13 17:57:59 -03:00
|
|
|
|
|
|
|
if db.version() >= (4,6):
|
|
|
|
self.assertTrue(deadlock_detection.count>0)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
def theThread(self, lockType):
|
2008-08-31 11:00:51 -03:00
|
|
|
import sys
|
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
name = currentThread().getName()
|
|
|
|
else :
|
|
|
|
name = currentThread().name
|
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
if lockType == db.DB_LOCK_WRITE:
|
|
|
|
lt = "write"
|
|
|
|
else:
|
|
|
|
lt = "read"
|
|
|
|
|
|
|
|
anID = self.env.lock_id()
|
|
|
|
if verbose:
|
|
|
|
print "%s: locker ID: %s" % (name, anID)
|
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
for i in xrange(1000) :
|
|
|
|
lock = self.env.lock_get(anID, "some locked thing", lockType)
|
|
|
|
if verbose:
|
|
|
|
print "%s: Aquired %s lock: %s" % (name, lt, lock)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
self.env.lock_put(lock)
|
|
|
|
if verbose:
|
|
|
|
print "%s: Released %s lock: %s" % (name, lt, lock)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2008-07-23 08:38:42 -03:00
|
|
|
self.env.lock_id_free(anID)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
def test_suite():
|
|
|
|
suite = unittest.TestSuite()
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
if have_threads:
|
2002-12-30 16:53:52 -04:00
|
|
|
suite.addTest(unittest.makeSuite(LockingTestCase))
|
2002-11-19 13:47:07 -04:00
|
|
|
else:
|
2002-12-30 16:53:52 -04:00
|
|
|
suite.addTest(unittest.makeSuite(LockingTestCase, 'test01'))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
return suite
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2002-12-30 16:53:52 -04:00
|
|
|
unittest.main(defaultTest='test_suite')
|