Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(8)

Side by Side Diff: Lib/test/test_threaded_import.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_tarfile.py ('k') | Lib/test/test_threading.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # This is a variant of the very old (early 90's) file 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 2 # Demo/threads/bug.py. It simply provokes a number of threads into
3 # trying to import the same module "at the same time". 3 # trying to import the same module "at the same time".
4 # There are no pleasant failure modes -- most likely is that Python 4 # There are no pleasant failure modes -- most likely is that Python
5 # complains several times about module random having no attribute 5 # complains several times about module random having no attribute
6 # randrange, and then Python hangs. 6 # randrange, and then Python hangs.
7 7
8 import os 8 import os
9 import imp 9 import imp
10 import sys 10 import sys
11 import time 11 import time
12 import shutil 12 import shutil
13 import unittest 13 import unittest
14 from test.support import ( 14 from test.support import verbose, import_module, run_unittest, TESTFN
15 verbose, import_module, run_unittest, TESTFN, reap_threads) 15 thread = import_module('_thread')
16 threading = import_module('threading') 16 threading = import_module('threading')
17 17
18 def task(N, done, done_tasks, errors): 18 def task(N, done, done_tasks, errors):
19 try: 19 try:
20 # We don't use modulefinder but still import it in order to stress 20 # We don't use modulefinder but still import it in order to stress
21 # importing of different modules from several threads. 21 # importing of different modules from several threads.
22 if len(done_tasks) % 2: 22 if len(done_tasks) % 2:
23 import modulefinder 23 import modulefinder
24 import random 24 import random
25 else: 25 else:
(...skipping 29 matching lines...) Expand all
55 'D': """import A""", 55 'D': """import A""",
56 } 56 }
57 57
58 class Finder: 58 class Finder:
59 """A dummy finder to detect concurrent access to its find_module() 59 """A dummy finder to detect concurrent access to its find_module()
60 method.""" 60 method."""
61 61
62 def __init__(self): 62 def __init__(self):
63 self.numcalls = 0 63 self.numcalls = 0
64 self.x = 0 64 self.x = 0
65 self.lock = threading.Lock() 65 self.lock = thread.allocate_lock()
66 66
67 def find_module(self, name, path=None): 67 def find_module(self, name, path=None):
68 # Simulate some thread-unsafe behaviour. If calls to find_module() 68 # Simulate some thread-unsafe behaviour. If calls to find_module()
69 # are properly serialized, `x` will end up the same as `numcalls`. 69 # are properly serialized, `x` will end up the same as `numcalls`.
70 # Otherwise not. 70 # Otherwise not.
71 with self.lock: 71 with self.lock:
72 self.numcalls += 1 72 self.numcalls += 1
73 x = self.x 73 x = self.x
74 time.sleep(0.1) 74 time.sleep(0.1)
75 self.x = x + 1 75 self.x = x + 1
(...skipping 30 matching lines...) Expand all
106 # Make sure that random and modulefinder get reimported freshly 106 # Make sure that random and modulefinder get reimported freshly
107 for modname in ['random', 'modulefinder']: 107 for modname in ['random', 'modulefinder']:
108 try: 108 try:
109 del sys.modules[modname] 109 del sys.modules[modname]
110 except KeyError: 110 except KeyError:
111 pass 111 pass
112 errors = [] 112 errors = []
113 done_tasks = [] 113 done_tasks = []
114 done.clear() 114 done.clear()
115 for i in range(N): 115 for i in range(N):
116 t = threading.Thread(target=task, 116 thread.start_new_thread(task, (N, done, done_tasks, errors,))
117 args=(N, done, done_tasks, errors,))
118 t.start()
119 done.wait(60) 117 done.wait(60)
120 self.assertFalse(errors) 118 self.assertFalse(errors)
121 if verbose: 119 if verbose:
122 print("OK.") 120 print("OK.")
123 121
124 def test_parallel_module_init(self): 122 def test_parallel_module_init(self):
125 self.check_parallel_module_init() 123 self.check_parallel_module_init()
126 124
127 def test_parallel_meta_path(self): 125 def test_parallel_meta_path(self):
128 finder = Finder() 126 finder = Finder()
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 results.append(getattr(B, 'x', None)) 196 results.append(getattr(B, 'x', None))
199 t1 = threading.Thread(target=import_ab) 197 t1 = threading.Thread(target=import_ab)
200 t2 = threading.Thread(target=import_ba) 198 t2 = threading.Thread(target=import_ba)
201 t1.start() 199 t1.start()
202 t2.start() 200 t2.start()
203 t1.join() 201 t1.join()
204 t2.join() 202 t2.join()
205 self.assertEqual(set(results), {'a', 'b'}) 203 self.assertEqual(set(results), {'a', 'b'})
206 204
207 205
208 @reap_threads
209 def test_main(): 206 def test_main():
210 run_unittest(ThreadedImportTests) 207 run_unittest(ThreadedImportTests)
211 208
212 if __name__ == "__main__": 209 if __name__ == "__main__":
213 test_main() 210 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_tarfile.py ('k') | Lib/test/test_threading.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+