Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 1 | import imp |
Brett Cannon | 373d90b | 2006-10-03 23:23:14 +0000 | [diff] [blame] | 2 | import unittest |
| 3 | from test import test_support |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 4 | |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 5 | |
Brett Cannon | 373d90b | 2006-10-03 23:23:14 +0000 | [diff] [blame] | 6 | class LockTests(unittest.TestCase): |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 7 | |
Brett Cannon | 373d90b | 2006-10-03 23:23:14 +0000 | [diff] [blame] | 8 | """Very basic test of import lock functions.""" |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 9 | |
Brett Cannon | 373d90b | 2006-10-03 23:23:14 +0000 | [diff] [blame] | 10 | 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 Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 15 | |
Brett Cannon | 373d90b | 2006-10-03 23:23:14 +0000 | [diff] [blame] | 16 | # 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 Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 20 | |
Brett Cannon | 373d90b | 2006-10-03 23:23:14 +0000 | [diff] [blame] | 21 | for i in range(LOOPS): |
| 22 | imp.acquire_lock() |
| 23 | self.verify_lock_state(True) |
Tim Peters | 579bed7 | 2003-04-26 14:31:24 +0000 | [diff] [blame] | 24 | |
Brett Cannon | 373d90b | 2006-10-03 23:23:14 +0000 | [diff] [blame] | 25 | for i in range(LOOPS): |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 26 | imp.release_lock() |
Brett Cannon | 373d90b | 2006-10-03 23:23:14 +0000 | [diff] [blame] | 27 | |
| 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 Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 39 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 40 | def test_main(): |
Brett Cannon | 373d90b | 2006-10-03 23:23:14 +0000 | [diff] [blame] | 41 | test_support.run_unittest( |
| 42 | LockTests, |
| 43 | ) |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 44 | |
Neal Norwitz | 2294c0d | 2003-02-12 23:02:21 +0000 | [diff] [blame] | 45 | if __name__ == "__main__": |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 46 | test_main() |