blob: 513ca60884113a89673aed0aa76b3a2f579d8f36 [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):
11 self.failUnlessEqual(imp.lock_held(), expected,
12 "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():
Brett Cannon373d90b2006-10-03 23:23:14 +000059 test_support.run_unittest(
60 LockTests,
Brett Cannon3aa2a492008-08-06 22:28:09 +000061 ReloadTests,
Brett Cannon373d90b2006-10-03 23:23:14 +000062 )
Neal Norwitz996acf12003-02-17 14:51:41 +000063
Neal Norwitz2294c0d2003-02-12 23:02:21 +000064if __name__ == "__main__":
Neal Norwitz996acf12003-02-17 14:51:41 +000065 test_main()