Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 1 | # This is a variant of the very old (early 90's) file |
| 2 | # Demo/threads/bug.py. It simply provokes a number of threads into |
| 3 | # trying to import the same module "at the same time". |
| 4 | # There are no pleasant failure modes -- most likely is that Python |
| 5 | # complains several times about module random having no attribute |
| 6 | # randrange, and then Python hangs. |
| 7 | |
Antoine Pitrou | 1f9dea0 | 2010-07-14 11:52:38 +0000 | [diff] [blame] | 8 | import imp |
| 9 | import sys |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 10 | import unittest |
Antoine Pitrou | 1f9dea0 | 2010-07-14 11:52:38 +0000 | [diff] [blame] | 11 | from test.support import verbose, TestFailed, import_module, run_unittest |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 12 | thread = import_module('_thread') |
Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 13 | |
Antoine Pitrou | 1f9dea0 | 2010-07-14 11:52:38 +0000 | [diff] [blame] | 14 | def task(N, done, done_tasks, errors): |
| 15 | try: |
| 16 | import random |
| 17 | # This will fail if random is not completely initialized |
| 18 | x = random.randrange(1, 3) |
| 19 | except Exception as e: |
| 20 | errors.append(e.with_traceback(None)) |
| 21 | finally: |
| 22 | done_tasks.append(thread.get_ident()) |
| 23 | finished = len(done_tasks) == N |
| 24 | if finished: |
| 25 | done.release() |
Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 26 | |
Antoine Pitrou | 1f9dea0 | 2010-07-14 11:52:38 +0000 | [diff] [blame] | 27 | |
| 28 | class ThreadedImportTests(unittest.TestCase): |
| 29 | |
Antoine Pitrou | 448acd0 | 2010-07-16 19:10:38 +0000 | [diff] [blame] | 30 | def setUp(self): |
| 31 | self.old_random = sys.modules.pop('random', None) |
| 32 | |
| 33 | def tearDown(self): |
| 34 | # If the `random` module was already initialized, we restore the |
| 35 | # old module at the end so that pickling tests don't fail. |
| 36 | # See http://bugs.python.org/issue3657#msg110461 |
| 37 | if self.old_random is not None: |
| 38 | sys.modules['random'] = self.old_random |
| 39 | |
Antoine Pitrou | 1f9dea0 | 2010-07-14 11:52:38 +0000 | [diff] [blame] | 40 | def test_parallel_module_init(self): |
| 41 | if imp.lock_held(): |
| 42 | # This triggers on, e.g., from test import autotest. |
| 43 | raise unittest.SkipTest("can't run when import lock is held") |
| 44 | |
| 45 | done = thread.allocate_lock() |
| 46 | done.acquire() |
| 47 | for N in (20, 50) * 3: |
| 48 | if verbose: |
| 49 | print("Trying", N, "threads ...", end=' ') |
| 50 | # Make sure that random gets reimported freshly |
| 51 | try: |
| 52 | del sys.modules['random'] |
| 53 | except KeyError: |
| 54 | pass |
| 55 | errors = [] |
| 56 | done_tasks = [] |
| 57 | for i in range(N): |
| 58 | thread.start_new_thread(task, (N, done, done_tasks, errors,)) |
| 59 | done.acquire() |
| 60 | self.assertFalse(errors) |
| 61 | if verbose: |
| 62 | print("OK.") |
Tim Peters | 20882dd | 2002-02-16 07:26:27 +0000 | [diff] [blame] | 63 | done.release() |
Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 64 | |
Antoine Pitrou | 1f9dea0 | 2010-07-14 11:52:38 +0000 | [diff] [blame] | 65 | def test_import_hangers(self): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 66 | # In case this test is run again, make sure the helper module |
| 67 | # gets loaded from scratch again. |
Antoine Pitrou | 1f9dea0 | 2010-07-14 11:52:38 +0000 | [diff] [blame] | 68 | try: |
| 69 | del sys.modules['test.threaded_import_hangers'] |
| 70 | except KeyError: |
| 71 | pass |
| 72 | import test.threaded_import_hangers |
| 73 | self.assertFalse(test.threaded_import_hangers.errors) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 74 | |
Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 75 | |
Antoine Pitrou | 1f9dea0 | 2010-07-14 11:52:38 +0000 | [diff] [blame] | 76 | def test_main(): |
| 77 | run_unittest(ThreadedImportTests) |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 78 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 79 | |
Tim Peters | d974221 | 2001-05-22 18:28:25 +0000 | [diff] [blame] | 80 | if __name__ == "__main__": |
| 81 | test_main() |