Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 1 | # Very rudimentary test of threading module |
| 2 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | import test.support |
Antoine Pitrou | 62f68ed | 2010-08-04 11:48:56 +0000 | [diff] [blame] | 4 | from test.support import verbose, strip_python_stderr |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 5 | import random |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 6 | import re |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 7 | import sys |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 8 | _thread = test.support.import_module('_thread') |
| 9 | threading = test.support.import_module('threading') |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 10 | import time |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 11 | import unittest |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 12 | import weakref |
Alexandre Vassalotti | 93f2cd2 | 2009-07-22 04:54:52 +0000 | [diff] [blame] | 13 | import os |
Gregory P. Smith | 96c886c | 2011-01-03 21:06:12 +0000 | [diff] [blame] | 14 | import subprocess |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 15 | |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 16 | from test import lock_tests |
| 17 | |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 18 | # A trivial mutable counter. |
| 19 | class Counter(object): |
| 20 | def __init__(self): |
| 21 | self.value = 0 |
| 22 | def inc(self): |
| 23 | self.value += 1 |
| 24 | def dec(self): |
| 25 | self.value -= 1 |
| 26 | def get(self): |
| 27 | return self.value |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 28 | |
| 29 | class TestThread(threading.Thread): |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 30 | def __init__(self, name, testcase, sema, mutex, nrunning): |
| 31 | threading.Thread.__init__(self, name=name) |
| 32 | self.testcase = testcase |
| 33 | self.sema = sema |
| 34 | self.mutex = mutex |
| 35 | self.nrunning = nrunning |
| 36 | |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 37 | def run(self): |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 38 | delay = random.random() / 10000.0 |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 39 | if verbose: |
Jeffrey Yasskin | ca67412 | 2008-03-29 05:06:52 +0000 | [diff] [blame] | 40 | print('task %s will run for %.1f usec' % |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 41 | (self.name, delay * 1e6)) |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 42 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 43 | with self.sema: |
| 44 | with self.mutex: |
| 45 | self.nrunning.inc() |
| 46 | if verbose: |
| 47 | print(self.nrunning.get(), 'tasks are running') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 48 | self.testcase.assertTrue(self.nrunning.get() <= 3) |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 49 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 50 | time.sleep(delay) |
| 51 | if verbose: |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 52 | print('task', self.name, 'done') |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 53 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 54 | with self.mutex: |
| 55 | self.nrunning.dec() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 56 | self.testcase.assertTrue(self.nrunning.get() >= 0) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 57 | if verbose: |
| 58 | print('%s is finished. %d tasks are running' % |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 59 | (self.name, self.nrunning.get())) |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 60 | |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 61 | |
Antoine Pitrou | b0e9bd4 | 2009-10-27 20:05:26 +0000 | [diff] [blame] | 62 | class BaseTestCase(unittest.TestCase): |
| 63 | def setUp(self): |
| 64 | self._threads = test.support.threading_setup() |
| 65 | |
| 66 | def tearDown(self): |
| 67 | test.support.threading_cleanup(*self._threads) |
| 68 | test.support.reap_children() |
| 69 | |
| 70 | |
| 71 | class ThreadTests(BaseTestCase): |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 72 | |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 73 | # Create a bunch of threads, let each do some work, wait until all are |
| 74 | # done. |
| 75 | def test_various_ops(self): |
| 76 | # This takes about n/3 seconds to run (about n/3 clumps of tasks, |
| 77 | # times about 1 second per clump). |
| 78 | NUMTASKS = 10 |
| 79 | |
| 80 | # no more than 3 of the 10 can run at once |
| 81 | sema = threading.BoundedSemaphore(value=3) |
| 82 | mutex = threading.RLock() |
| 83 | numrunning = Counter() |
| 84 | |
| 85 | threads = [] |
| 86 | |
| 87 | for i in range(NUMTASKS): |
| 88 | t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning) |
| 89 | threads.append(t) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 90 | self.assertEqual(t.ident, None) |
| 91 | self.assertTrue(re.match('<TestThread\(.*, initial\)>', repr(t))) |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 92 | t.start() |
| 93 | |
| 94 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 95 | print('waiting for all tasks to complete') |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 96 | for t in threads: |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 97 | t.join(NUMTASKS) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 98 | self.assertTrue(not t.is_alive()) |
| 99 | self.assertNotEqual(t.ident, 0) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 100 | self.assertFalse(t.ident is None) |
Brett Cannon | 3f5f226 | 2010-07-23 15:50:52 +0000 | [diff] [blame] | 101 | self.assertTrue(re.match('<TestThread\(.*, stopped -?\d+\)>', |
| 102 | repr(t))) |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 103 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 104 | print('all tasks done') |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 105 | self.assertEqual(numrunning.get(), 0) |
| 106 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 107 | def test_ident_of_no_threading_threads(self): |
| 108 | # The ident still must work for the main thread and dummy threads. |
| 109 | self.assertFalse(threading.currentThread().ident is None) |
| 110 | def f(): |
| 111 | ident.append(threading.currentThread().ident) |
| 112 | done.set() |
| 113 | done = threading.Event() |
| 114 | ident = [] |
| 115 | _thread.start_new_thread(f, ()) |
| 116 | done.wait() |
| 117 | self.assertFalse(ident[0] is None) |
Antoine Pitrou | ca13a0d | 2009-11-08 00:30:04 +0000 | [diff] [blame] | 118 | # Kill the "immortal" _DummyThread |
| 119 | del threading._active[ident[0]] |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 120 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 121 | # run with a small(ish) thread stack size (256kB) |
| 122 | def test_various_ops_small_stack(self): |
| 123 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 124 | print('with 256kB thread stack size...') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 125 | try: |
| 126 | threading.stack_size(262144) |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 127 | except _thread.error: |
Alexandre Vassalotti | 93f2cd2 | 2009-07-22 04:54:52 +0000 | [diff] [blame] | 128 | raise unittest.SkipTest( |
| 129 | 'platform does not support changing thread stack size') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 130 | self.test_various_ops() |
| 131 | threading.stack_size(0) |
| 132 | |
| 133 | # run with a large thread stack size (1MB) |
| 134 | def test_various_ops_large_stack(self): |
| 135 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 136 | print('with 1MB thread stack size...') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 137 | try: |
| 138 | threading.stack_size(0x100000) |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 139 | except _thread.error: |
Alexandre Vassalotti | 93f2cd2 | 2009-07-22 04:54:52 +0000 | [diff] [blame] | 140 | raise unittest.SkipTest( |
| 141 | 'platform does not support changing thread stack size') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 142 | self.test_various_ops() |
| 143 | threading.stack_size(0) |
| 144 | |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 145 | def test_foreign_thread(self): |
| 146 | # Check that a "foreign" thread can use the threading module. |
| 147 | def f(mutex): |
Antoine Pitrou | b087268 | 2009-11-09 16:08:16 +0000 | [diff] [blame] | 148 | # Calling current_thread() forces an entry for the foreign |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 149 | # thread to get made in the threading._active map. |
Antoine Pitrou | b087268 | 2009-11-09 16:08:16 +0000 | [diff] [blame] | 150 | threading.current_thread() |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 151 | mutex.release() |
| 152 | |
| 153 | mutex = threading.Lock() |
| 154 | mutex.acquire() |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 155 | tid = _thread.start_new_thread(f, (mutex,)) |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 156 | # Wait for the thread to finish. |
| 157 | mutex.acquire() |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 158 | self.assertIn(tid, threading._active) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 159 | self.assertIsInstance(threading._active[tid], threading._DummyThread) |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 160 | del threading._active[tid] |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 161 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 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): |
| 165 | try: |
| 166 | import ctypes |
| 167 | except ImportError: |
Alexandre Vassalotti | 93f2cd2 | 2009-07-22 04:54:52 +0000 | [diff] [blame] | 168 | raise unittest.SkipTest("cannot import ctypes") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 169 | |
| 170 | set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc |
| 171 | |
| 172 | class AsyncExc(Exception): |
| 173 | pass |
| 174 | |
| 175 | exception = ctypes.py_object(AsyncExc) |
| 176 | |
Antoine Pitrou | be4d809 | 2009-10-18 18:27:17 +0000 | [diff] [blame] | 177 | # First check it works when setting the exception from the same thread. |
| 178 | tid = _thread.get_ident() |
| 179 | |
| 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: |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 189 | # This code is unreachable but it reflects the intent. If we wanted |
| 190 | # to be smarter the above loop wouldn't be infinite. |
Antoine Pitrou | be4d809 | 2009-10-18 18:27:17 +0000 | [diff] [blame] | 191 | self.fail("AsyncExc not raised") |
| 192 | try: |
| 193 | self.assertEqual(result, 1) # one thread state modified |
| 194 | except UnboundLocalError: |
Benjamin Peterson | a0dfa82 | 2009-11-13 02:25:08 +0000 | [diff] [blame] | 195 | # The exception was raised too quickly for us to get the result. |
Antoine Pitrou | be4d809 | 2009-10-18 18:27:17 +0000 | [diff] [blame] | 196 | pass |
| 197 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 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() |
| 204 | |
| 205 | class Worker(threading.Thread): |
| 206 | def run(self): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 207 | self.id = _thread.get_ident() |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 208 | self.finished = False |
| 209 | |
| 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() |
| 217 | |
| 218 | t = Worker() |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 219 | t.daemon = True # so if this fails, we don't hang Python at shutdown |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 220 | t.start() |
| 221 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 222 | print(" started worker thread") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 223 | |
| 224 | # Try a thread id that doesn't make sense. |
| 225 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 226 | print(" trying nonsensical thread id") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 227 | result = set_async_exc(ctypes.c_long(-1), exception) |
| 228 | self.assertEqual(result, 0) # no thread states modified |
| 229 | |
| 230 | # Now raise an exception in the worker thread. |
| 231 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 232 | print(" waiting for worker thread to get started") |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 233 | ret = worker_started.wait() |
| 234 | self.assertTrue(ret) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 235 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 236 | print(" verifying worker hasn't exited") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 237 | self.assertTrue(not t.finished) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 238 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 239 | print(" attempting to raise asynch exception in worker") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 240 | result = set_async_exc(ctypes.c_long(t.id), exception) |
| 241 | self.assertEqual(result, 1) # one thread state modified |
| 242 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 243 | print(" waiting for worker to say it caught the exception") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 244 | worker_saw_exception.wait(timeout=10) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 245 | self.assertTrue(t.finished) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 246 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 247 | print(" all OK -- joining worker") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 248 | if t.finished: |
| 249 | t.join() |
| 250 | # else the thread is still running, and we have no way to kill it |
| 251 | |
Gregory P. Smith | 3fdd964 | 2010-02-28 18:57:46 +0000 | [diff] [blame] | 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 threading.ThreadError() |
| 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) |
Gregory P. Smith | f50f168 | 2010-03-01 03:13:36 +0000 | [diff] [blame] | 260 | self.assertRaises(threading.ThreadError, t.start) |
| 261 | self.assertFalse( |
| 262 | t in threading._limbo, |
| 263 | "Failed to cleanup _limbo map on failure of Thread.start().") |
Gregory P. Smith | 3fdd964 | 2010-02-28 18:57:46 +0000 | [diff] [blame] | 264 | finally: |
| 265 | threading._start_new_thread = _start_new_thread |
| 266 | |
Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 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: |
Alexandre Vassalotti | 93f2cd2 | 2009-07-22 04:54:52 +0000 | [diff] [blame] | 274 | raise unittest.SkipTest("cannot import ctypes") |
Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 275 | |
Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 276 | rc = subprocess.call([sys.executable, "-c", """if 1: |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 277 | import ctypes, sys, time, _thread |
Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 278 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 279 | # This lock is used as a simple event variable. |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 280 | ready = _thread.allocate_lock() |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 281 | ready.acquire() |
| 282 | |
Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 283 | # Module globals are cleared before __del__ is run |
| 284 | # So we save the functions in class dict |
| 285 | class C: |
| 286 | ensure = ctypes.pythonapi.PyGILState_Ensure |
| 287 | release = ctypes.pythonapi.PyGILState_Release |
| 288 | def __del__(self): |
| 289 | state = self.ensure() |
| 290 | self.release(state) |
| 291 | |
| 292 | def waitingThread(): |
| 293 | x = C() |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 294 | ready.release() |
Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 295 | time.sleep(100) |
| 296 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 297 | _thread.start_new_thread(waitingThread, ()) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 298 | ready.acquire() # Be sure the other thread is waiting. |
Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 299 | sys.exit(42) |
| 300 | """]) |
| 301 | self.assertEqual(rc, 42) |
| 302 | |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 303 | def test_finalize_with_trace(self): |
| 304 | # Issue1733757 |
| 305 | # Avoid a deadlock when sys.settrace steps into threading._shutdown |
Antoine Pitrou | 7c08744 | 2010-09-19 23:28:30 +0000 | [diff] [blame] | 306 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 307 | import sys, threading |
| 308 | |
| 309 | # A deadlock-killer, to prevent the |
| 310 | # testsuite to hang forever |
| 311 | def killer(): |
| 312 | import os, time |
| 313 | time.sleep(2) |
| 314 | print('program blocked; aborting') |
| 315 | os._exit(2) |
| 316 | t = threading.Thread(target=killer) |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 317 | t.daemon = True |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 318 | t.start() |
| 319 | |
| 320 | # This is the trace function |
| 321 | def func(frame, event, arg): |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 322 | threading.current_thread() |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 323 | return func |
| 324 | |
| 325 | sys.settrace(func) |
Antoine Pitrou | 7c08744 | 2010-09-19 23:28:30 +0000 | [diff] [blame] | 326 | """], |
| 327 | stdout=subprocess.PIPE, |
| 328 | stderr=subprocess.PIPE) |
Brian Curtin | de11b18 | 2010-11-05 17:22:46 +0000 | [diff] [blame] | 329 | self.addCleanup(p.stdout.close) |
| 330 | self.addCleanup(p.stderr.close) |
Antoine Pitrou | 7c08744 | 2010-09-19 23:28:30 +0000 | [diff] [blame] | 331 | stdout, stderr = p.communicate() |
| 332 | rc = p.returncode |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 333 | self.assertFalse(rc == 2, "interpreted was blocked") |
Antoine Pitrou | 7c08744 | 2010-09-19 23:28:30 +0000 | [diff] [blame] | 334 | self.assertTrue(rc == 0, |
| 335 | "Unexpected error: " + ascii(stderr)) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 336 | |
Antoine Pitrou | 011bd62 | 2009-10-20 21:52:47 +0000 | [diff] [blame] | 337 | def test_join_nondaemon_on_shutdown(self): |
| 338 | # Issue 1722344 |
| 339 | # Raising SystemExit skipped threading._shutdown |
Antoine Pitrou | 011bd62 | 2009-10-20 21:52:47 +0000 | [diff] [blame] | 340 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 341 | import threading |
| 342 | from time import sleep |
| 343 | |
| 344 | def child(): |
| 345 | sleep(1) |
| 346 | # As a non-daemon thread we SHOULD wake up and nothing |
| 347 | # should be torn down yet |
| 348 | print("Woke up, sleep function is:", sleep) |
| 349 | |
| 350 | threading.Thread(target=child).start() |
| 351 | raise SystemExit |
| 352 | """], |
| 353 | stdout=subprocess.PIPE, |
| 354 | stderr=subprocess.PIPE) |
Brian Curtin | de11b18 | 2010-11-05 17:22:46 +0000 | [diff] [blame] | 355 | self.addCleanup(p.stdout.close) |
| 356 | self.addCleanup(p.stderr.close) |
Antoine Pitrou | 011bd62 | 2009-10-20 21:52:47 +0000 | [diff] [blame] | 357 | stdout, stderr = p.communicate() |
Antoine Pitrou | 899d1c6 | 2009-10-23 21:55:36 +0000 | [diff] [blame] | 358 | self.assertEqual(stdout.strip(), |
| 359 | b"Woke up, sleep function is: <built-in function sleep>") |
Antoine Pitrou | 62f68ed | 2010-08-04 11:48:56 +0000 | [diff] [blame] | 360 | stderr = strip_python_stderr(stderr) |
Antoine Pitrou | 011bd62 | 2009-10-20 21:52:47 +0000 | [diff] [blame] | 361 | self.assertEqual(stderr, b"") |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 362 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 363 | def test_enumerate_after_join(self): |
| 364 | # Try hard to trigger #1703448: a thread is still returned in |
| 365 | # threading.enumerate() after it has been join()ed. |
| 366 | enum = threading.enumerate |
Antoine Pitrou | c3b0757 | 2009-11-13 22:19:19 +0000 | [diff] [blame] | 367 | old_interval = sys.getswitchinterval() |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 368 | try: |
Jeffrey Yasskin | ca67412 | 2008-03-29 05:06:52 +0000 | [diff] [blame] | 369 | for i in range(1, 100): |
Antoine Pitrou | c3b0757 | 2009-11-13 22:19:19 +0000 | [diff] [blame] | 370 | sys.setswitchinterval(i * 0.0002) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 371 | t = threading.Thread(target=lambda: None) |
| 372 | t.start() |
| 373 | t.join() |
| 374 | l = enum() |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 375 | self.assertNotIn(t, l, |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 376 | "#1703448 triggered after %d trials: %s" % (i, l)) |
| 377 | finally: |
Antoine Pitrou | c3b0757 | 2009-11-13 22:19:19 +0000 | [diff] [blame] | 378 | sys.setswitchinterval(old_interval) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 379 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 380 | def test_no_refcycle_through_target(self): |
| 381 | class RunSelfFunction(object): |
| 382 | def __init__(self, should_raise): |
| 383 | # The links in this refcycle from Thread back to self |
| 384 | # should be cleaned up when the thread completes. |
| 385 | self.should_raise = should_raise |
| 386 | self.thread = threading.Thread(target=self._run, |
| 387 | args=(self,), |
| 388 | kwargs={'yet_another':self}) |
| 389 | self.thread.start() |
| 390 | |
| 391 | def _run(self, other_ref, yet_another): |
| 392 | if self.should_raise: |
| 393 | raise SystemExit |
| 394 | |
| 395 | cyclic_object = RunSelfFunction(should_raise=False) |
| 396 | weak_cyclic_object = weakref.ref(cyclic_object) |
| 397 | cyclic_object.thread.join() |
| 398 | del cyclic_object |
Raymond Hettinger | 7beae8a | 2011-01-06 05:34:17 +0000 | [diff] [blame] | 399 | self.assertIsNone(weak_cyclic_object(), |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 400 | msg=('%d references still around' % |
| 401 | sys.getrefcount(weak_cyclic_object()))) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 402 | |
| 403 | raising_cyclic_object = RunSelfFunction(should_raise=True) |
| 404 | weak_raising_cyclic_object = weakref.ref(raising_cyclic_object) |
| 405 | raising_cyclic_object.thread.join() |
| 406 | del raising_cyclic_object |
Raymond Hettinger | 7beae8a | 2011-01-06 05:34:17 +0000 | [diff] [blame] | 407 | self.assertIsNone(weak_raising_cyclic_object(), |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 408 | msg=('%d references still around' % |
| 409 | sys.getrefcount(weak_raising_cyclic_object()))) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 410 | |
Benjamin Peterson | b3085c9 | 2008-09-01 23:09:31 +0000 | [diff] [blame] | 411 | def test_old_threading_api(self): |
| 412 | # Just a quick sanity check to make sure the old method names are |
| 413 | # still present |
Benjamin Peterson | f0923f5 | 2008-08-18 22:10:13 +0000 | [diff] [blame] | 414 | t = threading.Thread() |
Benjamin Peterson | b3085c9 | 2008-09-01 23:09:31 +0000 | [diff] [blame] | 415 | t.isDaemon() |
| 416 | t.setDaemon(True) |
| 417 | t.getName() |
| 418 | t.setName("name") |
| 419 | t.isAlive() |
| 420 | e = threading.Event() |
| 421 | e.isSet() |
| 422 | threading.activeCount() |
Benjamin Peterson | f0923f5 | 2008-08-18 22:10:13 +0000 | [diff] [blame] | 423 | |
Brian Curtin | 81a4a6a | 2010-07-23 16:30:10 +0000 | [diff] [blame] | 424 | def test_repr_daemon(self): |
| 425 | t = threading.Thread() |
| 426 | self.assertFalse('daemon' in repr(t)) |
| 427 | t.daemon = True |
| 428 | self.assertTrue('daemon' in repr(t)) |
Brett Cannon | 3f5f226 | 2010-07-23 15:50:52 +0000 | [diff] [blame] | 429 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 430 | |
Antoine Pitrou | b0e9bd4 | 2009-10-27 20:05:26 +0000 | [diff] [blame] | 431 | class ThreadJoinOnShutdown(BaseTestCase): |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 432 | |
| 433 | def _run_and_join(self, script): |
| 434 | script = """if 1: |
| 435 | import sys, os, time, threading |
| 436 | |
| 437 | # a thread, which waits for the main program to terminate |
| 438 | def joiningfunc(mainthread): |
| 439 | mainthread.join() |
| 440 | print('end of thread') |
Antoine Pitrou | 5fe291f | 2008-09-06 23:00:03 +0000 | [diff] [blame] | 441 | # stdout is fully buffered because not a tty, we have to flush |
| 442 | # before exit. |
| 443 | sys.stdout.flush() |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 444 | \n""" + script |
| 445 | |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 446 | p = subprocess.Popen([sys.executable, "-c", script], stdout=subprocess.PIPE) |
| 447 | rc = p.wait() |
Benjamin Peterson | ad703dc | 2008-07-17 17:02:57 +0000 | [diff] [blame] | 448 | data = p.stdout.read().decode().replace('\r', '') |
Brian Curtin | b68928b | 2010-11-02 03:59:09 +0000 | [diff] [blame] | 449 | p.stdout.close() |
Benjamin Peterson | ad703dc | 2008-07-17 17:02:57 +0000 | [diff] [blame] | 450 | self.assertEqual(data, "end of main\nend of thread\n") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 451 | self.assertFalse(rc == 2, "interpreter was blocked") |
| 452 | self.assertTrue(rc == 0, "Unexpected error") |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 453 | |
| 454 | def test_1_join_on_shutdown(self): |
| 455 | # The usual case: on exit, wait for a non-daemon thread |
| 456 | script = """if 1: |
| 457 | import os |
| 458 | t = threading.Thread(target=joiningfunc, |
| 459 | args=(threading.current_thread(),)) |
| 460 | t.start() |
| 461 | time.sleep(0.1) |
| 462 | print('end of main') |
| 463 | """ |
| 464 | self._run_and_join(script) |
| 465 | |
| 466 | |
Alexandre Vassalotti | 93f2cd2 | 2009-07-22 04:54:52 +0000 | [diff] [blame] | 467 | @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()") |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 468 | def test_2_join_in_forked_process(self): |
| 469 | # Like the test above, but from a forked interpreter |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 470 | script = """if 1: |
| 471 | childpid = os.fork() |
| 472 | if childpid != 0: |
| 473 | os.waitpid(childpid, 0) |
| 474 | sys.exit(0) |
| 475 | |
| 476 | t = threading.Thread(target=joiningfunc, |
| 477 | args=(threading.current_thread(),)) |
| 478 | t.start() |
| 479 | print('end of main') |
| 480 | """ |
| 481 | self._run_and_join(script) |
| 482 | |
Alexandre Vassalotti | 93f2cd2 | 2009-07-22 04:54:52 +0000 | [diff] [blame] | 483 | @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()") |
Antoine Pitrou | 5fe291f | 2008-09-06 23:00:03 +0000 | [diff] [blame] | 484 | def test_3_join_in_forked_from_thread(self): |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 485 | # Like the test above, but fork() was called from a worker thread |
| 486 | # In the forked process, the main Thread object must be marked as stopped. |
Alexandre Vassalotti | 93f2cd2 | 2009-07-22 04:54:52 +0000 | [diff] [blame] | 487 | |
Benjamin Peterson | bcd8ac3 | 2008-10-10 22:20:52 +0000 | [diff] [blame] | 488 | # Skip platforms with known problems forking from a worker thread. |
| 489 | # See http://bugs.python.org/issue3863. |
Gregory P. Smith | feedda2 | 2010-10-17 03:09:12 +0000 | [diff] [blame] | 490 | if sys.platform in ('freebsd4', 'freebsd5', 'freebsd6', 'netbsd5', |
| 491 | 'os2emx'): |
Alexandre Vassalotti | 93f2cd2 | 2009-07-22 04:54:52 +0000 | [diff] [blame] | 492 | raise unittest.SkipTest('due to known OS bugs on ' + sys.platform) |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 493 | script = """if 1: |
| 494 | main_thread = threading.current_thread() |
| 495 | def worker(): |
| 496 | childpid = os.fork() |
| 497 | if childpid != 0: |
| 498 | os.waitpid(childpid, 0) |
| 499 | sys.exit(0) |
| 500 | |
| 501 | t = threading.Thread(target=joiningfunc, |
| 502 | args=(main_thread,)) |
| 503 | print('end of main') |
| 504 | t.start() |
| 505 | t.join() # Should not block: main_thread is already stopped |
| 506 | |
| 507 | w = threading.Thread(target=worker) |
| 508 | w.start() |
| 509 | """ |
| 510 | self._run_and_join(script) |
| 511 | |
Gregory P. Smith | 96c886c | 2011-01-03 21:06:12 +0000 | [diff] [blame] | 512 | def assertScriptHasOutput(self, script, expected_output): |
| 513 | p = subprocess.Popen([sys.executable, "-c", script], |
| 514 | stdout=subprocess.PIPE) |
Victor Stinner | c932b65 | 2011-01-05 03:54:28 +0000 | [diff] [blame] | 515 | stdout, stderr = p.communicate() |
| 516 | data = stdout.decode().replace('\r', '') |
| 517 | self.assertEqual(p.returncode, 0, "Unexpected error") |
Gregory P. Smith | 96c886c | 2011-01-03 21:06:12 +0000 | [diff] [blame] | 518 | self.assertEqual(data, expected_output) |
| 519 | |
| 520 | @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()") |
| 521 | def test_4_joining_across_fork_in_worker_thread(self): |
| 522 | # There used to be a possible deadlock when forking from a child |
| 523 | # thread. See http://bugs.python.org/issue6643. |
| 524 | |
| 525 | # Skip platforms with known problems forking from a worker thread. |
| 526 | # See http://bugs.python.org/issue3863. |
| 527 | if sys.platform in ('freebsd4', 'freebsd5', 'freebsd6', 'os2emx'): |
| 528 | raise unittest.SkipTest('due to known OS bugs on ' + sys.platform) |
| 529 | |
| 530 | # The script takes the following steps: |
| 531 | # - The main thread in the parent process starts a new thread and then |
| 532 | # tries to join it. |
| 533 | # - The join operation acquires the Lock inside the thread's _block |
| 534 | # Condition. (See threading.py:Thread.join().) |
| 535 | # - We stub out the acquire method on the condition to force it to wait |
| 536 | # until the child thread forks. (See LOCK ACQUIRED HERE) |
| 537 | # - The child thread forks. (See LOCK HELD and WORKER THREAD FORKS |
| 538 | # HERE) |
| 539 | # - The main thread of the parent process enters Condition.wait(), |
| 540 | # which releases the lock on the child thread. |
| 541 | # - The child process returns. Without the necessary fix, when the |
| 542 | # main thread of the child process (which used to be the child thread |
| 543 | # in the parent process) attempts to exit, it will try to acquire the |
| 544 | # lock in the Thread._block Condition object and hang, because the |
| 545 | # lock was held across the fork. |
| 546 | |
| 547 | script = """if 1: |
| 548 | import os, time, threading |
| 549 | |
| 550 | finish_join = False |
| 551 | start_fork = False |
| 552 | |
| 553 | def worker(): |
| 554 | # Wait until this thread's lock is acquired before forking to |
| 555 | # create the deadlock. |
| 556 | global finish_join |
| 557 | while not start_fork: |
| 558 | time.sleep(0.01) |
| 559 | # LOCK HELD: Main thread holds lock across this call. |
| 560 | childpid = os.fork() |
| 561 | finish_join = True |
| 562 | if childpid != 0: |
| 563 | # Parent process just waits for child. |
| 564 | os.waitpid(childpid, 0) |
| 565 | # Child process should just return. |
| 566 | |
| 567 | w = threading.Thread(target=worker) |
| 568 | |
| 569 | # Stub out the private condition variable's lock acquire method. |
| 570 | # This acquires the lock and then waits until the child has forked |
| 571 | # before returning, which will release the lock soon after. If |
| 572 | # someone else tries to fix this test case by acquiring this lock |
| 573 | # before forking instead of reseting it, the test case will |
| 574 | # deadlock when it shouldn't. |
| 575 | condition = w._block |
| 576 | orig_acquire = condition.acquire |
| 577 | call_count_lock = threading.Lock() |
| 578 | call_count = 0 |
| 579 | def my_acquire(): |
| 580 | global call_count |
| 581 | global start_fork |
| 582 | orig_acquire() # LOCK ACQUIRED HERE |
| 583 | start_fork = True |
| 584 | if call_count == 0: |
| 585 | while not finish_join: |
| 586 | time.sleep(0.01) # WORKER THREAD FORKS HERE |
| 587 | with call_count_lock: |
| 588 | call_count += 1 |
| 589 | condition.acquire = my_acquire |
| 590 | |
| 591 | w.start() |
| 592 | w.join() |
| 593 | print('end of main') |
| 594 | """ |
| 595 | self.assertScriptHasOutput(script, "end of main\n") |
| 596 | |
| 597 | @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()") |
| 598 | def test_5_clear_waiter_locks_to_avoid_crash(self): |
| 599 | # Check that a spawned thread that forks doesn't segfault on certain |
| 600 | # platforms, namely OS X. This used to happen if there was a waiter |
| 601 | # lock in the thread's condition variable's waiters list. Even though |
| 602 | # we know the lock will be held across the fork, it is not safe to |
| 603 | # release locks held across forks on all platforms, so releasing the |
| 604 | # waiter lock caused a segfault on OS X. Furthermore, since locks on |
| 605 | # OS X are (as of this writing) implemented with a mutex + condition |
| 606 | # variable instead of a semaphore, while we know that the Python-level |
| 607 | # lock will be acquired, we can't know if the internal mutex will be |
| 608 | # acquired at the time of the fork. |
| 609 | |
| 610 | # Skip platforms with known problems forking from a worker thread. |
| 611 | # See http://bugs.python.org/issue3863. |
| 612 | if sys.platform in ('freebsd4', 'freebsd5', 'freebsd6', 'os2emx'): |
| 613 | raise unittest.SkipTest('due to known OS bugs on ' + sys.platform) |
| 614 | script = """if True: |
| 615 | import os, time, threading |
| 616 | |
| 617 | start_fork = False |
| 618 | |
| 619 | def worker(): |
| 620 | # Wait until the main thread has attempted to join this thread |
| 621 | # before continuing. |
| 622 | while not start_fork: |
| 623 | time.sleep(0.01) |
| 624 | childpid = os.fork() |
| 625 | if childpid != 0: |
| 626 | # Parent process just waits for child. |
| 627 | (cpid, rc) = os.waitpid(childpid, 0) |
| 628 | assert cpid == childpid |
| 629 | assert rc == 0 |
| 630 | print('end of worker thread') |
| 631 | else: |
| 632 | # Child process should just return. |
| 633 | pass |
| 634 | |
| 635 | w = threading.Thread(target=worker) |
| 636 | |
| 637 | # Stub out the private condition variable's _release_save method. |
| 638 | # This releases the condition's lock and flips the global that |
| 639 | # causes the worker to fork. At this point, the problematic waiter |
| 640 | # lock has been acquired once by the waiter and has been put onto |
| 641 | # the waiters list. |
| 642 | condition = w._block |
| 643 | orig_release_save = condition._release_save |
| 644 | def my_release_save(): |
| 645 | global start_fork |
| 646 | orig_release_save() |
| 647 | # Waiter lock held here, condition lock released. |
| 648 | start_fork = True |
| 649 | condition._release_save = my_release_save |
| 650 | |
| 651 | w.start() |
| 652 | w.join() |
| 653 | print('end of main thread') |
| 654 | """ |
| 655 | output = "end of worker thread\nend of main thread\n" |
| 656 | self.assertScriptHasOutput(script, output) |
| 657 | |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 658 | |
Antoine Pitrou | b0e9bd4 | 2009-10-27 20:05:26 +0000 | [diff] [blame] | 659 | class ThreadingExceptionTests(BaseTestCase): |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 660 | # A RuntimeError should be raised if Thread.start() is called |
| 661 | # multiple times. |
| 662 | def test_start_thread_again(self): |
| 663 | thread = threading.Thread() |
| 664 | thread.start() |
| 665 | self.assertRaises(RuntimeError, thread.start) |
| 666 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 667 | def test_joining_current_thread(self): |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 668 | current_thread = threading.current_thread() |
| 669 | self.assertRaises(RuntimeError, current_thread.join); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 670 | |
| 671 | def test_joining_inactive_thread(self): |
| 672 | thread = threading.Thread() |
| 673 | self.assertRaises(RuntimeError, thread.join) |
| 674 | |
| 675 | def test_daemonize_active_thread(self): |
| 676 | thread = threading.Thread() |
| 677 | thread.start() |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 678 | self.assertRaises(RuntimeError, setattr, thread, "daemon", True) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 679 | |
| 680 | |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 681 | class LockTests(lock_tests.LockTests): |
| 682 | locktype = staticmethod(threading.Lock) |
| 683 | |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 684 | class PyRLockTests(lock_tests.RLockTests): |
| 685 | locktype = staticmethod(threading._PyRLock) |
| 686 | |
| 687 | class CRLockTests(lock_tests.RLockTests): |
| 688 | locktype = staticmethod(threading._CRLock) |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 689 | |
| 690 | class EventTests(lock_tests.EventTests): |
| 691 | eventtype = staticmethod(threading.Event) |
| 692 | |
| 693 | class ConditionAsRLockTests(lock_tests.RLockTests): |
| 694 | # An Condition uses an RLock by default and exports its API. |
| 695 | locktype = staticmethod(threading.Condition) |
| 696 | |
| 697 | class ConditionTests(lock_tests.ConditionTests): |
| 698 | condtype = staticmethod(threading.Condition) |
| 699 | |
| 700 | class SemaphoreTests(lock_tests.SemaphoreTests): |
| 701 | semtype = staticmethod(threading.Semaphore) |
| 702 | |
| 703 | class BoundedSemaphoreTests(lock_tests.BoundedSemaphoreTests): |
| 704 | semtype = staticmethod(threading.BoundedSemaphore) |
| 705 | |
Kristján Valur Jónsson | 3be0003 | 2010-10-28 09:43:10 +0000 | [diff] [blame] | 706 | class BarrierTests(lock_tests.BarrierTests): |
| 707 | barriertype = staticmethod(threading.Barrier) |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 708 | |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 709 | def test_main(): |
Antoine Pitrou | 434736a | 2009-11-10 18:46:01 +0000 | [diff] [blame] | 710 | test.support.run_unittest(LockTests, PyRLockTests, CRLockTests, EventTests, |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 711 | ConditionAsRLockTests, ConditionTests, |
| 712 | SemaphoreTests, BoundedSemaphoreTests, |
| 713 | ThreadTests, |
| 714 | ThreadJoinOnShutdown, |
| 715 | ThreadingExceptionTests, |
Kristján Valur Jónsson | 3be0003 | 2010-10-28 09:43:10 +0000 | [diff] [blame] | 716 | BarrierTests |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 717 | ) |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 718 | |
| 719 | if __name__ == "__main__": |
| 720 | test_main() |