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 |
Benjamin Peterson | fcf5d63 | 2008-10-16 23:24:44 +0000 | [diff] [blame] | 4 | from test.support import verbose |
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 |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 8 | import threading |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 9 | import _thread |
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 |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 13 | |
Antoine Pitrou | 959f3e5 | 2009-11-09 16:52:46 +0000 | [diff] [blame] | 14 | from test import lock_tests |
| 15 | |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 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 |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 26 | |
| 27 | class TestThread(threading.Thread): |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 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 |
| 34 | |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 35 | def run(self): |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 36 | delay = random.random() / 10000.0 |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 37 | if verbose: |
Jeffrey Yasskin | ca67412 | 2008-03-29 05:06:52 +0000 | [diff] [blame] | 38 | print('task %s will run for %.1f usec' % |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 39 | (self.name, delay * 1e6)) |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 40 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 41 | with self.sema: |
| 42 | with self.mutex: |
| 43 | self.nrunning.inc() |
| 44 | if verbose: |
| 45 | print(self.nrunning.get(), 'tasks are running') |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 46 | self.testcase.assertTrue(self.nrunning.get() <= 3) |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 47 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 48 | time.sleep(delay) |
| 49 | if verbose: |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 50 | print('task', self.name, 'done') |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 51 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 52 | with self.mutex: |
| 53 | self.nrunning.dec() |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 54 | self.testcase.assertTrue(self.nrunning.get() >= 0) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 55 | if verbose: |
| 56 | print('%s is finished. %d tasks are running' % |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 57 | (self.name, self.nrunning.get())) |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 58 | |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 59 | |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 60 | class ThreadTests(unittest.TestCase): |
Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 61 | |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 62 | # Create a bunch of threads, let each do some work, wait until all are |
| 63 | # done. |
| 64 | def test_various_ops(self): |
| 65 | # This takes about n/3 seconds to run (about n/3 clumps of tasks, |
| 66 | # times about 1 second per clump). |
| 67 | NUMTASKS = 10 |
| 68 | |
| 69 | # no more than 3 of the 10 can run at once |
| 70 | sema = threading.BoundedSemaphore(value=3) |
| 71 | mutex = threading.RLock() |
| 72 | numrunning = Counter() |
| 73 | |
| 74 | threads = [] |
| 75 | |
| 76 | for i in range(NUMTASKS): |
| 77 | t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning) |
| 78 | threads.append(t) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 79 | self.assertEqual(t.ident, None) |
| 80 | self.assertTrue(re.match('<TestThread\(.*, initial\)>', repr(t))) |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 81 | t.start() |
| 82 | |
| 83 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 84 | print('waiting for all tasks to complete') |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 85 | for t in threads: |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 86 | t.join(NUMTASKS) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 87 | self.assertTrue(not t.is_alive()) |
| 88 | self.assertNotEqual(t.ident, 0) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 89 | self.assertFalse(t.ident is None) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 90 | self.assertTrue(re.match('<TestThread\(.*, \w+ -?\d+\)>', repr(t))) |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 91 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 92 | print('all tasks done') |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 93 | self.assertEqual(numrunning.get(), 0) |
| 94 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 95 | def test_ident_of_no_threading_threads(self): |
| 96 | # The ident still must work for the main thread and dummy threads. |
| 97 | self.assertFalse(threading.currentThread().ident is None) |
| 98 | def f(): |
| 99 | ident.append(threading.currentThread().ident) |
| 100 | done.set() |
| 101 | done = threading.Event() |
| 102 | ident = [] |
| 103 | _thread.start_new_thread(f, ()) |
| 104 | done.wait() |
| 105 | self.assertFalse(ident[0] is None) |
Antoine Pitrou | debfafd | 2009-11-08 00:36:36 +0000 | [diff] [blame] | 106 | # Kill the "immortal" _DummyThread |
| 107 | del threading._active[ident[0]] |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 108 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 109 | # run with a small(ish) thread stack size (256kB) |
| 110 | def test_various_ops_small_stack(self): |
| 111 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 112 | print('with 256kB thread stack size...') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 113 | try: |
| 114 | threading.stack_size(262144) |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 115 | except _thread.error: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 116 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 117 | print('platform does not support changing thread stack size') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 118 | return |
| 119 | self.test_various_ops() |
| 120 | threading.stack_size(0) |
| 121 | |
| 122 | # run with a large thread stack size (1MB) |
| 123 | def test_various_ops_large_stack(self): |
| 124 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 125 | print('with 1MB thread stack size...') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 126 | try: |
| 127 | threading.stack_size(0x100000) |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 128 | except _thread.error: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 129 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 130 | print('platform does not support changing thread stack size') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 131 | return |
| 132 | self.test_various_ops() |
| 133 | threading.stack_size(0) |
| 134 | |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 135 | def test_foreign_thread(self): |
| 136 | # Check that a "foreign" thread can use the threading module. |
| 137 | def f(mutex): |
Antoine Pitrou | 959f3e5 | 2009-11-09 16:52:46 +0000 | [diff] [blame] | 138 | # Calling current_thread() forces an entry for the foreign |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 139 | # thread to get made in the threading._active map. |
Antoine Pitrou | 959f3e5 | 2009-11-09 16:52:46 +0000 | [diff] [blame] | 140 | threading.current_thread() |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 141 | mutex.release() |
| 142 | |
| 143 | mutex = threading.Lock() |
| 144 | mutex.acquire() |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 145 | tid = _thread.start_new_thread(f, (mutex,)) |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 146 | # Wait for the thread to finish. |
| 147 | mutex.acquire() |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 148 | self.assertTrue(tid in threading._active) |
| 149 | self.assertTrue(isinstance(threading._active[tid], |
Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 150 | threading._DummyThread)) |
| 151 | del threading._active[tid] |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 152 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 153 | # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently) |
| 154 | # exposed at the Python level. This test relies on ctypes to get at it. |
| 155 | def test_PyThreadState_SetAsyncExc(self): |
| 156 | try: |
| 157 | import ctypes |
| 158 | except ImportError: |
| 159 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 160 | print("test_PyThreadState_SetAsyncExc can't import ctypes") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 161 | return # can't do anything |
| 162 | |
| 163 | set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc |
| 164 | |
| 165 | class AsyncExc(Exception): |
| 166 | pass |
| 167 | |
| 168 | exception = ctypes.py_object(AsyncExc) |
| 169 | |
| 170 | # `worker_started` is set by the thread when it's inside a try/except |
| 171 | # block waiting to catch the asynchronously set AsyncExc exception. |
| 172 | # `worker_saw_exception` is set by the thread upon catching that |
| 173 | # exception. |
| 174 | worker_started = threading.Event() |
| 175 | worker_saw_exception = threading.Event() |
| 176 | |
| 177 | class Worker(threading.Thread): |
| 178 | def run(self): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 179 | self.id = _thread.get_ident() |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 180 | self.finished = False |
| 181 | |
| 182 | try: |
| 183 | while True: |
| 184 | worker_started.set() |
| 185 | time.sleep(0.1) |
| 186 | except AsyncExc: |
| 187 | self.finished = True |
| 188 | worker_saw_exception.set() |
| 189 | |
| 190 | t = Worker() |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 191 | 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] | 192 | t.start() |
| 193 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 194 | print(" started worker thread") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 195 | |
| 196 | # Try a thread id that doesn't make sense. |
| 197 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 198 | print(" trying nonsensical thread id") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 199 | result = set_async_exc(ctypes.c_long(-1), exception) |
| 200 | self.assertEqual(result, 0) # no thread states modified |
| 201 | |
| 202 | # Now raise an exception in the worker thread. |
| 203 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 204 | print(" waiting for worker thread to get started") |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 205 | ret = worker_started.wait() |
| 206 | self.assertTrue(ret) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 207 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 208 | print(" verifying worker hasn't exited") |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 209 | self.assertTrue(not t.finished) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 210 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 211 | print(" attempting to raise asynch exception in worker") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 212 | result = set_async_exc(ctypes.c_long(t.id), exception) |
| 213 | self.assertEqual(result, 1) # one thread state modified |
| 214 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 215 | print(" waiting for worker to say it caught the exception") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 216 | worker_saw_exception.wait(timeout=10) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 217 | self.assertTrue(t.finished) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 218 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 219 | print(" all OK -- joining worker") |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 220 | if t.finished: |
| 221 | t.join() |
| 222 | # else the thread is still running, and we have no way to kill it |
| 223 | |
Gregory P. Smith | 31d12ca | 2010-02-28 19:21:42 +0000 | [diff] [blame] | 224 | def test_limbo_cleanup(self): |
| 225 | # Issue 7481: Failure to start thread should cleanup the limbo map. |
| 226 | def fail_new_thread(*args): |
| 227 | raise threading.ThreadError() |
| 228 | _start_new_thread = threading._start_new_thread |
| 229 | threading._start_new_thread = fail_new_thread |
| 230 | try: |
| 231 | t = threading.Thread(target=lambda: None) |
Gregory P. Smith | ff31864 | 2010-03-01 03:19:29 +0000 | [diff] [blame] | 232 | self.assertRaises(threading.ThreadError, t.start) |
| 233 | self.assertFalse( |
| 234 | t in threading._limbo, |
| 235 | "Failed to cleanup _limbo map on failure of Thread.start().") |
Gregory P. Smith | 31d12ca | 2010-02-28 19:21:42 +0000 | [diff] [blame] | 236 | finally: |
| 237 | threading._start_new_thread = _start_new_thread |
| 238 | |
Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 239 | def test_finalize_runnning_thread(self): |
| 240 | # Issue 1402: the PyGILState_Ensure / _Release functions may be called |
| 241 | # very late on python exit: on deallocation of a running thread for |
| 242 | # example. |
| 243 | try: |
| 244 | import ctypes |
| 245 | except ImportError: |
| 246 | if verbose: |
| 247 | print("test_finalize_with_runnning_thread can't import ctypes") |
| 248 | return # can't do anything |
| 249 | |
| 250 | import subprocess |
| 251 | rc = subprocess.call([sys.executable, "-c", """if 1: |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 252 | import ctypes, sys, time, _thread |
Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 253 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 254 | # This lock is used as a simple event variable. |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 255 | ready = _thread.allocate_lock() |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 256 | ready.acquire() |
| 257 | |
Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 258 | # Module globals are cleared before __del__ is run |
| 259 | # So we save the functions in class dict |
| 260 | class C: |
| 261 | ensure = ctypes.pythonapi.PyGILState_Ensure |
| 262 | release = ctypes.pythonapi.PyGILState_Release |
| 263 | def __del__(self): |
| 264 | state = self.ensure() |
| 265 | self.release(state) |
| 266 | |
| 267 | def waitingThread(): |
| 268 | x = C() |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 269 | ready.release() |
Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 270 | time.sleep(100) |
| 271 | |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 272 | _thread.start_new_thread(waitingThread, ()) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 273 | ready.acquire() # Be sure the other thread is waiting. |
Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 274 | sys.exit(42) |
| 275 | """]) |
| 276 | self.assertEqual(rc, 42) |
| 277 | |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 278 | def test_finalize_with_trace(self): |
| 279 | # Issue1733757 |
| 280 | # Avoid a deadlock when sys.settrace steps into threading._shutdown |
| 281 | import subprocess |
Antoine Pitrou | 4a5dd5c | 2010-09-20 11:17:39 +0000 | [diff] [blame^] | 282 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 283 | import sys, threading |
| 284 | |
| 285 | # A deadlock-killer, to prevent the |
| 286 | # testsuite to hang forever |
| 287 | def killer(): |
| 288 | import os, time |
| 289 | time.sleep(2) |
| 290 | print('program blocked; aborting') |
| 291 | os._exit(2) |
| 292 | t = threading.Thread(target=killer) |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 293 | t.daemon = True |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 294 | t.start() |
| 295 | |
| 296 | # This is the trace function |
| 297 | def func(frame, event, arg): |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 298 | threading.current_thread() |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 299 | return func |
| 300 | |
| 301 | sys.settrace(func) |
Antoine Pitrou | 4a5dd5c | 2010-09-20 11:17:39 +0000 | [diff] [blame^] | 302 | """], |
| 303 | stdout=subprocess.PIPE, |
| 304 | stderr=subprocess.PIPE) |
| 305 | stdout, stderr = p.communicate() |
| 306 | rc = p.returncode |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 307 | self.assertFalse(rc == 2, "interpreted was blocked") |
Antoine Pitrou | 4a5dd5c | 2010-09-20 11:17:39 +0000 | [diff] [blame^] | 308 | self.assertTrue(rc == 0, |
| 309 | "Unexpected error: " + ascii(stderr)) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 310 | |
Antoine Pitrou | cefb316 | 2009-10-20 22:08:36 +0000 | [diff] [blame] | 311 | def test_join_nondaemon_on_shutdown(self): |
| 312 | # Issue 1722344 |
| 313 | # Raising SystemExit skipped threading._shutdown |
| 314 | import subprocess |
| 315 | p = subprocess.Popen([sys.executable, "-c", """if 1: |
| 316 | import threading |
| 317 | from time import sleep |
| 318 | |
| 319 | def child(): |
| 320 | sleep(1) |
| 321 | # As a non-daemon thread we SHOULD wake up and nothing |
| 322 | # should be torn down yet |
| 323 | print("Woke up, sleep function is:", sleep) |
| 324 | |
| 325 | threading.Thread(target=child).start() |
| 326 | raise SystemExit |
| 327 | """], |
| 328 | stdout=subprocess.PIPE, |
| 329 | stderr=subprocess.PIPE) |
| 330 | stdout, stderr = p.communicate() |
Antoine Pitrou | f6779fb | 2009-10-23 22:06:37 +0000 | [diff] [blame] | 331 | self.assertEqual(stdout.strip(), |
| 332 | b"Woke up, sleep function is: <built-in function sleep>") |
Antoine Pitrou | cefb316 | 2009-10-20 22:08:36 +0000 | [diff] [blame] | 333 | stderr = re.sub(br"^\[\d+ refs\]", b"", stderr, re.MULTILINE).strip() |
| 334 | self.assertEqual(stderr, b"") |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 335 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 336 | def test_enumerate_after_join(self): |
| 337 | # Try hard to trigger #1703448: a thread is still returned in |
| 338 | # threading.enumerate() after it has been join()ed. |
| 339 | enum = threading.enumerate |
| 340 | old_interval = sys.getcheckinterval() |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 341 | try: |
Jeffrey Yasskin | ca67412 | 2008-03-29 05:06:52 +0000 | [diff] [blame] | 342 | for i in range(1, 100): |
| 343 | # Try a couple times at each thread-switching interval |
| 344 | # to get more interleavings. |
| 345 | sys.setcheckinterval(i // 5) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 346 | t = threading.Thread(target=lambda: None) |
| 347 | t.start() |
| 348 | t.join() |
| 349 | l = enum() |
| 350 | self.assertFalse(t in l, |
| 351 | "#1703448 triggered after %d trials: %s" % (i, l)) |
| 352 | finally: |
| 353 | sys.setcheckinterval(old_interval) |
| 354 | |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 355 | def test_no_refcycle_through_target(self): |
| 356 | class RunSelfFunction(object): |
| 357 | def __init__(self, should_raise): |
| 358 | # The links in this refcycle from Thread back to self |
| 359 | # should be cleaned up when the thread completes. |
| 360 | self.should_raise = should_raise |
| 361 | self.thread = threading.Thread(target=self._run, |
| 362 | args=(self,), |
| 363 | kwargs={'yet_another':self}) |
| 364 | self.thread.start() |
| 365 | |
| 366 | def _run(self, other_ref, yet_another): |
| 367 | if self.should_raise: |
| 368 | raise SystemExit |
| 369 | |
| 370 | cyclic_object = RunSelfFunction(should_raise=False) |
| 371 | weak_cyclic_object = weakref.ref(cyclic_object) |
| 372 | cyclic_object.thread.join() |
| 373 | del cyclic_object |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 374 | self.assertEquals(None, weak_cyclic_object(), |
| 375 | msg=('%d references still around' % |
| 376 | sys.getrefcount(weak_cyclic_object()))) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 377 | |
| 378 | raising_cyclic_object = RunSelfFunction(should_raise=True) |
| 379 | weak_raising_cyclic_object = weakref.ref(raising_cyclic_object) |
| 380 | raising_cyclic_object.thread.join() |
| 381 | del raising_cyclic_object |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 382 | self.assertEquals(None, weak_raising_cyclic_object(), |
| 383 | msg=('%d references still around' % |
| 384 | sys.getrefcount(weak_raising_cyclic_object()))) |
Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 385 | |
Benjamin Peterson | b3085c9 | 2008-09-01 23:09:31 +0000 | [diff] [blame] | 386 | def test_old_threading_api(self): |
| 387 | # Just a quick sanity check to make sure the old method names are |
| 388 | # still present |
Benjamin Peterson | f0923f5 | 2008-08-18 22:10:13 +0000 | [diff] [blame] | 389 | t = threading.Thread() |
Benjamin Peterson | b3085c9 | 2008-09-01 23:09:31 +0000 | [diff] [blame] | 390 | t.isDaemon() |
| 391 | t.setDaemon(True) |
| 392 | t.getName() |
| 393 | t.setName("name") |
| 394 | t.isAlive() |
| 395 | e = threading.Event() |
| 396 | e.isSet() |
| 397 | threading.activeCount() |
Benjamin Peterson | f0923f5 | 2008-08-18 22:10:13 +0000 | [diff] [blame] | 398 | |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 399 | |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 400 | class ThreadJoinOnShutdown(unittest.TestCase): |
| 401 | |
| 402 | def _run_and_join(self, script): |
| 403 | script = """if 1: |
| 404 | import sys, os, time, threading |
| 405 | |
| 406 | # a thread, which waits for the main program to terminate |
| 407 | def joiningfunc(mainthread): |
| 408 | mainthread.join() |
| 409 | print('end of thread') |
Antoine Pitrou | 5fe291f | 2008-09-06 23:00:03 +0000 | [diff] [blame] | 410 | # stdout is fully buffered because not a tty, we have to flush |
| 411 | # before exit. |
| 412 | sys.stdout.flush() |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 413 | \n""" + script |
| 414 | |
| 415 | import subprocess |
| 416 | p = subprocess.Popen([sys.executable, "-c", script], stdout=subprocess.PIPE) |
| 417 | rc = p.wait() |
Benjamin Peterson | ad703dc | 2008-07-17 17:02:57 +0000 | [diff] [blame] | 418 | data = p.stdout.read().decode().replace('\r', '') |
| 419 | self.assertEqual(data, "end of main\nend of thread\n") |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 420 | self.assertFalse(rc == 2, "interpreter was blocked") |
| 421 | self.assertTrue(rc == 0, "Unexpected error") |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 422 | |
| 423 | def test_1_join_on_shutdown(self): |
| 424 | # The usual case: on exit, wait for a non-daemon thread |
| 425 | script = """if 1: |
| 426 | import os |
| 427 | t = threading.Thread(target=joiningfunc, |
| 428 | args=(threading.current_thread(),)) |
| 429 | t.start() |
| 430 | time.sleep(0.1) |
| 431 | print('end of main') |
| 432 | """ |
| 433 | self._run_and_join(script) |
| 434 | |
| 435 | |
| 436 | def test_2_join_in_forked_process(self): |
| 437 | # Like the test above, but from a forked interpreter |
| 438 | import os |
| 439 | if not hasattr(os, 'fork'): |
| 440 | return |
| 441 | script = """if 1: |
| 442 | childpid = os.fork() |
| 443 | if childpid != 0: |
| 444 | os.waitpid(childpid, 0) |
| 445 | sys.exit(0) |
| 446 | |
| 447 | t = threading.Thread(target=joiningfunc, |
| 448 | args=(threading.current_thread(),)) |
| 449 | t.start() |
| 450 | print('end of main') |
| 451 | """ |
| 452 | self._run_and_join(script) |
| 453 | |
Antoine Pitrou | 5fe291f | 2008-09-06 23:00:03 +0000 | [diff] [blame] | 454 | def test_3_join_in_forked_from_thread(self): |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 455 | # Like the test above, but fork() was called from a worker thread |
| 456 | # In the forked process, the main Thread object must be marked as stopped. |
| 457 | import os |
| 458 | if not hasattr(os, 'fork'): |
| 459 | return |
Benjamin Peterson | bcd8ac3 | 2008-10-10 22:20:52 +0000 | [diff] [blame] | 460 | # Skip platforms with known problems forking from a worker thread. |
| 461 | # See http://bugs.python.org/issue3863. |
| 462 | if sys.platform in ('freebsd4', 'freebsd5', 'freebsd6', 'os2emx'): |
R. David Murray | 9948da0 | 2009-10-26 03:27:32 +0000 | [diff] [blame] | 463 | print('Skipping test_3_join_in_forked_from_thread' |
| 464 | ' due to known OS bugs on', sys.platform, file=sys.stderr) |
Benjamin Peterson | bcd8ac3 | 2008-10-10 22:20:52 +0000 | [diff] [blame] | 465 | return |
Jesse Noller | a851397 | 2008-07-17 16:49:17 +0000 | [diff] [blame] | 466 | script = """if 1: |
| 467 | main_thread = threading.current_thread() |
| 468 | def worker(): |
| 469 | childpid = os.fork() |
| 470 | if childpid != 0: |
| 471 | os.waitpid(childpid, 0) |
| 472 | sys.exit(0) |
| 473 | |
| 474 | t = threading.Thread(target=joiningfunc, |
| 475 | args=(main_thread,)) |
| 476 | print('end of main') |
| 477 | t.start() |
| 478 | t.join() # Should not block: main_thread is already stopped |
| 479 | |
| 480 | w = threading.Thread(target=worker) |
| 481 | w.start() |
| 482 | """ |
| 483 | self._run_and_join(script) |
| 484 | |
| 485 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 486 | class ThreadingExceptionTests(unittest.TestCase): |
| 487 | # A RuntimeError should be raised if Thread.start() is called |
| 488 | # multiple times. |
| 489 | def test_start_thread_again(self): |
| 490 | thread = threading.Thread() |
| 491 | thread.start() |
| 492 | self.assertRaises(RuntimeError, thread.start) |
| 493 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 494 | def test_joining_current_thread(self): |
Benjamin Peterson | 672b803 | 2008-06-11 19:14:14 +0000 | [diff] [blame] | 495 | current_thread = threading.current_thread() |
| 496 | self.assertRaises(RuntimeError, current_thread.join); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 497 | |
| 498 | def test_joining_inactive_thread(self): |
| 499 | thread = threading.Thread() |
| 500 | self.assertRaises(RuntimeError, thread.join) |
| 501 | |
| 502 | def test_daemonize_active_thread(self): |
| 503 | thread = threading.Thread() |
| 504 | thread.start() |
Benjamin Peterson | fdbea96 | 2008-08-18 17:33:47 +0000 | [diff] [blame] | 505 | self.assertRaises(RuntimeError, setattr, thread, "daemon", True) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 506 | |
| 507 | |
Antoine Pitrou | 959f3e5 | 2009-11-09 16:52:46 +0000 | [diff] [blame] | 508 | class LockTests(lock_tests.LockTests): |
| 509 | locktype = staticmethod(threading.Lock) |
| 510 | |
| 511 | class RLockTests(lock_tests.RLockTests): |
| 512 | locktype = staticmethod(threading.RLock) |
| 513 | |
| 514 | class EventTests(lock_tests.EventTests): |
| 515 | eventtype = staticmethod(threading.Event) |
| 516 | |
| 517 | class ConditionAsRLockTests(lock_tests.RLockTests): |
| 518 | # An Condition uses an RLock by default and exports its API. |
| 519 | locktype = staticmethod(threading.Condition) |
| 520 | |
| 521 | class ConditionTests(lock_tests.ConditionTests): |
| 522 | condtype = staticmethod(threading.Condition) |
| 523 | |
| 524 | class SemaphoreTests(lock_tests.SemaphoreTests): |
| 525 | semtype = staticmethod(threading.Semaphore) |
| 526 | |
| 527 | class BoundedSemaphoreTests(lock_tests.BoundedSemaphoreTests): |
| 528 | semtype = staticmethod(threading.BoundedSemaphore) |
| 529 | |
| 530 | |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 531 | def test_main(): |
Antoine Pitrou | 959f3e5 | 2009-11-09 16:52:46 +0000 | [diff] [blame] | 532 | test.support.run_unittest(LockTests, RLockTests, EventTests, |
| 533 | ConditionAsRLockTests, ConditionTests, |
| 534 | SemaphoreTests, BoundedSemaphoreTests, |
| 535 | ThreadTests, |
| 536 | ThreadJoinOnShutdown, |
| 537 | ThreadingExceptionTests, |
| 538 | ) |
Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 539 | |
| 540 | if __name__ == "__main__": |
| 541 | test_main() |