blob: 62b14e071bd0da41585376c2293c66f92e3792c1 [file] [log] [blame]
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001import imp
Thomas Wouters89f507f2006-12-13 04:49:30 +00002import thread
3import unittest
4from test import test_support
Neal Norwitz2294c0d2003-02-12 23:02:21 +00005
Neal Norwitz2294c0d2003-02-12 23:02:21 +00006
Thomas Wouters89f507f2006-12-13 04:49:30 +00007class LockTests(unittest.TestCase):
Tim Peters579bed72003-04-26 14:31:24 +00008
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 """Very basic test of import lock functions."""
Tim Peters579bed72003-04-26 14:31:24 +000010
Thomas Wouters89f507f2006-12-13 04:49:30 +000011 def verify_lock_state(self, expected):
12 self.failUnlessEqual(imp.lock_held(), expected,
13 "expected imp.lock_held() to be %r" % expected)
14 def testLock(self):
15 LOOPS = 50
Tim Peters579bed72003-04-26 14:31:24 +000016
Thomas Wouters89f507f2006-12-13 04:49:30 +000017 # The import lock may already be held, e.g. if the test suite is run
18 # via "import test.autotest".
19 lock_held_at_start = imp.lock_held()
20 self.verify_lock_state(lock_held_at_start)
Tim Peters579bed72003-04-26 14:31:24 +000021
Thomas Wouters89f507f2006-12-13 04:49:30 +000022 for i in range(LOOPS):
23 imp.acquire_lock()
24 self.verify_lock_state(True)
Tim Peters579bed72003-04-26 14:31:24 +000025
Thomas Wouters89f507f2006-12-13 04:49:30 +000026 for i in range(LOOPS):
Neal Norwitz2294c0d2003-02-12 23:02:21 +000027 imp.release_lock()
Thomas Wouters89f507f2006-12-13 04:49:30 +000028
29 # The original state should be restored now.
30 self.verify_lock_state(lock_held_at_start)
31
32 if not lock_held_at_start:
33 try:
34 imp.release_lock()
35 except RuntimeError:
36 pass
37 else:
38 self.fail("release_lock() without lock should raise "
39 "RuntimeError")
Neal Norwitz2294c0d2003-02-12 23:02:21 +000040
Neal Norwitz996acf12003-02-17 14:51:41 +000041def test_main():
Thomas Wouters89f507f2006-12-13 04:49:30 +000042 test_support.run_unittest(
43 LockTests,
44 )
Neal Norwitz996acf12003-02-17 14:51:41 +000045
Neal Norwitz2294c0d2003-02-12 23:02:21 +000046if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +000047 test_main()