Facundo Batista | c12b988 | 2008-02-23 22:54:12 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | import test.test_support |
| 3 | |
Brett Cannon | 9d44182 | 2008-05-08 19:26:08 +0000 | [diff] [blame] | 4 | mutex = test.test_support.import_module("mutex", deprecated=True) |
| 5 | |
Facundo Batista | c12b988 | 2008-02-23 22:54:12 +0000 | [diff] [blame] | 6 | class MutexTest(unittest.TestCase): |
| 7 | |
Facundo Batista | c12b988 | 2008-02-23 22:54:12 +0000 | [diff] [blame] | 8 | def test_lock_and_unlock(self): |
Benjamin Peterson | c5393c6 | 2008-06-03 01:30:37 +0000 | [diff] [blame] | 9 | |
| 10 | def called_by_mutex(some_data): |
| 11 | self.assertEqual(some_data, "spam") |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 12 | self.assertTrue(m.test(), "mutex not held") |
Benjamin Peterson | c5393c6 | 2008-06-03 01:30:37 +0000 | [diff] [blame] | 13 | # Nested locking |
| 14 | m.lock(called_by_mutex2, "eggs") |
| 15 | |
| 16 | def called_by_mutex2(some_data): |
Serhiy Storchaka | 8876145 | 2012-12-28 00:32:19 +0200 | [diff] [blame] | 17 | self.assertEqual(some_data, "eggs") |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 18 | self.assertTrue(m.test(), "mutex not held") |
| 19 | self.assertTrue(ready_for_2, |
Benjamin Peterson | c5393c6 | 2008-06-03 01:30:37 +0000 | [diff] [blame] | 20 | "called_by_mutex2 called too soon") |
| 21 | |
| 22 | m = mutex.mutex() |
| 23 | read_for_2 = False |
| 24 | m.lock(called_by_mutex, "spam") |
| 25 | ready_for_2 = True |
Facundo Batista | c12b988 | 2008-02-23 22:54:12 +0000 | [diff] [blame] | 26 | # unlock both locks |
Benjamin Peterson | c5393c6 | 2008-06-03 01:30:37 +0000 | [diff] [blame] | 27 | m.unlock() |
| 28 | m.unlock() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 29 | self.assertFalse(m.test(), "mutex still held") |
Facundo Batista | c12b988 | 2008-02-23 22:54:12 +0000 | [diff] [blame] | 30 | |
| 31 | def test_main(): |
| 32 | test.test_support.run_unittest(MutexTest) |
| 33 | |
| 34 | if __name__ == "__main__": |
| 35 | test_main() |