blob: f4a1649698c0f9bbc7962588b3e67f3c2a13ba0c [file] [log] [blame]
Neal Norwitz2294c0d2003-02-12 23:02:21 +00001import imp
Brett Cannon373d90b2006-10-03 23:23:14 +00002import unittest
3from test import test_support
Neal Norwitz2294c0d2003-02-12 23:02:21 +00004
Neal Norwitz2294c0d2003-02-12 23:02:21 +00005
Brett Cannon373d90b2006-10-03 23:23:14 +00006class LockTests(unittest.TestCase):
Tim Peters579bed72003-04-26 14:31:24 +00007
Brett Cannon373d90b2006-10-03 23:23:14 +00008 """Very basic test of import lock functions."""
Tim Peters579bed72003-04-26 14:31:24 +00009
Brett Cannon373d90b2006-10-03 23:23:14 +000010 def verify_lock_state(self, expected):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000011 self.assertEqual(imp.lock_held(), expected,
Brett Cannon373d90b2006-10-03 23:23:14 +000012 "expected imp.lock_held() to be %r" % expected)
13 def testLock(self):
14 LOOPS = 50
Tim Peters579bed72003-04-26 14:31:24 +000015
Brett Cannon373d90b2006-10-03 23:23:14 +000016 # The import lock may already be held, e.g. if the test suite is run
17 # via "import test.autotest".
18 lock_held_at_start = imp.lock_held()
19 self.verify_lock_state(lock_held_at_start)
Tim Peters579bed72003-04-26 14:31:24 +000020
Brett Cannon373d90b2006-10-03 23:23:14 +000021 for i in range(LOOPS):
22 imp.acquire_lock()
23 self.verify_lock_state(True)
Tim Peters579bed72003-04-26 14:31:24 +000024
Brett Cannon373d90b2006-10-03 23:23:14 +000025 for i in range(LOOPS):
Neal Norwitz2294c0d2003-02-12 23:02:21 +000026 imp.release_lock()
Brett Cannon373d90b2006-10-03 23:23:14 +000027
28 # The original state should be restored now.
29 self.verify_lock_state(lock_held_at_start)
30
31 if not lock_held_at_start:
32 try:
33 imp.release_lock()
34 except RuntimeError:
35 pass
36 else:
37 self.fail("release_lock() without lock should raise "
38 "RuntimeError")
Neal Norwitz2294c0d2003-02-12 23:02:21 +000039
Brett Cannon3aa2a492008-08-06 22:28:09 +000040class ReloadTests(unittest.TestCase):
41
42 """Very basic tests to make sure that imp.reload() operates just like
43 reload()."""
44
45 def test_source(self):
46 import os
47 imp.reload(os)
48
49 def test_extension(self):
50 import time
51 imp.reload(time)
52
53 def test_builtin(self):
54 import marshal
55 imp.reload(marshal)
56
57
Neal Norwitz996acf12003-02-17 14:51:41 +000058def test_main():
Hirokazu Yamamoto631be012008-09-08 23:38:42 +000059 tests = [
60 ReloadTests,
61 ]
62 try:
63 import thread
64 except ImportError:
65 pass
66 else:
67 tests.append(LockTests)
68 test_support.run_unittest(*tests)
Neal Norwitz996acf12003-02-17 14:51:41 +000069
Neal Norwitz2294c0d2003-02-12 23:02:21 +000070if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +000071 test_main()