annotate Lib/test/test_threaded_import.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 0bf9e3099c57
children e6009b9cd908 4cdf21a0dbbc
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
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
8 import thread
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
9 from test.test_support import verbose, TestSkipped, TestFailed
17897
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
10
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
11 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
12 done = thread.allocate_lock()
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
13
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
14 def task():
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
15 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
16 import random
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
17 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
18 critical_section.acquire()
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
19 N -= 1
21995
2714739d483b backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 19596
diff changeset
20 # 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
21 # 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
22 # teardown; then critical_section.release() raises AttributeError.
2714739d483b backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 19596
diff changeset
23 finished = N == 0
2714739d483b backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 19596
diff changeset
24 critical_section.release()
2714739d483b backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents: 19596
diff changeset
25 if finished:
17897
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
26 done.release()
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
27
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
28 def test_import_hangers():
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
29 import sys
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
30 if verbose:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
31 print "testing import hangers ...",
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
32
38878
0bf9e3099c57 Repair KeyError when running test_threaded_import under -R,
Tim Peters <tim.peters@gmail.com>
parents: 37908
diff changeset
33 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
34 try:
38878
0bf9e3099c57 Repair KeyError when running test_threaded_import under -R,
Tim Peters <tim.peters@gmail.com>
parents: 37908
diff changeset
35 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
36 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
37 elif verbose:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
38 print "OK."
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
39 finally:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
40 # 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
41 # gets loaded from scratch again.
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
42 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
43
17913
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
44 # 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
45 # 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
46 # 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
47 # 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
48 # 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
49 # 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
50 # 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
51 # importing it.
17897
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
52
17913
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
53 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
54 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
55
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 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
57 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
58 # This triggers on, e.g., from test import autotest.
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 raise TestSkipped("can't run when import lock is 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
60
17897
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
61 done.acquire()
17913
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
62 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
63 if verbose:
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
64 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
65 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
66 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
67 done.acquire()
17913
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
68 if verbose:
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
69 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
70 done.release()
17897
cc984ed26586 New test adapted from the ancient Demo/threads/bug.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
71
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
72 test_import_hangers()
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 32918
diff changeset
73
17913
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
74 if __name__ == "__main__":
46909f22170c Implementing an idea from Guido on the checkins list:
Tim Peters <tim.peters@gmail.com>
parents: 17897
diff changeset
75 test_main()