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 | |
| 30 | def test_parallel_module_init(self): |
| 31 | if imp.lock_held(): |
| 32 | # This triggers on, e.g., from test import autotest. |
| 33 | raise unittest.SkipTest("can't run when import lock is held") |
| 34 | |
| 35 | done = thread.allocate_lock() |
| 36 | done.acquire() |
| 37 | for N in (20, 50) * 3: |
| 38 | if verbose: |
| 39 | print("Trying", N, "threads ...", end=' ') |
| 40 | # Make sure that random gets reimported freshly |
| 41 | try: |
| 42 | del sys.modules['random'] |
| 43 | except KeyError: |
| 44 | pass |
| 45 | errors = [] |
| 46 | done_tasks = [] |
| 47 | for i in range(N): |
| 48 | thread.start_new_thread(task, (N, done, done_tasks, errors,)) |
| 49 | done.acquire() |
| 50 | self.assertFalse(errors) |
| 51 | if verbose: |
| 52 | print("OK.") |
Tim Peters | 20882dd | 2002-02-16 07:26:27 +0000 | [diff] [blame] | 53 | done.release() |
Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 54 | |
Antoine Pitrou | 1f9dea0 | 2010-07-14 11:52:38 +0000 | [diff] [blame] | 55 | def test_import_hangers(self): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 56 | # In case this test is run again, make sure the helper module |
| 57 | # gets loaded from scratch again. |
Antoine Pitrou | 1f9dea0 | 2010-07-14 11:52:38 +0000 | [diff] [blame] | 58 | try: |
| 59 | del sys.modules['test.threaded_import_hangers'] |
| 60 | except KeyError: |
| 61 | pass |
| 62 | import test.threaded_import_hangers |
| 63 | self.assertFalse(test.threaded_import_hangers.errors) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 64 | |
Tim Peters | aa22223 | 2001-05-22 09:34:27 +0000 | [diff] [blame] | 65 | |
Antoine Pitrou | 1f9dea0 | 2010-07-14 11:52:38 +0000 | [diff] [blame] | 66 | def test_main(): |
| 67 | run_unittest(ThreadedImportTests) |
Tim Peters | 6923234 | 2001-08-30 05:16:13 +0000 | [diff] [blame] | 68 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 69 | |
Tim Peters | d974221 | 2001-05-22 18:28:25 +0000 | [diff] [blame] | 70 | if __name__ == "__main__": |
| 71 | test_main() |