1import unittest
2import test.test_support
3
4mutex = test.test_support.import_module("mutex", deprecated=True)
5
6class MutexTest(unittest.TestCase):
7
8    def test_lock_and_unlock(self):
9
10        def called_by_mutex(some_data):
11            self.assertEqual(some_data, "spam")
12            self.assertTrue(m.test(), "mutex not held")
13            # Nested locking
14            m.lock(called_by_mutex2, "eggs")
15
16        def called_by_mutex2(some_data):
17            self.assertEqual(some_data, "eggs")
18            self.assertTrue(m.test(), "mutex not held")
19            self.assertTrue(ready_for_2,
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
26        # unlock both locks
27        m.unlock()
28        m.unlock()
29        self.assertFalse(m.test(), "mutex still held")
30
31def test_main():
32    test.test_support.run_unittest(MutexTest)
33
34if __name__ == "__main__":
35    test_main()
36