move sections
[python/dscho.git] / Lib / test / test_threading.py
blob2e2558a6562d1c91c66b9a266076c97e07628d53
1 # Very rudimentary test of threading module
3 import test.test_support
4 from test.test_support import verbose
5 import random
6 import re
7 import sys
8 thread = test.test_support.import_module('thread')
9 threading = test.test_support.import_module('threading')
10 import time
11 import unittest
12 import weakref
14 from test import lock_tests
16 # A trivial mutable counter.
17 class Counter(object):
18 def __init__(self):
19 self.value = 0
20 def inc(self):
21 self.value += 1
22 def dec(self):
23 self.value -= 1
24 def get(self):
25 return self.value
27 class TestThread(threading.Thread):
28 def __init__(self, name, testcase, sema, mutex, nrunning):
29 threading.Thread.__init__(self, name=name)
30 self.testcase = testcase
31 self.sema = sema
32 self.mutex = mutex
33 self.nrunning = nrunning
35 def run(self):
36 delay = random.random() / 10000.0
37 if verbose:
38 print 'task %s will run for %.1f usec' % (
39 self.name, delay * 1e6)
41 with self.sema:
42 with self.mutex:
43 self.nrunning.inc()
44 if verbose:
45 print self.nrunning.get(), 'tasks are running'
46 self.testcase.assertTrue(self.nrunning.get() <= 3)
48 time.sleep(delay)
49 if verbose:
50 print 'task', self.name, 'done'
52 with self.mutex:
53 self.nrunning.dec()
54 self.testcase.assertTrue(self.nrunning.get() >= 0)
55 if verbose:
56 print '%s is finished. %d tasks are running' % (
57 self.name, self.nrunning.get())
59 class BaseTestCase(unittest.TestCase):
60 def setUp(self):
61 self._threads = test.test_support.threading_setup()
63 def tearDown(self):
64 test.test_support.threading_cleanup(*self._threads)
65 test.test_support.reap_children()
68 class ThreadTests(BaseTestCase):
70 # Create a bunch of threads, let each do some work, wait until all are
71 # done.
72 def test_various_ops(self):
73 # This takes about n/3 seconds to run (about n/3 clumps of tasks,
74 # times about 1 second per clump).
75 NUMTASKS = 10
77 # no more than 3 of the 10 can run at once
78 sema = threading.BoundedSemaphore(value=3)
79 mutex = threading.RLock()
80 numrunning = Counter()
82 threads = []
84 for i in range(NUMTASKS):
85 t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning)
86 threads.append(t)
87 self.assertEqual(t.ident, None)
88 self.assertTrue(re.match('<TestThread\(.*, initial\)>', repr(t)))
89 t.start()
91 if verbose:
92 print 'waiting for all tasks to complete'
93 for t in threads:
94 t.join(NUMTASKS)
95 self.assertTrue(not t.is_alive())
96 self.assertNotEqual(t.ident, 0)
97 self.assertFalse(t.ident is None)
98 self.assertTrue(re.match('<TestThread\(.*, \w+ -?\d+\)>', repr(t)))
99 if verbose:
100 print 'all tasks done'
101 self.assertEqual(numrunning.get(), 0)
103 def test_ident_of_no_threading_threads(self):
104 # The ident still must work for the main thread and dummy threads.
105 self.assertFalse(threading.currentThread().ident is None)
106 def f():
107 ident.append(threading.currentThread().ident)
108 done.set()
109 done = threading.Event()
110 ident = []
111 thread.start_new_thread(f, ())
112 done.wait()
113 self.assertFalse(ident[0] is None)
114 # Kill the "immortal" _DummyThread
115 del threading._active[ident[0]]
117 # run with a small(ish) thread stack size (256kB)
118 def test_various_ops_small_stack(self):
119 if verbose:
120 print 'with 256kB thread stack size...'
121 try:
122 threading.stack_size(262144)
123 except thread.error:
124 if verbose:
125 print 'platform does not support changing thread stack size'
126 return
127 self.test_various_ops()
128 threading.stack_size(0)
130 # run with a large thread stack size (1MB)
131 def test_various_ops_large_stack(self):
132 if verbose:
133 print 'with 1MB thread stack size...'
134 try:
135 threading.stack_size(0x100000)
136 except thread.error:
137 if verbose:
138 print 'platform does not support changing thread stack size'
139 return
140 self.test_various_ops()
141 threading.stack_size(0)
143 def test_foreign_thread(self):
144 # Check that a "foreign" thread can use the threading module.
145 def f(mutex):
146 # Calling current_thread() forces an entry for the foreign
147 # thread to get made in the threading._active map.
148 threading.current_thread()
149 mutex.release()
151 mutex = threading.Lock()
152 mutex.acquire()
153 tid = thread.start_new_thread(f, (mutex,))
154 # Wait for the thread to finish.
155 mutex.acquire()
156 self.assertIn(tid, threading._active)
157 self.assertIsInstance(threading._active[tid], threading._DummyThread)
158 del threading._active[tid]
160 # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently)
161 # exposed at the Python level. This test relies on ctypes to get at it.
162 def test_PyThreadState_SetAsyncExc(self):
163 try:
164 import ctypes
165 except ImportError:
166 if verbose:
167 print "test_PyThreadState_SetAsyncExc can't import ctypes"
168 return # can't do anything
170 set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc
172 class AsyncExc(Exception):
173 pass
175 exception = ctypes.py_object(AsyncExc)
177 # First check it works when setting the exception from the same thread.
178 tid = thread.get_ident()
180 try:
181 result = set_async_exc(ctypes.c_long(tid), exception)
182 # The exception is async, so we might have to keep the VM busy until
183 # it notices.
184 while True:
185 pass
186 except AsyncExc:
187 pass
188 else:
189 # This code is unreachable but it reflects the intent. If we wanted
190 # to be smarter the above loop wouldn't be infinite.
191 self.fail("AsyncExc not raised")
192 try:
193 self.assertEqual(result, 1) # one thread state modified
194 except UnboundLocalError:
195 # The exception was raised too quickly for us to get the result.
196 pass
198 # `worker_started` is set by the thread when it's inside a try/except
199 # block waiting to catch the asynchronously set AsyncExc exception.
200 # `worker_saw_exception` is set by the thread upon catching that
201 # exception.
202 worker_started = threading.Event()
203 worker_saw_exception = threading.Event()
205 class Worker(threading.Thread):
206 def run(self):
207 self.id = thread.get_ident()
208 self.finished = False
210 try:
211 while True:
212 worker_started.set()
213 time.sleep(0.1)
214 except AsyncExc:
215 self.finished = True
216 worker_saw_exception.set()
218 t = Worker()
219 t.daemon = True # so if this fails, we don't hang Python at shutdown
220 t.start()
221 if verbose:
222 print " started worker thread"
224 # Try a thread id that doesn't make sense.
225 if verbose:
226 print " trying nonsensical thread id"
227 result = set_async_exc(ctypes.c_long(-1), exception)
228 self.assertEqual(result, 0) # no thread states modified
230 # Now raise an exception in the worker thread.
231 if verbose:
232 print " waiting for worker thread to get started"
233 ret = worker_started.wait()
234 self.assertTrue(ret)
235 if verbose:
236 print " verifying worker hasn't exited"
237 self.assertTrue(not t.finished)
238 if verbose:
239 print " attempting to raise asynch exception in worker"
240 result = set_async_exc(ctypes.c_long(t.id), exception)
241 self.assertEqual(result, 1) # one thread state modified
242 if verbose:
243 print " waiting for worker to say it caught the exception"
244 worker_saw_exception.wait(timeout=10)
245 self.assertTrue(t.finished)
246 if verbose:
247 print " all OK -- joining worker"
248 if t.finished:
249 t.join()
250 # else the thread is still running, and we have no way to kill it
252 def test_limbo_cleanup(self):
253 # Issue 7481: Failure to start thread should cleanup the limbo map.
254 def fail_new_thread(*args):
255 raise thread.error()
256 _start_new_thread = threading._start_new_thread
257 threading._start_new_thread = fail_new_thread
258 try:
259 t = threading.Thread(target=lambda: None)
260 self.assertRaises(thread.error, t.start)
261 self.assertFalse(
262 t in threading._limbo,
263 "Failed to cleanup _limbo map on failure of Thread.start().")
264 finally:
265 threading._start_new_thread = _start_new_thread
267 def test_finalize_runnning_thread(self):
268 # Issue 1402: the PyGILState_Ensure / _Release functions may be called
269 # very late on python exit: on deallocation of a running thread for
270 # example.
271 try:
272 import ctypes
273 except ImportError:
274 if verbose:
275 print("test_finalize_with_runnning_thread can't import ctypes")
276 return # can't do anything
278 import subprocess
279 rc = subprocess.call([sys.executable, "-c", """if 1:
280 import ctypes, sys, time, thread
282 # This lock is used as a simple event variable.
283 ready = thread.allocate_lock()
284 ready.acquire()
286 # Module globals are cleared before __del__ is run
287 # So we save the functions in class dict
288 class C:
289 ensure = ctypes.pythonapi.PyGILState_Ensure
290 release = ctypes.pythonapi.PyGILState_Release
291 def __del__(self):
292 state = self.ensure()
293 self.release(state)
295 def waitingThread():
296 x = C()
297 ready.release()
298 time.sleep(100)
300 thread.start_new_thread(waitingThread, ())
301 ready.acquire() # Be sure the other thread is waiting.
302 sys.exit(42)
303 """])
304 self.assertEqual(rc, 42)
306 def test_finalize_with_trace(self):
307 # Issue1733757
308 # Avoid a deadlock when sys.settrace steps into threading._shutdown
309 import subprocess
310 rc = subprocess.call([sys.executable, "-c", """if 1:
311 import sys, threading
313 # A deadlock-killer, to prevent the
314 # testsuite to hang forever
315 def killer():
316 import os, time
317 time.sleep(2)
318 print 'program blocked; aborting'
319 os._exit(2)
320 t = threading.Thread(target=killer)
321 t.daemon = True
322 t.start()
324 # This is the trace function
325 def func(frame, event, arg):
326 threading.current_thread()
327 return func
329 sys.settrace(func)
330 """])
331 self.assertFalse(rc == 2, "interpreted was blocked")
332 self.assertTrue(rc == 0, "Unexpected error")
334 def test_join_nondaemon_on_shutdown(self):
335 # Issue 1722344
336 # Raising SystemExit skipped threading._shutdown
337 import subprocess
338 p = subprocess.Popen([sys.executable, "-c", """if 1:
339 import threading
340 from time import sleep
342 def child():
343 sleep(1)
344 # As a non-daemon thread we SHOULD wake up and nothing
345 # should be torn down yet
346 print "Woke up, sleep function is:", sleep
348 threading.Thread(target=child).start()
349 raise SystemExit
350 """],
351 stdout=subprocess.PIPE,
352 stderr=subprocess.PIPE)
353 stdout, stderr = p.communicate()
354 self.assertEqual(stdout.strip(),
355 "Woke up, sleep function is: <built-in function sleep>")
356 stderr = re.sub(r"^\[\d+ refs\]", "", stderr, re.MULTILINE).strip()
357 self.assertEqual(stderr, "")
359 def test_enumerate_after_join(self):
360 # Try hard to trigger #1703448: a thread is still returned in
361 # threading.enumerate() after it has been join()ed.
362 enum = threading.enumerate
363 old_interval = sys.getcheckinterval()
364 try:
365 for i in xrange(1, 100):
366 # Try a couple times at each thread-switching interval
367 # to get more interleavings.
368 sys.setcheckinterval(i // 5)
369 t = threading.Thread(target=lambda: None)
370 t.start()
371 t.join()
372 l = enum()
373 self.assertNotIn(t, l,
374 "#1703448 triggered after %d trials: %s" % (i, l))
375 finally:
376 sys.setcheckinterval(old_interval)
378 def test_no_refcycle_through_target(self):
379 class RunSelfFunction(object):
380 def __init__(self, should_raise):
381 # The links in this refcycle from Thread back to self
382 # should be cleaned up when the thread completes.
383 self.should_raise = should_raise
384 self.thread = threading.Thread(target=self._run,
385 args=(self,),
386 kwargs={'yet_another':self})
387 self.thread.start()
389 def _run(self, other_ref, yet_another):
390 if self.should_raise:
391 raise SystemExit
393 cyclic_object = RunSelfFunction(should_raise=False)
394 weak_cyclic_object = weakref.ref(cyclic_object)
395 cyclic_object.thread.join()
396 del cyclic_object
397 self.assertEquals(None, weak_cyclic_object(),
398 msg=('%d references still around' %
399 sys.getrefcount(weak_cyclic_object())))
401 raising_cyclic_object = RunSelfFunction(should_raise=True)
402 weak_raising_cyclic_object = weakref.ref(raising_cyclic_object)
403 raising_cyclic_object.thread.join()
404 del raising_cyclic_object
405 self.assertEquals(None, weak_raising_cyclic_object(),
406 msg=('%d references still around' %
407 sys.getrefcount(weak_raising_cyclic_object())))
410 class ThreadJoinOnShutdown(BaseTestCase):
412 def _run_and_join(self, script):
413 script = """if 1:
414 import sys, os, time, threading
416 # a thread, which waits for the main program to terminate
417 def joiningfunc(mainthread):
418 mainthread.join()
419 print 'end of thread'
420 \n""" + script
422 import subprocess
423 p = subprocess.Popen([sys.executable, "-c", script], stdout=subprocess.PIPE)
424 rc = p.wait()
425 data = p.stdout.read().replace('\r', '')
426 self.assertEqual(data, "end of main\nend of thread\n")
427 self.assertFalse(rc == 2, "interpreter was blocked")
428 self.assertTrue(rc == 0, "Unexpected error")
430 def test_1_join_on_shutdown(self):
431 # The usual case: on exit, wait for a non-daemon thread
432 script = """if 1:
433 import os
434 t = threading.Thread(target=joiningfunc,
435 args=(threading.current_thread(),))
436 t.start()
437 time.sleep(0.1)
438 print 'end of main'
440 self._run_and_join(script)
443 def test_2_join_in_forked_process(self):
444 # Like the test above, but from a forked interpreter
445 import os
446 if not hasattr(os, 'fork'):
447 return
448 script = """if 1:
449 childpid = os.fork()
450 if childpid != 0:
451 os.waitpid(childpid, 0)
452 sys.exit(0)
454 t = threading.Thread(target=joiningfunc,
455 args=(threading.current_thread(),))
456 t.start()
457 print 'end of main'
459 self._run_and_join(script)
461 def test_3_join_in_forked_from_thread(self):
462 # Like the test above, but fork() was called from a worker thread
463 # In the forked process, the main Thread object must be marked as stopped.
464 import os
465 if not hasattr(os, 'fork'):
466 return
467 # Skip platforms with known problems forking from a worker thread.
468 # See http://bugs.python.org/issue3863.
469 if sys.platform in ('freebsd4', 'freebsd5', 'freebsd6', 'os2emx'):
470 print >>sys.stderr, ('Skipping test_3_join_in_forked_from_thread'
471 ' due to known OS bugs on'), sys.platform
472 return
473 script = """if 1:
474 main_thread = threading.current_thread()
475 def worker():
476 childpid = os.fork()
477 if childpid != 0:
478 os.waitpid(childpid, 0)
479 sys.exit(0)
481 t = threading.Thread(target=joiningfunc,
482 args=(main_thread,))
483 print 'end of main'
484 t.start()
485 t.join() # Should not block: main_thread is already stopped
487 w = threading.Thread(target=worker)
488 w.start()
490 self._run_and_join(script)
493 class ThreadingExceptionTests(BaseTestCase):
494 # A RuntimeError should be raised if Thread.start() is called
495 # multiple times.
496 def test_start_thread_again(self):
497 thread = threading.Thread()
498 thread.start()
499 self.assertRaises(RuntimeError, thread.start)
501 def test_joining_current_thread(self):
502 current_thread = threading.current_thread()
503 self.assertRaises(RuntimeError, current_thread.join);
505 def test_joining_inactive_thread(self):
506 thread = threading.Thread()
507 self.assertRaises(RuntimeError, thread.join)
509 def test_daemonize_active_thread(self):
510 thread = threading.Thread()
511 thread.start()
512 self.assertRaises(RuntimeError, setattr, thread, "daemon", True)
515 class LockTests(lock_tests.LockTests):
516 locktype = staticmethod(threading.Lock)
518 class RLockTests(lock_tests.RLockTests):
519 locktype = staticmethod(threading.RLock)
521 class EventTests(lock_tests.EventTests):
522 eventtype = staticmethod(threading.Event)
524 class ConditionAsRLockTests(lock_tests.RLockTests):
525 # An Condition uses an RLock by default and exports its API.
526 locktype = staticmethod(threading.Condition)
528 class ConditionTests(lock_tests.ConditionTests):
529 condtype = staticmethod(threading.Condition)
531 class SemaphoreTests(lock_tests.SemaphoreTests):
532 semtype = staticmethod(threading.Semaphore)
534 class BoundedSemaphoreTests(lock_tests.BoundedSemaphoreTests):
535 semtype = staticmethod(threading.BoundedSemaphore)
538 def test_main():
539 test.test_support.run_unittest(LockTests, RLockTests, EventTests,
540 ConditionAsRLockTests, ConditionTests,
541 SemaphoreTests, BoundedSemaphoreTests,
542 ThreadTests,
543 ThreadJoinOnShutdown,
544 ThreadingExceptionTests,
547 if __name__ == "__main__":
548 test_main()