1 # Very rudimentary test of threading module
3 import test
.test_support
4 from test
.test_support
import verbose
8 thread
= test
.test_support
.import_module('thread')
9 threading
= test
.test_support
.import_module('threading')
16 from test
import lock_tests
18 # A trivial mutable counter.
19 class Counter(object):
29 class TestThread(threading
.Thread
):
30 def __init__(self
, name
, testcase
, sema
, mutex
, nrunning
):
31 threading
.Thread
.__init
__(self
, name
=name
)
32 self
.testcase
= testcase
35 self
.nrunning
= nrunning
38 delay
= random
.random() / 10000.0
40 print 'task %s will run for %.1f usec' % (
41 self
.name
, delay
* 1e6
)
47 print self
.nrunning
.get(), 'tasks are running'
48 self
.testcase
.assertTrue(self
.nrunning
.get() <= 3)
52 print 'task', self
.name
, 'done'
56 self
.testcase
.assertTrue(self
.nrunning
.get() >= 0)
58 print '%s is finished. %d tasks are running' % (
59 self
.name
, self
.nrunning
.get())
61 class BaseTestCase(unittest
.TestCase
):
63 self
._threads
= test
.test_support
.threading_setup()
66 test
.test_support
.threading_cleanup(*self
._threads
)
67 test
.test_support
.reap_children()
70 class ThreadTests(BaseTestCase
):
72 # Create a bunch of threads, let each do some work, wait until all are
74 def test_various_ops(self
):
75 # This takes about n/3 seconds to run (about n/3 clumps of tasks,
76 # times about 1 second per clump).
79 # no more than 3 of the 10 can run at once
80 sema
= threading
.BoundedSemaphore(value
=3)
81 mutex
= threading
.RLock()
82 numrunning
= Counter()
86 for i
in range(NUMTASKS
):
87 t
= TestThread("<thread %d>"%i, self
, sema
, mutex
, numrunning
)
89 self
.assertEqual(t
.ident
, None)
90 self
.assertTrue(re
.match('<TestThread\(.*, initial\)>', repr(t
)))
94 print 'waiting for all tasks to complete'
97 self
.assertTrue(not t
.is_alive())
98 self
.assertNotEqual(t
.ident
, 0)
99 self
.assertFalse(t
.ident
is None)
100 self
.assertTrue(re
.match('<TestThread\(.*, \w+ -?\d+\)>', repr(t
)))
102 print 'all tasks done'
103 self
.assertEqual(numrunning
.get(), 0)
105 def test_ident_of_no_threading_threads(self
):
106 # The ident still must work for the main thread and dummy threads.
107 self
.assertFalse(threading
.currentThread().ident
is None)
109 ident
.append(threading
.currentThread().ident
)
111 done
= threading
.Event()
113 thread
.start_new_thread(f
, ())
115 self
.assertFalse(ident
[0] is None)
116 # Kill the "immortal" _DummyThread
117 del threading
._active
[ident
[0]]
119 # run with a small(ish) thread stack size (256kB)
120 def test_various_ops_small_stack(self
):
122 print 'with 256kB thread stack size...'
124 threading
.stack_size(262144)
127 print 'platform does not support changing thread stack size'
129 self
.test_various_ops()
130 threading
.stack_size(0)
132 # run with a large thread stack size (1MB)
133 def test_various_ops_large_stack(self
):
135 print 'with 1MB thread stack size...'
137 threading
.stack_size(0x100000)
140 print 'platform does not support changing thread stack size'
142 self
.test_various_ops()
143 threading
.stack_size(0)
145 def test_foreign_thread(self
):
146 # Check that a "foreign" thread can use the threading module.
148 # Calling current_thread() forces an entry for the foreign
149 # thread to get made in the threading._active map.
150 threading
.current_thread()
153 mutex
= threading
.Lock()
155 tid
= thread
.start_new_thread(f
, (mutex
,))
156 # Wait for the thread to finish.
158 self
.assertIn(tid
, threading
._active
)
159 self
.assertIsInstance(threading
._active
[tid
], threading
._DummyThread
)
160 del threading
._active
[tid
]
162 # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently)
163 # exposed at the Python level. This test relies on ctypes to get at it.
164 def test_PyThreadState_SetAsyncExc(self
):
169 print "test_PyThreadState_SetAsyncExc can't import ctypes"
170 return # can't do anything
172 set_async_exc
= ctypes
.pythonapi
.PyThreadState_SetAsyncExc
174 class AsyncExc(Exception):
177 exception
= ctypes
.py_object(AsyncExc
)
179 # First check it works when setting the exception from the same thread.
180 tid
= thread
.get_ident()
183 result
= set_async_exc(ctypes
.c_long(tid
), exception
)
184 # The exception is async, so we might have to keep the VM busy until
191 # This code is unreachable but it reflects the intent. If we wanted
192 # to be smarter the above loop wouldn't be infinite.
193 self
.fail("AsyncExc not raised")
195 self
.assertEqual(result
, 1) # one thread state modified
196 except UnboundLocalError:
197 # The exception was raised too quickly for us to get the result.
200 # `worker_started` is set by the thread when it's inside a try/except
201 # block waiting to catch the asynchronously set AsyncExc exception.
202 # `worker_saw_exception` is set by the thread upon catching that
204 worker_started
= threading
.Event()
205 worker_saw_exception
= threading
.Event()
207 class Worker(threading
.Thread
):
209 self
.id = thread
.get_ident()
210 self
.finished
= False
218 worker_saw_exception
.set()
221 t
.daemon
= True # so if this fails, we don't hang Python at shutdown
224 print " started worker thread"
226 # Try a thread id that doesn't make sense.
228 print " trying nonsensical thread id"
229 result
= set_async_exc(ctypes
.c_long(-1), exception
)
230 self
.assertEqual(result
, 0) # no thread states modified
232 # Now raise an exception in the worker thread.
234 print " waiting for worker thread to get started"
235 ret
= worker_started
.wait()
238 print " verifying worker hasn't exited"
239 self
.assertTrue(not t
.finished
)
241 print " attempting to raise asynch exception in worker"
242 result
= set_async_exc(ctypes
.c_long(t
.id), exception
)
243 self
.assertEqual(result
, 1) # one thread state modified
245 print " waiting for worker to say it caught the exception"
246 worker_saw_exception
.wait(timeout
=10)
247 self
.assertTrue(t
.finished
)
249 print " all OK -- joining worker"
252 # else the thread is still running, and we have no way to kill it
254 def test_limbo_cleanup(self
):
255 # Issue 7481: Failure to start thread should cleanup the limbo map.
256 def fail_new_thread(*args
):
258 _start_new_thread
= threading
._start
_new
_thread
259 threading
._start
_new
_thread
= fail_new_thread
261 t
= threading
.Thread(target
=lambda: None)
262 self
.assertRaises(thread
.error
, t
.start
)
264 t
in threading
._limbo
,
265 "Failed to cleanup _limbo map on failure of Thread.start().")
267 threading
._start
_new
_thread
= _start_new_thread
269 def test_finalize_runnning_thread(self
):
270 # Issue 1402: the PyGILState_Ensure / _Release functions may be called
271 # very late on python exit: on deallocation of a running thread for
277 print("test_finalize_with_runnning_thread can't import ctypes")
278 return # can't do anything
280 rc
= subprocess
.call([sys
.executable
, "-c", """if 1:
281 import ctypes, sys, time, thread
283 # This lock is used as a simple event variable.
284 ready = thread.allocate_lock()
287 # Module globals are cleared before __del__ is run
288 # So we save the functions in class dict
290 ensure = ctypes.pythonapi.PyGILState_Ensure
291 release = ctypes.pythonapi.PyGILState_Release
293 state = self.ensure()
301 thread.start_new_thread(waitingThread, ())
302 ready.acquire() # Be sure the other thread is waiting.
305 self
.assertEqual(rc
, 42)
307 def test_finalize_with_trace(self
):
309 # Avoid a deadlock when sys.settrace steps into threading._shutdown
310 p
= subprocess
.Popen([sys
.executable
, "-c", """if 1:
311 import sys, threading
313 # A deadlock-killer, to prevent the
314 # testsuite to hang forever
318 print 'program blocked; aborting'
320 t = threading.Thread(target=killer)
324 # This is the trace function
325 def func(frame, event, arg):
326 threading.current_thread()
331 stdout
=subprocess
.PIPE
,
332 stderr
=subprocess
.PIPE
)
333 self
.addCleanup(p
.stdout
.close
)
334 self
.addCleanup(p
.stderr
.close
)
335 stdout
, stderr
= p
.communicate()
337 self
.assertFalse(rc
== 2, "interpreted was blocked")
338 self
.assertTrue(rc
== 0,
339 "Unexpected error: " + repr(stderr
))
341 def test_join_nondaemon_on_shutdown(self
):
343 # Raising SystemExit skipped threading._shutdown
344 p
= subprocess
.Popen([sys
.executable
, "-c", """if 1:
346 from time import sleep
350 # As a non-daemon thread we SHOULD wake up and nothing
351 # should be torn down yet
352 print "Woke up, sleep function is:", sleep
354 threading.Thread(target=child).start()
357 stdout
=subprocess
.PIPE
,
358 stderr
=subprocess
.PIPE
)
359 self
.addCleanup(p
.stdout
.close
)
360 self
.addCleanup(p
.stderr
.close
)
361 stdout
, stderr
= p
.communicate()
362 self
.assertEqual(stdout
.strip(),
363 "Woke up, sleep function is: <built-in function sleep>")
364 stderr
= re
.sub(r
"^\[\d+ refs\]", "", stderr
, re
.MULTILINE
).strip()
365 self
.assertEqual(stderr
, "")
367 def test_enumerate_after_join(self
):
368 # Try hard to trigger #1703448: a thread is still returned in
369 # threading.enumerate() after it has been join()ed.
370 enum
= threading
.enumerate
371 old_interval
= sys
.getcheckinterval()
373 for i
in xrange(1, 100):
374 # Try a couple times at each thread-switching interval
375 # to get more interleavings.
376 sys
.setcheckinterval(i
// 5)
377 t
= threading
.Thread(target
=lambda: None)
381 self
.assertNotIn(t
, l
,
382 "#1703448 triggered after %d trials: %s" % (i
, l
))
384 sys
.setcheckinterval(old_interval
)
386 def test_no_refcycle_through_target(self
):
387 class RunSelfFunction(object):
388 def __init__(self
, should_raise
):
389 # The links in this refcycle from Thread back to self
390 # should be cleaned up when the thread completes.
391 self
.should_raise
= should_raise
392 self
.thread
= threading
.Thread(target
=self
._run
,
394 kwargs
={'yet_another':self
})
397 def _run(self
, other_ref
, yet_another
):
398 if self
.should_raise
:
401 cyclic_object
= RunSelfFunction(should_raise
=False)
402 weak_cyclic_object
= weakref
.ref(cyclic_object
)
403 cyclic_object
.thread
.join()
405 self
.assertEqual(None, weak_cyclic_object(),
406 msg
=('%d references still around' %
407 sys
.getrefcount(weak_cyclic_object())))
409 raising_cyclic_object
= RunSelfFunction(should_raise
=True)
410 weak_raising_cyclic_object
= weakref
.ref(raising_cyclic_object
)
411 raising_cyclic_object
.thread
.join()
412 del raising_cyclic_object
413 self
.assertEqual(None, weak_raising_cyclic_object(),
414 msg
=('%d references still around' %
415 sys
.getrefcount(weak_raising_cyclic_object())))
418 class ThreadJoinOnShutdown(BaseTestCase
):
420 def _run_and_join(self
, script
):
422 import sys, os, time, threading
424 # a thread, which waits for the main program to terminate
425 def joiningfunc(mainthread):
427 print 'end of thread'
430 p
= subprocess
.Popen([sys
.executable
, "-c", script
], stdout
=subprocess
.PIPE
)
432 data
= p
.stdout
.read().replace('\r', '')
434 self
.assertEqual(data
, "end of main\nend of thread\n")
435 self
.assertFalse(rc
== 2, "interpreter was blocked")
436 self
.assertTrue(rc
== 0, "Unexpected error")
438 def test_1_join_on_shutdown(self
):
439 # The usual case: on exit, wait for a non-daemon thread
442 t = threading.Thread(target=joiningfunc,
443 args=(threading.current_thread(),))
448 self
._run
_and
_join
(script
)
451 def test_2_join_in_forked_process(self
):
452 # Like the test above, but from a forked interpreter
454 if not hasattr(os
, 'fork'):
459 os.waitpid(childpid, 0)
462 t = threading.Thread(target=joiningfunc,
463 args=(threading.current_thread(),))
467 self
._run
_and
_join
(script
)
469 def test_3_join_in_forked_from_thread(self
):
470 # Like the test above, but fork() was called from a worker thread
471 # In the forked process, the main Thread object must be marked as stopped.
473 if not hasattr(os
, 'fork'):
475 # Skip platforms with known problems forking from a worker thread.
476 # See http://bugs.python.org/issue3863.
477 if sys
.platform
in ('freebsd4', 'freebsd5', 'freebsd6', 'netbsd5',
479 print >>sys
.stderr
, ('Skipping test_3_join_in_forked_from_thread'
480 ' due to known OS bugs on'), sys
.platform
483 main_thread = threading.current_thread()
487 os.waitpid(childpid, 0)
490 t = threading.Thread(target=joiningfunc,
494 t.join() # Should not block: main_thread is already stopped
496 w = threading.Thread(target=worker)
499 self
._run
_and
_join
(script
)
501 def assertScriptHasOutput(self
, script
, expected_output
):
502 p
= subprocess
.Popen([sys
.executable
, "-c", script
],
503 stdout
=subprocess
.PIPE
)
505 data
= p
.stdout
.read().decode().replace('\r', '')
506 self
.assertEqual(rc
, 0, "Unexpected error")
507 self
.assertEqual(data
, expected_output
)
509 @unittest.skipUnless(hasattr(os
, 'fork'), "needs os.fork()")
510 def test_4_joining_across_fork_in_worker_thread(self
):
511 # There used to be a possible deadlock when forking from a child
512 # thread. See http://bugs.python.org/issue6643.
514 # Skip platforms with known problems forking from a worker thread.
515 # See http://bugs.python.org/issue3863.
516 if sys
.platform
in ('freebsd4', 'freebsd5', 'freebsd6', 'os2emx'):
517 raise unittest
.SkipTest('due to known OS bugs on ' + sys
.platform
)
519 # The script takes the following steps:
520 # - The main thread in the parent process starts a new thread and then
522 # - The join operation acquires the Lock inside the thread's _block
523 # Condition. (See threading.py:Thread.join().)
524 # - We stub out the acquire method on the condition to force it to wait
525 # until the child thread forks. (See LOCK ACQUIRED HERE)
526 # - The child thread forks. (See LOCK HELD and WORKER THREAD FORKS
528 # - The main thread of the parent process enters Condition.wait(),
529 # which releases the lock on the child thread.
530 # - The child process returns. Without the necessary fix, when the
531 # main thread of the child process (which used to be the child thread
532 # in the parent process) attempts to exit, it will try to acquire the
533 # lock in the Thread._block Condition object and hang, because the
534 # lock was held across the fork.
537 import os, time, threading
543 # Wait until this thread's lock is acquired before forking to
544 # create the deadlock.
546 while not start_fork:
548 # LOCK HELD: Main thread holds lock across this call.
552 # Parent process just waits for child.
553 os.waitpid(childpid, 0)
554 # Child process should just return.
556 w = threading.Thread(target=worker)
558 # Stub out the private condition variable's lock acquire method.
559 # This acquires the lock and then waits until the child has forked
560 # before returning, which will release the lock soon after. If
561 # someone else tries to fix this test case by acquiring this lock
562 # before forking instead of resetting it, the test case will
563 # deadlock when it shouldn't.
565 orig_acquire = condition.acquire
566 call_count_lock = threading.Lock()
571 orig_acquire() # LOCK ACQUIRED HERE
574 while not finish_join:
575 time.sleep(0.01) # WORKER THREAD FORKS HERE
576 with call_count_lock:
578 condition.acquire = my_acquire
584 self
.assertScriptHasOutput(script
, "end of main\n")
586 @unittest.skipUnless(hasattr(os
, 'fork'), "needs os.fork()")
587 def test_5_clear_waiter_locks_to_avoid_crash(self
):
588 # Check that a spawned thread that forks doesn't segfault on certain
589 # platforms, namely OS X. This used to happen if there was a waiter
590 # lock in the thread's condition variable's waiters list. Even though
591 # we know the lock will be held across the fork, it is not safe to
592 # release locks held across forks on all platforms, so releasing the
593 # waiter lock caused a segfault on OS X. Furthermore, since locks on
594 # OS X are (as of this writing) implemented with a mutex + condition
595 # variable instead of a semaphore, while we know that the Python-level
596 # lock will be acquired, we can't know if the internal mutex will be
597 # acquired at the time of the fork.
599 # Skip platforms with known problems forking from a worker thread.
600 # See http://bugs.python.org/issue3863.
601 if sys
.platform
in ('freebsd4', 'freebsd5', 'freebsd6', 'os2emx'):
602 raise unittest
.SkipTest('due to known OS bugs on ' + sys
.platform
)
604 import os, time, threading
609 # Wait until the main thread has attempted to join this thread
611 while not start_fork:
615 # Parent process just waits for child.
616 (cpid, rc) = os.waitpid(childpid, 0)
617 assert cpid == childpid
619 print('end of worker thread')
621 # Child process should just return.
624 w = threading.Thread(target=worker)
626 # Stub out the private condition variable's _release_save method.
627 # This releases the condition's lock and flips the global that
628 # causes the worker to fork. At this point, the problematic waiter
629 # lock has been acquired once by the waiter and has been put onto
632 orig_release_save = condition._release_save
633 def my_release_save():
636 # Waiter lock held here, condition lock released.
638 condition._release_save = my_release_save
642 print('end of main thread')
644 output
= "end of worker thread\nend of main thread\n"
645 self
.assertScriptHasOutput(script
, output
)
648 class ThreadingExceptionTests(BaseTestCase
):
649 # A RuntimeError should be raised if Thread.start() is called
651 def test_start_thread_again(self
):
652 thread
= threading
.Thread()
654 self
.assertRaises(RuntimeError, thread
.start
)
656 def test_joining_current_thread(self
):
657 current_thread
= threading
.current_thread()
658 self
.assertRaises(RuntimeError, current_thread
.join
);
660 def test_joining_inactive_thread(self
):
661 thread
= threading
.Thread()
662 self
.assertRaises(RuntimeError, thread
.join
)
664 def test_daemonize_active_thread(self
):
665 thread
= threading
.Thread()
667 self
.assertRaises(RuntimeError, setattr, thread
, "daemon", True)
670 class LockTests(lock_tests
.LockTests
):
671 locktype
= staticmethod(threading
.Lock
)
673 class RLockTests(lock_tests
.RLockTests
):
674 locktype
= staticmethod(threading
.RLock
)
676 class EventTests(lock_tests
.EventTests
):
677 eventtype
= staticmethod(threading
.Event
)
679 class ConditionAsRLockTests(lock_tests
.RLockTests
):
680 # An Condition uses an RLock by default and exports its API.
681 locktype
= staticmethod(threading
.Condition
)
683 class ConditionTests(lock_tests
.ConditionTests
):
684 condtype
= staticmethod(threading
.Condition
)
686 class SemaphoreTests(lock_tests
.SemaphoreTests
):
687 semtype
= staticmethod(threading
.Semaphore
)
689 class BoundedSemaphoreTests(lock_tests
.BoundedSemaphoreTests
):
690 semtype
= staticmethod(threading
.BoundedSemaphore
)
692 @unittest.skipUnless(sys
.platform
== 'darwin', 'test macosx problem')
693 def test_recursion_limit(self
):
695 # test that excessive recursion within a non-main thread causes
696 # an exception rather than crashing the interpreter on platforms
697 # like Mac OS X or FreeBSD which have small default stack sizes
711 w = threading.Thread(target=outer)
714 print('end of main thread')
716 expected_output
= "end of main thread\n"
717 p
= subprocess
.Popen([sys
.executable
, "-c", script
],
718 stdout
=subprocess
.PIPE
)
719 stdout
, stderr
= p
.communicate()
720 data
= stdout
.decode().replace('\r', '')
721 self
.assertEqual(p
.returncode
, 0, "Unexpected error")
722 self
.assertEqual(data
, expected_output
)
725 test
.test_support
.run_unittest(LockTests
, RLockTests
, EventTests
,
726 ConditionAsRLockTests
, ConditionTests
,
727 SemaphoreTests
, BoundedSemaphoreTests
,
729 ThreadJoinOnShutdown
,
730 ThreadingExceptionTests
,
733 if __name__
== "__main__":