| 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 | 
 | 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 |  | 
| Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 14 | # A trivial mutable counter. | 
 | 15 | class Counter(object): | 
 | 16 |     def __init__(self): | 
 | 17 |         self.value = 0 | 
 | 18 |     def inc(self): | 
 | 19 |         self.value += 1 | 
 | 20 |     def dec(self): | 
 | 21 |         self.value -= 1 | 
 | 22 |     def get(self): | 
 | 23 |         return self.value | 
| Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 24 |  | 
 | 25 | class TestThread(threading.Thread): | 
| Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 26 |     def __init__(self, name, testcase, sema, mutex, nrunning): | 
 | 27 |         threading.Thread.__init__(self, name=name) | 
 | 28 |         self.testcase = testcase | 
 | 29 |         self.sema = sema | 
 | 30 |         self.mutex = mutex | 
 | 31 |         self.nrunning = nrunning | 
 | 32 |  | 
| Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 33 |     def run(self): | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 34 |         delay = random.random() / 10000.0 | 
| Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 35 |         if verbose: | 
| Jeffrey Yasskin | ca67412 | 2008-03-29 05:06:52 +0000 | [diff] [blame] | 36 |             print('task %s will run for %.1f usec' % | 
 | 37 |                   (self.getName(), delay * 1e6)) | 
| Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 38 |  | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 39 |         with self.sema: | 
 | 40 |             with self.mutex: | 
 | 41 |                 self.nrunning.inc() | 
 | 42 |                 if verbose: | 
 | 43 |                     print(self.nrunning.get(), 'tasks are running') | 
 | 44 |                 self.testcase.assert_(self.nrunning.get() <= 3) | 
| Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 45 |  | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 46 |             time.sleep(delay) | 
 | 47 |             if verbose: | 
 | 48 |                 print('task', self.getName(), 'done') | 
 | 49 |             with self.mutex: | 
 | 50 |                 self.nrunning.dec() | 
 | 51 |                 self.testcase.assert_(self.nrunning.get() >= 0) | 
 | 52 |                 if verbose: | 
 | 53 |                     print('%s is finished. %d tasks are running' % | 
| Jeffrey Yasskin | ca67412 | 2008-03-29 05:06:52 +0000 | [diff] [blame] | 54 |                           (self.getName(), self.nrunning.get())) | 
| Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 55 |  | 
| Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 56 | class ThreadTests(unittest.TestCase): | 
| Skip Montanaro | 4533f60 | 2001-08-20 20:28:48 +0000 | [diff] [blame] | 57 |  | 
| Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 58 |     # Create a bunch of threads, let each do some work, wait until all are | 
 | 59 |     # done. | 
 | 60 |     def test_various_ops(self): | 
 | 61 |         # This takes about n/3 seconds to run (about n/3 clumps of tasks, | 
 | 62 |         # times about 1 second per clump). | 
 | 63 |         NUMTASKS = 10 | 
 | 64 |  | 
 | 65 |         # no more than 3 of the 10 can run at once | 
 | 66 |         sema = threading.BoundedSemaphore(value=3) | 
 | 67 |         mutex = threading.RLock() | 
 | 68 |         numrunning = Counter() | 
 | 69 |  | 
 | 70 |         threads = [] | 
 | 71 |  | 
 | 72 |         for i in range(NUMTASKS): | 
 | 73 |             t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning) | 
 | 74 |             threads.append(t) | 
| Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 75 |             self.failUnlessEqual(t.getIdent(), None) | 
 | 76 |             self.assert_(re.match('<TestThread\(.*, initial\)>', repr(t))) | 
| Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 77 |             t.start() | 
 | 78 |  | 
 | 79 |         if verbose: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 80 |             print('waiting for all tasks to complete') | 
| Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 81 |         for t in threads: | 
| Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 82 |             t.join(NUMTASKS) | 
 | 83 |             self.assert_(not t.isAlive()) | 
| Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 84 |             self.failIfEqual(t.getIdent(), 0) | 
 | 85 |             self.assert_(re.match('<TestThread\(.*, \w+ -?\d+\)>', repr(t))) | 
| Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 86 |         if verbose: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 87 |             print('all tasks done') | 
| Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 88 |         self.assertEqual(numrunning.get(), 0) | 
 | 89 |  | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 90 |     # run with a small(ish) thread stack size (256kB) | 
 | 91 |     def test_various_ops_small_stack(self): | 
 | 92 |         if verbose: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 93 |             print('with 256kB thread stack size...') | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 94 |         try: | 
 | 95 |             threading.stack_size(262144) | 
| Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 96 |         except _thread.error: | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 97 |             if verbose: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 98 |                 print('platform does not support changing thread stack size') | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 99 |             return | 
 | 100 |         self.test_various_ops() | 
 | 101 |         threading.stack_size(0) | 
 | 102 |  | 
 | 103 |     # run with a large thread stack size (1MB) | 
 | 104 |     def test_various_ops_large_stack(self): | 
 | 105 |         if verbose: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 106 |             print('with 1MB thread stack size...') | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 107 |         try: | 
 | 108 |             threading.stack_size(0x100000) | 
| Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 109 |         except _thread.error: | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 110 |             if verbose: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 111 |                 print('platform does not support changing thread stack size') | 
| Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 112 |             return | 
 | 113 |         self.test_various_ops() | 
 | 114 |         threading.stack_size(0) | 
 | 115 |  | 
| Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 116 |     def test_foreign_thread(self): | 
 | 117 |         # Check that a "foreign" thread can use the threading module. | 
 | 118 |         def f(mutex): | 
 | 119 |             # Acquiring an RLock forces an entry for the foreign | 
 | 120 |             # thread to get made in the threading._active map. | 
 | 121 |             r = threading.RLock() | 
 | 122 |             r.acquire() | 
 | 123 |             r.release() | 
 | 124 |             mutex.release() | 
 | 125 |  | 
 | 126 |         mutex = threading.Lock() | 
 | 127 |         mutex.acquire() | 
| Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 128 |         tid = _thread.start_new_thread(f, (mutex,)) | 
| Tim Peters | 711906e | 2005-01-08 07:30:42 +0000 | [diff] [blame] | 129 |         # Wait for the thread to finish. | 
 | 130 |         mutex.acquire() | 
 | 131 |         self.assert_(tid in threading._active) | 
 | 132 |         self.assert_(isinstance(threading._active[tid], | 
 | 133 |                                 threading._DummyThread)) | 
 | 134 |         del threading._active[tid] | 
| Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 135 |  | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 136 |     # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently) | 
 | 137 |     # exposed at the Python level.  This test relies on ctypes to get at it. | 
 | 138 |     def test_PyThreadState_SetAsyncExc(self): | 
 | 139 |         try: | 
 | 140 |             import ctypes | 
 | 141 |         except ImportError: | 
 | 142 |             if verbose: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 143 |                 print("test_PyThreadState_SetAsyncExc can't import ctypes") | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 144 |             return  # can't do anything | 
 | 145 |  | 
 | 146 |         set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc | 
 | 147 |  | 
 | 148 |         class AsyncExc(Exception): | 
 | 149 |             pass | 
 | 150 |  | 
 | 151 |         exception = ctypes.py_object(AsyncExc) | 
 | 152 |  | 
 | 153 |         # `worker_started` is set by the thread when it's inside a try/except | 
 | 154 |         # block waiting to catch the asynchronously set AsyncExc exception. | 
 | 155 |         # `worker_saw_exception` is set by the thread upon catching that | 
 | 156 |         # exception. | 
 | 157 |         worker_started = threading.Event() | 
 | 158 |         worker_saw_exception = threading.Event() | 
 | 159 |  | 
 | 160 |         class Worker(threading.Thread): | 
 | 161 |             def run(self): | 
| Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 162 |                 self.id = _thread.get_ident() | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 163 |                 self.finished = False | 
 | 164 |  | 
 | 165 |                 try: | 
 | 166 |                     while True: | 
 | 167 |                         worker_started.set() | 
 | 168 |                         time.sleep(0.1) | 
 | 169 |                 except AsyncExc: | 
 | 170 |                     self.finished = True | 
 | 171 |                     worker_saw_exception.set() | 
 | 172 |  | 
 | 173 |         t = Worker() | 
 | 174 |         t.setDaemon(True) # so if this fails, we don't hang Python at shutdown | 
 | 175 |         t.start() | 
 | 176 |         if verbose: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 177 |             print("    started worker thread") | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 178 |  | 
 | 179 |         # Try a thread id that doesn't make sense. | 
 | 180 |         if verbose: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 181 |             print("    trying nonsensical thread id") | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 182 |         result = set_async_exc(ctypes.c_long(-1), exception) | 
 | 183 |         self.assertEqual(result, 0)  # no thread states modified | 
 | 184 |  | 
 | 185 |         # Now raise an exception in the worker thread. | 
 | 186 |         if verbose: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 187 |             print("    waiting for worker thread to get started") | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 188 |         worker_started.wait() | 
 | 189 |         if verbose: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 190 |             print("    verifying worker hasn't exited") | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 191 |         self.assert_(not t.finished) | 
 | 192 |         if verbose: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 193 |             print("    attempting to raise asynch exception in worker") | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 194 |         result = set_async_exc(ctypes.c_long(t.id), exception) | 
 | 195 |         self.assertEqual(result, 1) # one thread state modified | 
 | 196 |         if verbose: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 197 |             print("    waiting for worker to say it caught the exception") | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 198 |         worker_saw_exception.wait(timeout=10) | 
 | 199 |         self.assert_(t.finished) | 
 | 200 |         if verbose: | 
| Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 201 |             print("    all OK -- joining worker") | 
| Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 202 |         if t.finished: | 
 | 203 |             t.join() | 
 | 204 |         # else the thread is still running, and we have no way to kill it | 
 | 205 |  | 
| Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 206 |     def test_finalize_runnning_thread(self): | 
 | 207 |         # Issue 1402: the PyGILState_Ensure / _Release functions may be called | 
 | 208 |         # very late on python exit: on deallocation of a running thread for | 
 | 209 |         # example. | 
 | 210 |         try: | 
 | 211 |             import ctypes | 
 | 212 |         except ImportError: | 
 | 213 |             if verbose: | 
 | 214 |                 print("test_finalize_with_runnning_thread can't import ctypes") | 
 | 215 |             return  # can't do anything | 
 | 216 |  | 
 | 217 |         import subprocess | 
 | 218 |         rc = subprocess.call([sys.executable, "-c", """if 1: | 
| Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 219 |             import ctypes, sys, time, _thread | 
| Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 220 |  | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 221 |             # This lock is used as a simple event variable. | 
| Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 222 |             ready = _thread.allocate_lock() | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 223 |             ready.acquire() | 
 | 224 |  | 
| Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 225 |             # Module globals are cleared before __del__ is run | 
 | 226 |             # So we save the functions in class dict | 
 | 227 |             class C: | 
 | 228 |                 ensure = ctypes.pythonapi.PyGILState_Ensure | 
 | 229 |                 release = ctypes.pythonapi.PyGILState_Release | 
 | 230 |                 def __del__(self): | 
 | 231 |                     state = self.ensure() | 
 | 232 |                     self.release(state) | 
 | 233 |  | 
 | 234 |             def waitingThread(): | 
 | 235 |                 x = C() | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 236 |                 ready.release() | 
| Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 237 |                 time.sleep(100) | 
 | 238 |  | 
| Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 239 |             _thread.start_new_thread(waitingThread, ()) | 
| Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 240 |             ready.acquire()  # Be sure the other thread is waiting. | 
| Christian Heimes | 7d2ff88 | 2007-11-30 14:35:04 +0000 | [diff] [blame] | 241 |             sys.exit(42) | 
 | 242 |             """]) | 
 | 243 |         self.assertEqual(rc, 42) | 
 | 244 |  | 
| Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 245 |     def test_finalize_with_trace(self): | 
 | 246 |         # Issue1733757 | 
 | 247 |         # Avoid a deadlock when sys.settrace steps into threading._shutdown | 
 | 248 |         import subprocess | 
 | 249 |         rc = subprocess.call([sys.executable, "-c", """if 1: | 
 | 250 |             import sys, threading | 
 | 251 |  | 
 | 252 |             # A deadlock-killer, to prevent the | 
 | 253 |             # testsuite to hang forever | 
 | 254 |             def killer(): | 
 | 255 |                 import os, time | 
 | 256 |                 time.sleep(2) | 
 | 257 |                 print('program blocked; aborting') | 
 | 258 |                 os._exit(2) | 
 | 259 |             t = threading.Thread(target=killer) | 
 | 260 |             t.setDaemon(True) | 
 | 261 |             t.start() | 
 | 262 |  | 
 | 263 |             # This is the trace function | 
 | 264 |             def func(frame, event, arg): | 
 | 265 |                 threading.currentThread() | 
 | 266 |                 return func | 
 | 267 |  | 
 | 268 |             sys.settrace(func) | 
 | 269 |             """]) | 
 | 270 |         self.failIf(rc == 2, "interpreted was blocked") | 
 | 271 |         self.failUnless(rc == 0, "Unexpected error") | 
 | 272 |  | 
 | 273 |  | 
| Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 274 |     def test_enumerate_after_join(self): | 
 | 275 |         # Try hard to trigger #1703448: a thread is still returned in | 
 | 276 |         # threading.enumerate() after it has been join()ed. | 
 | 277 |         enum = threading.enumerate | 
 | 278 |         old_interval = sys.getcheckinterval() | 
| Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 279 |         try: | 
| Jeffrey Yasskin | ca67412 | 2008-03-29 05:06:52 +0000 | [diff] [blame] | 280 |             for i in range(1, 100): | 
 | 281 |                 # Try a couple times at each thread-switching interval | 
 | 282 |                 # to get more interleavings. | 
 | 283 |                 sys.setcheckinterval(i // 5) | 
| Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 284 |                 t = threading.Thread(target=lambda: None) | 
 | 285 |                 t.start() | 
 | 286 |                 t.join() | 
 | 287 |                 l = enum() | 
 | 288 |                 self.assertFalse(t in l, | 
 | 289 |                     "#1703448 triggered after %d trials: %s" % (i, l)) | 
 | 290 |         finally: | 
 | 291 |             sys.setcheckinterval(old_interval) | 
 | 292 |  | 
| Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 293 |     def test_no_refcycle_through_target(self): | 
 | 294 |         class RunSelfFunction(object): | 
 | 295 |             def __init__(self, should_raise): | 
 | 296 |                 # The links in this refcycle from Thread back to self | 
 | 297 |                 # should be cleaned up when the thread completes. | 
 | 298 |                 self.should_raise = should_raise | 
 | 299 |                 self.thread = threading.Thread(target=self._run, | 
 | 300 |                                                args=(self,), | 
 | 301 |                                                kwargs={'yet_another':self}) | 
 | 302 |                 self.thread.start() | 
 | 303 |  | 
 | 304 |             def _run(self, other_ref, yet_another): | 
 | 305 |                 if self.should_raise: | 
 | 306 |                     raise SystemExit | 
 | 307 |  | 
 | 308 |         cyclic_object = RunSelfFunction(should_raise=False) | 
 | 309 |         weak_cyclic_object = weakref.ref(cyclic_object) | 
 | 310 |         cyclic_object.thread.join() | 
 | 311 |         del cyclic_object | 
| Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 312 |         self.assertEquals(None, weak_cyclic_object(), | 
 | 313 |                           msg=('%d references still around' % | 
 | 314 |                                sys.getrefcount(weak_cyclic_object()))) | 
| Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 315 |  | 
 | 316 |         raising_cyclic_object = RunSelfFunction(should_raise=True) | 
 | 317 |         weak_raising_cyclic_object = weakref.ref(raising_cyclic_object) | 
 | 318 |         raising_cyclic_object.thread.join() | 
 | 319 |         del raising_cyclic_object | 
| Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 320 |         self.assertEquals(None, weak_raising_cyclic_object(), | 
 | 321 |                           msg=('%d references still around' % | 
 | 322 |                                sys.getrefcount(weak_raising_cyclic_object()))) | 
| Christian Heimes | d3eb5a15 | 2008-02-24 00:38:49 +0000 | [diff] [blame] | 323 |  | 
| Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 324 |  | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 325 | class ThreadingExceptionTests(unittest.TestCase): | 
 | 326 |     # A RuntimeError should be raised if Thread.start() is called | 
 | 327 |     # multiple times. | 
 | 328 |     def test_start_thread_again(self): | 
 | 329 |         thread = threading.Thread() | 
 | 330 |         thread.start() | 
 | 331 |         self.assertRaises(RuntimeError, thread.start) | 
 | 332 |  | 
 | 333 |     def test_releasing_unacquired_rlock(self): | 
 | 334 |         rlock = threading.RLock() | 
 | 335 |         self.assertRaises(RuntimeError, rlock.release) | 
 | 336 |  | 
 | 337 |     def test_waiting_on_unacquired_condition(self): | 
 | 338 |         cond = threading.Condition() | 
 | 339 |         self.assertRaises(RuntimeError, cond.wait) | 
 | 340 |  | 
 | 341 |     def test_notify_on_unacquired_condition(self): | 
 | 342 |         cond = threading.Condition() | 
 | 343 |         self.assertRaises(RuntimeError, cond.notify) | 
 | 344 |  | 
 | 345 |     def test_semaphore_with_negative_value(self): | 
 | 346 |         self.assertRaises(ValueError, threading.Semaphore, value = -1) | 
| Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 347 |         self.assertRaises(ValueError, threading.Semaphore, value = -sys.maxsize) | 
| Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 348 |  | 
 | 349 |     def test_joining_current_thread(self): | 
 | 350 |         currentThread = threading.currentThread() | 
 | 351 |         self.assertRaises(RuntimeError, currentThread.join); | 
 | 352 |  | 
 | 353 |     def test_joining_inactive_thread(self): | 
 | 354 |         thread = threading.Thread() | 
 | 355 |         self.assertRaises(RuntimeError, thread.join) | 
 | 356 |  | 
 | 357 |     def test_daemonize_active_thread(self): | 
 | 358 |         thread = threading.Thread() | 
 | 359 |         thread.start() | 
 | 360 |         self.assertRaises(RuntimeError, thread.setDaemon, True) | 
 | 361 |  | 
 | 362 |  | 
| Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 363 | def test_main(): | 
| Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 364 |     test.support.run_unittest(ThreadTests, | 
| Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 365 |                               ThreadingExceptionTests) | 
| Tim Peters | 84d5489 | 2005-01-08 06:03:17 +0000 | [diff] [blame] | 366 |  | 
 | 367 | if __name__ == "__main__": | 
 | 368 |     test_main() |