2 TestCases for testing the locking sub-system.
8 from pprint
import pprint
9 from whrandom
import random
12 from threading
import Thread
, currentThread
19 from test_all
import verbose
25 # For earlier Pythons w/distutils pybsddb
29 #----------------------------------------------------------------------
31 class LockingTestCase(unittest
.TestCase
):
34 homeDir
= os
.path
.join(os
.path
.dirname(sys
.argv
[0]), 'db_home')
35 self
.homeDir
= homeDir
36 try: os
.mkdir(homeDir
)
39 self
.env
.open(homeDir
, db
.DB_THREAD | db
.DB_INIT_MPOOL |
40 db
.DB_INIT_LOCK | db
.DB_CREATE
)
46 files
= glob
.glob(os
.path
.join(self
.homeDir
, '*'))
51 def test01_simple(self
):
54 print "Running %s.test01_simple..." % self
.__class
__.__name
__
56 anID
= self
.env
.lock_id()
58 print "locker ID: %s" % anID
59 lock
= self
.env
.lock_get(anID
, "some locked thing", db
.DB_LOCK_WRITE
)
61 print "Aquired lock: %s" % lock
63 self
.env
.lock_put(lock
)
65 print "Released lock: %s" % lock
70 def test02_threaded(self
):
73 print "Running %s.test02_threaded..." % self
.__class
__.__name
__
76 threads
.append(Thread(target
= self
.theThread
,
77 args
=(5, db
.DB_LOCK_WRITE
)))
78 threads
.append(Thread(target
= self
.theThread
,
79 args
=(1, db
.DB_LOCK_READ
)))
80 threads
.append(Thread(target
= self
.theThread
,
81 args
=(1, db
.DB_LOCK_READ
)))
82 threads
.append(Thread(target
= self
.theThread
,
83 args
=(1, db
.DB_LOCK_WRITE
)))
84 threads
.append(Thread(target
= self
.theThread
,
85 args
=(1, db
.DB_LOCK_READ
)))
86 threads
.append(Thread(target
= self
.theThread
,
87 args
=(1, db
.DB_LOCK_READ
)))
88 threads
.append(Thread(target
= self
.theThread
,
89 args
=(1, db
.DB_LOCK_WRITE
)))
90 threads
.append(Thread(target
= self
.theThread
,
91 args
=(1, db
.DB_LOCK_WRITE
)))
92 threads
.append(Thread(target
= self
.theThread
,
93 args
=(1, db
.DB_LOCK_WRITE
)))
102 def theThread(self
, sleepTime
, lockType
):
103 name
= currentThread().getName()
104 if lockType
== db
.DB_LOCK_WRITE
:
109 anID
= self
.env
.lock_id()
111 print "%s: locker ID: %s" % (name
, anID
)
113 lock
= self
.env
.lock_get(anID
, "some locked thing", lockType
)
115 print "%s: Aquired %s lock: %s" % (name
, lt
, lock
)
117 time
.sleep(sleepTime
)
119 self
.env
.lock_put(lock
)
121 print "%s: Released %s lock: %s" % (name
, lt
, lock
)
124 #----------------------------------------------------------------------
127 suite
= unittest
.TestSuite()
130 suite
.addTest(unittest
.makeSuite(LockingTestCase
))
132 suite
.addTest(unittest
.makeSuite(LockingTestCase
, 'test01'))
137 if __name__
== '__main__':
138 unittest
.main(defaultTest
='test_suite')