annotate Lib/test/test_threaded_import.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 3a0484200034
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17897
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1 # This is a variant of the very old (early 90's) file
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2 # Demo/threads/bug.py. It simply provokes a number of threads into
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
3 # trying to import the same module "at the same time".
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
4 # There are no pleasant failure modes -- most likely is that Python
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
5 # complains several times about module random having no attribute
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
6 # randrange, and then Python hangs.
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
7
58770
22358700a4d0 Fix various missing import/unbound name errors.
Georg Brandl <georg@python.org>
parents: 52355
diff changeset
8 import unittest
61046
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58770
diff changeset
9 from test.test_support import verbose, TestFailed, import_module
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58770
diff changeset
10 thread = import_module('thread')
17897
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
11
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
12 critical_section = thread.allocate_lock()
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
13 done = thread.allocate_lock()
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
14
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
15 def task():
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
16 global N, critical_section, done
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
17 import random
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
18 x = random.randrange(1, 3)
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
19 critical_section.acquire()
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
20 N -= 1
21995
2714739d483b backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 19596
diff changeset
21 # Must release critical_section before releasing done, else the main
2714739d483b backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 19596
diff changeset
22 # thread can exit and set critical_section to None as part of global
2714739d483b backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 19596
diff changeset
23 # teardown; then critical_section.release() raises AttributeError.
2714739d483b backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 19596
diff changeset
24 finished = N == 0
2714739d483b backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 19596
diff changeset
25 critical_section.release()
2714739d483b backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 19596
diff changeset
26 if finished:
17897
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
27 done.release()
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
28
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
29 def test_import_hangers():
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
30 import sys
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
31 if verbose:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
32 print "testing import hangers ...",
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
33
38878
0bf9e3099c57 Repair KeyError when running test_threaded_import under -R,
Tim Peters <tim.peters@gmail.com>
parents: 37908
diff changeset
34 import test.threaded_import_hangers
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
35 try:
38878
0bf9e3099c57 Repair KeyError when running test_threaded_import under -R,
Tim Peters <tim.peters@gmail.com>
parents: 37908
diff changeset
36 if test.threaded_import_hangers.errors:
0bf9e3099c57 Repair KeyError when running test_threaded_import under -R,
Tim Peters <tim.peters@gmail.com>
parents: 37908
diff changeset
37 raise TestFailed(test.threaded_import_hangers.errors)
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
38 elif verbose:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
39 print "OK."
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
40 finally:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
41 # In case this test is run again, make sure the helper module
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
42 # gets loaded from scratch again.
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
43 del sys.modules['test.threaded_import_hangers']
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
44
17913
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
45 # Tricky: When regrtest imports this module, the thread running regrtest
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
46 # grabs the import lock and won't let go of it until this module returns.
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
47 # All other threads attempting an import hang for the duration. Since
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
48 # this test spawns threads that do little *but* import, we can't do that
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
49 # successfully until after this module finishes importing and regrtest
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
50 # regains control. To make this work, a special case was added to
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
51 # regrtest to invoke a module's "test_main" function (if any) after
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
52 # importing it.
17897
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
53
17913
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
54 def test_main(): # magic name! see above
17897
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
55 global N, done
19596
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 19564
diff changeset
56
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 19564
diff changeset
57 import imp
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 19564
diff changeset
58 if imp.lock_held():
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 19564
diff changeset
59 # This triggers on, e.g., from test import autotest.
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
60 raise unittest.SkipTest("can't run when import lock is held")
19596
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 19564
diff changeset
61
17897
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
62 done.acquire()
17913
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
63 for N in (20, 50) * 3:
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
64 if verbose:
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
65 print "Trying", N, "threads ...",
17897
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
66 for i in range(N):
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
67 thread.start_new_thread(task, ())
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
68 done.acquire()
17913
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
69 if verbose:
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
70 print "OK."
32918
7e7457a69b4d Don't exit test_main() with the lock 'done' held -- there's no cleaner
Michael W. Hudson <mwh@python.net>
parents: 24364
diff changeset
71 done.release()
17897
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
72
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
73 test_import_hangers()
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
74
17913
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
75 if __name__ == "__main__":
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
76 test_main()