blob: 893ba24617119f313df78e8299cb41f45ba12297 [file] [log] [blame]
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001import imp
Brett Cannon94eaee62004-12-18 21:06:55 +00002from test.test_support import TestFailed, TestSkipped
3try:
4 import thread
5except ImportError:
6 raise TestSkipped("test only valid when thread support is available")
Neal Norwitz2294c0d2003-02-12 23:02:21 +00007
Tim Peters579bed72003-04-26 14:31:24 +00008def verify_lock_state(expected):
9 if imp.lock_held() != expected:
10 raise TestFailed("expected imp.lock_held() to be %r" % expected)
Neal Norwitz2294c0d2003-02-12 23:02:21 +000011
Tim Peters579bed72003-04-26 14:31:24 +000012def testLock():
13 LOOPS = 50
14
15 # The import lock may already be held, e.g. if the test suite is run
16 # via "import test.autotest".
17 lock_held_at_start = imp.lock_held()
18 verify_lock_state(lock_held_at_start)
19
20 for i in range(LOOPS):
21 imp.acquire_lock()
22 verify_lock_state(True)
23
24 for i in range(LOOPS):
25 imp.release_lock()
26
27 # The original state should be restored now.
28 verify_lock_state(lock_held_at_start)
29
30 if not lock_held_at_start:
31 try:
Neal Norwitz2294c0d2003-02-12 23:02:21 +000032 imp.release_lock()
Tim Peters579bed72003-04-26 14:31:24 +000033 except RuntimeError:
34 pass
35 else:
36 raise TestFailed("release_lock() without lock should raise "
37 "RuntimeError")
Neal Norwitz2294c0d2003-02-12 23:02:21 +000038
Neal Norwitz996acf12003-02-17 14:51:41 +000039def test_main():
Tim Peters579bed72003-04-26 14:31:24 +000040 testLock()
Neal Norwitz996acf12003-02-17 14:51:41 +000041
Neal Norwitz2294c0d2003-02-12 23:02:21 +000042if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +000043 test_main()