blob: 9b7e3b79e6b53e1b8c29d1e11539cf447b468a07 [file] [log] [blame]
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001import imp
Tim Peters579bed72003-04-26 14:31:24 +00002from test.test_support import TestFailed
Neal Norwitz2294c0d2003-02-12 23:02:21 +00003
Tim Peters579bed72003-04-26 14:31:24 +00004def verify_lock_state(expected):
5 if imp.lock_held() != expected:
6 raise TestFailed("expected imp.lock_held() to be %r" % expected)
Neal Norwitz2294c0d2003-02-12 23:02:21 +00007
Tim Peters579bed72003-04-26 14:31:24 +00008def testLock():
9 LOOPS = 50
10
11 # The import lock may already be held, e.g. if the test suite is run
12 # via "import test.autotest".
13 lock_held_at_start = imp.lock_held()
14 verify_lock_state(lock_held_at_start)
15
16 for i in range(LOOPS):
17 imp.acquire_lock()
18 verify_lock_state(True)
19
20 for i in range(LOOPS):
21 imp.release_lock()
22
23 # The original state should be restored now.
24 verify_lock_state(lock_held_at_start)
25
26 if not lock_held_at_start:
27 try:
Neal Norwitz2294c0d2003-02-12 23:02:21 +000028 imp.release_lock()
Tim Peters579bed72003-04-26 14:31:24 +000029 except RuntimeError:
30 pass
31 else:
32 raise TestFailed("release_lock() without lock should raise "
33 "RuntimeError")
Neal Norwitz2294c0d2003-02-12 23:02:21 +000034
Neal Norwitz996acf12003-02-17 14:51:41 +000035def test_main():
Tim Peters579bed72003-04-26 14:31:24 +000036 testLock()
Neal Norwitz996acf12003-02-17 14:51:41 +000037
Neal Norwitz2294c0d2003-02-12 23:02:21 +000038if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +000039 test_main()