blob: 4e0a572c853aa4180a370cf572f65dca3a3dfae2 [file] [log] [blame]
Skip Montanaro4533f602001-08-20 20:28:48 +00001# Very rudimentary test of threading module
2
Tim Peters84d54892005-01-08 06:03:17 +00003import test.test_support
Barry Warsaw04f357c2002-07-23 19:04:11 +00004from test.test_support import verbose
Skip Montanaro4533f602001-08-20 20:28:48 +00005import random
Gregory P. Smith8856dda2008-06-01 23:48:47 +00006import re
Collin Winter50b79ce2007-06-06 00:17:35 +00007import sys
Skip Montanaro4533f602001-08-20 20:28:48 +00008import threading
Tim Peters711906e2005-01-08 07:30:42 +00009import thread
Skip Montanaro4533f602001-08-20 20:28:48 +000010import time
Tim Peters84d54892005-01-08 06:03:17 +000011import unittest
Jeffrey Yasskin3414ea92008-02-23 19:40:54 +000012import weakref
Skip Montanaro4533f602001-08-20 20:28:48 +000013
Antoine Pitrouc98efe02009-11-06 22:34:35 +000014from test import lock_tests
15
Tim Peters84d54892005-01-08 06:03:17 +000016# A trivial mutable counter.
17class 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 Montanaro4533f602001-08-20 20:28:48 +000026
27class TestThread(threading.Thread):
Tim Peters84d54892005-01-08 06:03:17 +000028 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 Montanaro4533f602001-08-20 20:28:48 +000035 def run(self):
Jeffrey Yasskin510eab52008-03-21 18:48:04 +000036 delay = random.random() / 10000.0
Skip Montanaro4533f602001-08-20 20:28:48 +000037 if verbose:
Jeffrey Yasskin510eab52008-03-21 18:48:04 +000038 print 'task %s will run for %.1f usec' % (
Benjamin Petersoncbae8692008-08-18 17:45:09 +000039 self.name, delay * 1e6)
Tim Peters84d54892005-01-08 06:03:17 +000040
Jeffrey Yasskin510eab52008-03-21 18:48:04 +000041 with self.sema:
42 with self.mutex:
43 self.nrunning.inc()
44 if verbose:
45 print self.nrunning.get(), 'tasks are running'
Benjamin Peterson5c8da862009-06-30 22:57:08 +000046 self.testcase.assertTrue(self.nrunning.get() <= 3)
Tim Peters84d54892005-01-08 06:03:17 +000047
Jeffrey Yasskin510eab52008-03-21 18:48:04 +000048 time.sleep(delay)
49 if verbose:
Benjamin Petersoncbae8692008-08-18 17:45:09 +000050 print 'task', self.name, 'done'
Tim Peters84d54892005-01-08 06:03:17 +000051
Jeffrey Yasskin510eab52008-03-21 18:48:04 +000052 with self.mutex:
53 self.nrunning.dec()
Benjamin Peterson5c8da862009-06-30 22:57:08 +000054 self.testcase.assertTrue(self.nrunning.get() >= 0)
Jeffrey Yasskin510eab52008-03-21 18:48:04 +000055 if verbose:
56 print '%s is finished. %d tasks are running' % (
Benjamin Petersoncbae8692008-08-18 17:45:09 +000057 self.name, self.nrunning.get())
Skip Montanaro4533f602001-08-20 20:28:48 +000058
Antoine Pitroubb0bb302009-10-27 20:02:23 +000059class BaseTestCase(unittest.TestCase):
60 def setUp(self):
61 self._threads = test.test_support.threading_setup()
62
63 def tearDown(self):
64 test.test_support.threading_cleanup(*self._threads)
65 test.test_support.reap_children()
66
67
68class ThreadTests(BaseTestCase):
Skip Montanaro4533f602001-08-20 20:28:48 +000069
Tim Peters84d54892005-01-08 06:03:17 +000070 # Create a bunch of threads, let each do some work, wait until all are
71 # done.
72 def test_various_ops(self):
73 # This takes about n/3 seconds to run (about n/3 clumps of tasks,
74 # times about 1 second per clump).
75 NUMTASKS = 10
76
77 # no more than 3 of the 10 can run at once
78 sema = threading.BoundedSemaphore(value=3)
79 mutex = threading.RLock()
80 numrunning = Counter()
81
82 threads = []
83
84 for i in range(NUMTASKS):
85 t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning)
86 threads.append(t)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000087 self.assertEqual(t.ident, None)
88 self.assertTrue(re.match('<TestThread\(.*, initial\)>', repr(t)))
Tim Peters84d54892005-01-08 06:03:17 +000089 t.start()
90
91 if verbose:
92 print 'waiting for all tasks to complete'
93 for t in threads:
Tim Peters711906e2005-01-08 07:30:42 +000094 t.join(NUMTASKS)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000095 self.assertTrue(not t.is_alive())
96 self.assertNotEqual(t.ident, 0)
Benjamin Petersond906ea62009-03-31 21:34:42 +000097 self.assertFalse(t.ident is None)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000098 self.assertTrue(re.match('<TestThread\(.*, \w+ -?\d+\)>', repr(t)))
Tim Peters84d54892005-01-08 06:03:17 +000099 if verbose:
100 print 'all tasks done'
101 self.assertEqual(numrunning.get(), 0)
102
Benjamin Petersond906ea62009-03-31 21:34:42 +0000103 def test_ident_of_no_threading_threads(self):
104 # The ident still must work for the main thread and dummy threads.
105 self.assertFalse(threading.currentThread().ident is None)
106 def f():
107 ident.append(threading.currentThread().ident)
108 done.set()
109 done = threading.Event()
110 ident = []
111 thread.start_new_thread(f, ())
112 done.wait()
113 self.assertFalse(ident[0] is None)
Antoine Pitrou00253302009-11-08 00:24:12 +0000114 # Kill the "immortal" _DummyThread
115 del threading._active[ident[0]]
Benjamin Petersond906ea62009-03-31 21:34:42 +0000116
Andrew MacIntyre93e3ecb2006-06-13 19:02:35 +0000117 # run with a small(ish) thread stack size (256kB)
Andrew MacIntyre92913322006-06-13 15:04:24 +0000118 def test_various_ops_small_stack(self):
119 if verbose:
Andrew MacIntyre93e3ecb2006-06-13 19:02:35 +0000120 print 'with 256kB thread stack size...'
Andrew MacIntyre16ee33a2006-08-06 12:37:03 +0000121 try:
122 threading.stack_size(262144)
123 except thread.error:
124 if verbose:
125 print 'platform does not support changing thread stack size'
126 return
Andrew MacIntyre92913322006-06-13 15:04:24 +0000127 self.test_various_ops()
128 threading.stack_size(0)
129
130 # run with a large thread stack size (1MB)
131 def test_various_ops_large_stack(self):
132 if verbose:
133 print 'with 1MB thread stack size...'
Andrew MacIntyre16ee33a2006-08-06 12:37:03 +0000134 try:
135 threading.stack_size(0x100000)
136 except thread.error:
137 if verbose:
138 print 'platform does not support changing thread stack size'
139 return
Andrew MacIntyre92913322006-06-13 15:04:24 +0000140 self.test_various_ops()
141 threading.stack_size(0)
142
Tim Peters711906e2005-01-08 07:30:42 +0000143 def test_foreign_thread(self):
144 # Check that a "foreign" thread can use the threading module.
145 def f(mutex):
146 # Acquiring an RLock forces an entry for the foreign
147 # thread to get made in the threading._active map.
148 r = threading.RLock()
149 r.acquire()
150 r.release()
151 mutex.release()
152
153 mutex = threading.Lock()
154 mutex.acquire()
155 tid = thread.start_new_thread(f, (mutex,))
156 # Wait for the thread to finish.
157 mutex.acquire()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000158 self.assertTrue(tid in threading._active)
159 self.assertTrue(isinstance(threading._active[tid],
Tim Peters711906e2005-01-08 07:30:42 +0000160 threading._DummyThread))
161 del threading._active[tid]
Tim Peters84d54892005-01-08 06:03:17 +0000162
Tim Peters4643c2f2006-08-10 22:45:34 +0000163 # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently)
164 # exposed at the Python level. This test relies on ctypes to get at it.
165 def test_PyThreadState_SetAsyncExc(self):
166 try:
167 import ctypes
168 except ImportError:
169 if verbose:
170 print "test_PyThreadState_SetAsyncExc can't import ctypes"
171 return # can't do anything
172
173 set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc
174
175 class AsyncExc(Exception):
176 pass
177
178 exception = ctypes.py_object(AsyncExc)
179
Antoine Pitrou8a172b12009-10-18 18:22:04 +0000180 # First check it works when setting the exception from the same thread.
181 tid = thread.get_ident()
182
183 try:
184 result = set_async_exc(ctypes.c_long(tid), exception)
185 # The exception is async, so we might have to keep the VM busy until
186 # it notices.
187 while True:
188 pass
189 except AsyncExc:
190 pass
191 else:
Antoine Pitrou603acf92009-10-18 18:37:11 +0000192 # This code is unreachable but it reflects the intent. If we wanted
193 # to be smarter the above loop wouldn't be infinite.
Antoine Pitrou8a172b12009-10-18 18:22:04 +0000194 self.fail("AsyncExc not raised")
195 try:
196 self.assertEqual(result, 1) # one thread state modified
197 except UnboundLocalError:
Antoine Pitrou603acf92009-10-18 18:37:11 +0000198 # The exception was raised too quickly for us to get the result.
Antoine Pitrou8a172b12009-10-18 18:22:04 +0000199 pass
200
Tim Peters4643c2f2006-08-10 22:45:34 +0000201 # `worker_started` is set by the thread when it's inside a try/except
202 # block waiting to catch the asynchronously set AsyncExc exception.
203 # `worker_saw_exception` is set by the thread upon catching that
204 # exception.
205 worker_started = threading.Event()
206 worker_saw_exception = threading.Event()
207
208 class Worker(threading.Thread):
209 def run(self):
210 self.id = thread.get_ident()
211 self.finished = False
212
213 try:
214 while True:
215 worker_started.set()
216 time.sleep(0.1)
217 except AsyncExc:
218 self.finished = True
219 worker_saw_exception.set()
220
221 t = Worker()
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000222 t.daemon = True # so if this fails, we don't hang Python at shutdown
Tim Peters08574772006-08-11 00:49:01 +0000223 t.start()
Tim Peters4643c2f2006-08-10 22:45:34 +0000224 if verbose:
225 print " started worker thread"
Tim Peters4643c2f2006-08-10 22:45:34 +0000226
227 # Try a thread id that doesn't make sense.
228 if verbose:
229 print " trying nonsensical thread id"
Tim Peters08574772006-08-11 00:49:01 +0000230 result = set_async_exc(ctypes.c_long(-1), exception)
Tim Peters4643c2f2006-08-10 22:45:34 +0000231 self.assertEqual(result, 0) # no thread states modified
232
233 # Now raise an exception in the worker thread.
234 if verbose:
235 print " waiting for worker thread to get started"
Georg Brandlef660e82009-03-31 20:41:08 +0000236 ret = worker_started.wait()
237 self.assertTrue(ret)
Tim Peters4643c2f2006-08-10 22:45:34 +0000238 if verbose:
239 print " verifying worker hasn't exited"
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000240 self.assertTrue(not t.finished)
Tim Peters4643c2f2006-08-10 22:45:34 +0000241 if verbose:
242 print " attempting to raise asynch exception in worker"
Tim Peters08574772006-08-11 00:49:01 +0000243 result = set_async_exc(ctypes.c_long(t.id), exception)
Tim Peters4643c2f2006-08-10 22:45:34 +0000244 self.assertEqual(result, 1) # one thread state modified
245 if verbose:
246 print " waiting for worker to say it caught the exception"
247 worker_saw_exception.wait(timeout=10)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000248 self.assertTrue(t.finished)
Tim Peters4643c2f2006-08-10 22:45:34 +0000249 if verbose:
250 print " all OK -- joining worker"
251 if t.finished:
252 t.join()
253 # else the thread is still running, and we have no way to kill it
254
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000255 def test_finalize_runnning_thread(self):
256 # Issue 1402: the PyGILState_Ensure / _Release functions may be called
257 # very late on python exit: on deallocation of a running thread for
258 # example.
259 try:
260 import ctypes
261 except ImportError:
262 if verbose:
263 print("test_finalize_with_runnning_thread can't import ctypes")
264 return # can't do anything
265
266 import subprocess
267 rc = subprocess.call([sys.executable, "-c", """if 1:
268 import ctypes, sys, time, thread
269
Jeffrey Yasskin510eab52008-03-21 18:48:04 +0000270 # This lock is used as a simple event variable.
271 ready = thread.allocate_lock()
272 ready.acquire()
273
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000274 # Module globals are cleared before __del__ is run
275 # So we save the functions in class dict
276 class C:
277 ensure = ctypes.pythonapi.PyGILState_Ensure
278 release = ctypes.pythonapi.PyGILState_Release
279 def __del__(self):
280 state = self.ensure()
281 self.release(state)
282
283 def waitingThread():
284 x = C()
Jeffrey Yasskin510eab52008-03-21 18:48:04 +0000285 ready.release()
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000286 time.sleep(100)
287
288 thread.start_new_thread(waitingThread, ())
Jeffrey Yasskin510eab52008-03-21 18:48:04 +0000289 ready.acquire() # Be sure the other thread is waiting.
Amaury Forgeot d'Arc025c3472007-11-29 23:35:25 +0000290 sys.exit(42)
291 """])
292 self.assertEqual(rc, 42)
293
Amaury Forgeot d'Arcd7a26512008-04-03 23:07:55 +0000294 def test_finalize_with_trace(self):
295 # Issue1733757
296 # Avoid a deadlock when sys.settrace steps into threading._shutdown
297 import subprocess
298 rc = subprocess.call([sys.executable, "-c", """if 1:
299 import sys, threading
300
301 # A deadlock-killer, to prevent the
302 # testsuite to hang forever
303 def killer():
304 import os, time
305 time.sleep(2)
306 print 'program blocked; aborting'
307 os._exit(2)
308 t = threading.Thread(target=killer)
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000309 t.daemon = True
Amaury Forgeot d'Arcd7a26512008-04-03 23:07:55 +0000310 t.start()
311
312 # This is the trace function
313 def func(frame, event, arg):
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000314 threading.current_thread()
Amaury Forgeot d'Arcd7a26512008-04-03 23:07:55 +0000315 return func
316
317 sys.settrace(func)
318 """])
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000319 self.assertFalse(rc == 2, "interpreted was blocked")
320 self.assertTrue(rc == 0, "Unexpected error")
Amaury Forgeot d'Arcd7a26512008-04-03 23:07:55 +0000321
Antoine Pitrouefb60c02009-10-20 21:29:37 +0000322 def test_join_nondaemon_on_shutdown(self):
323 # Issue 1722344
324 # Raising SystemExit skipped threading._shutdown
325 import subprocess
326 p = subprocess.Popen([sys.executable, "-c", """if 1:
327 import threading
328 from time import sleep
329
330 def child():
331 sleep(1)
332 # As a non-daemon thread we SHOULD wake up and nothing
333 # should be torn down yet
334 print "Woke up, sleep function is:", sleep
335
336 threading.Thread(target=child).start()
337 raise SystemExit
338 """],
339 stdout=subprocess.PIPE,
340 stderr=subprocess.PIPE)
341 stdout, stderr = p.communicate()
Antoine Pitroub119ca92009-10-23 12:01:13 +0000342 self.assertEqual(stdout.strip(),
343 "Woke up, sleep function is: <built-in function sleep>")
Antoine Pitrou9bd246b2009-10-20 21:59:25 +0000344 stderr = re.sub(r"^\[\d+ refs\]", "", stderr, re.MULTILINE).strip()
Antoine Pitrouefb60c02009-10-20 21:29:37 +0000345 self.assertEqual(stderr, "")
346
Gregory P. Smith95cd5c02008-01-22 01:20:42 +0000347 def test_enumerate_after_join(self):
348 # Try hard to trigger #1703448: a thread is still returned in
349 # threading.enumerate() after it has been join()ed.
350 enum = threading.enumerate
351 old_interval = sys.getcheckinterval()
Gregory P. Smith95cd5c02008-01-22 01:20:42 +0000352 try:
Jeffrey Yasskin510eab52008-03-21 18:48:04 +0000353 for i in xrange(1, 100):
354 # Try a couple times at each thread-switching interval
355 # to get more interleavings.
356 sys.setcheckinterval(i // 5)
Gregory P. Smith95cd5c02008-01-22 01:20:42 +0000357 t = threading.Thread(target=lambda: None)
358 t.start()
359 t.join()
360 l = enum()
361 self.assertFalse(t in l,
362 "#1703448 triggered after %d trials: %s" % (i, l))
363 finally:
364 sys.setcheckinterval(old_interval)
365
Jeffrey Yasskin3414ea92008-02-23 19:40:54 +0000366 def test_no_refcycle_through_target(self):
367 class RunSelfFunction(object):
Jeffrey Yasskina885c152008-02-23 20:40:35 +0000368 def __init__(self, should_raise):
Jeffrey Yasskin3414ea92008-02-23 19:40:54 +0000369 # The links in this refcycle from Thread back to self
370 # should be cleaned up when the thread completes.
Jeffrey Yasskina885c152008-02-23 20:40:35 +0000371 self.should_raise = should_raise
Jeffrey Yasskin3414ea92008-02-23 19:40:54 +0000372 self.thread = threading.Thread(target=self._run,
373 args=(self,),
374 kwargs={'yet_another':self})
375 self.thread.start()
376
377 def _run(self, other_ref, yet_another):
Jeffrey Yasskina885c152008-02-23 20:40:35 +0000378 if self.should_raise:
379 raise SystemExit
Jeffrey Yasskin3414ea92008-02-23 19:40:54 +0000380
Jeffrey Yasskina885c152008-02-23 20:40:35 +0000381 cyclic_object = RunSelfFunction(should_raise=False)
Jeffrey Yasskin3414ea92008-02-23 19:40:54 +0000382 weak_cyclic_object = weakref.ref(cyclic_object)
383 cyclic_object.thread.join()
384 del cyclic_object
Jeffrey Yasskin8b9091f2008-03-28 04:11:18 +0000385 self.assertEquals(None, weak_cyclic_object(),
386 msg=('%d references still around' %
387 sys.getrefcount(weak_cyclic_object())))
Jeffrey Yasskin3414ea92008-02-23 19:40:54 +0000388
Jeffrey Yasskina885c152008-02-23 20:40:35 +0000389 raising_cyclic_object = RunSelfFunction(should_raise=True)
390 weak_raising_cyclic_object = weakref.ref(raising_cyclic_object)
391 raising_cyclic_object.thread.join()
392 del raising_cyclic_object
Jeffrey Yasskin8b9091f2008-03-28 04:11:18 +0000393 self.assertEquals(None, weak_raising_cyclic_object(),
394 msg=('%d references still around' %
395 sys.getrefcount(weak_raising_cyclic_object())))
Jeffrey Yasskina885c152008-02-23 20:40:35 +0000396
Gregory P. Smith95cd5c02008-01-22 01:20:42 +0000397
Antoine Pitroubb0bb302009-10-27 20:02:23 +0000398class ThreadJoinOnShutdown(BaseTestCase):
Jesse Noller5e62ca42008-07-16 20:03:47 +0000399
400 def _run_and_join(self, script):
401 script = """if 1:
402 import sys, os, time, threading
403
404 # a thread, which waits for the main program to terminate
405 def joiningfunc(mainthread):
406 mainthread.join()
407 print 'end of thread'
408 \n""" + script
409
410 import subprocess
411 p = subprocess.Popen([sys.executable, "-c", script], stdout=subprocess.PIPE)
412 rc = p.wait()
Benjamin Petersonf5668f12008-07-17 12:57:22 +0000413 data = p.stdout.read().replace('\r', '')
414 self.assertEqual(data, "end of main\nend of thread\n")
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000415 self.assertFalse(rc == 2, "interpreter was blocked")
416 self.assertTrue(rc == 0, "Unexpected error")
Jesse Noller5e62ca42008-07-16 20:03:47 +0000417
418 def test_1_join_on_shutdown(self):
419 # The usual case: on exit, wait for a non-daemon thread
420 script = """if 1:
421 import os
422 t = threading.Thread(target=joiningfunc,
423 args=(threading.current_thread(),))
424 t.start()
425 time.sleep(0.1)
426 print 'end of main'
427 """
428 self._run_and_join(script)
429
430
431 def test_2_join_in_forked_process(self):
432 # Like the test above, but from a forked interpreter
433 import os
434 if not hasattr(os, 'fork'):
435 return
436 script = """if 1:
437 childpid = os.fork()
438 if childpid != 0:
439 os.waitpid(childpid, 0)
440 sys.exit(0)
441
442 t = threading.Thread(target=joiningfunc,
443 args=(threading.current_thread(),))
444 t.start()
445 print 'end of main'
446 """
447 self._run_and_join(script)
448
449 def test_3_join_in_forked_from_thread(self):
450 # Like the test above, but fork() was called from a worker thread
451 # In the forked process, the main Thread object must be marked as stopped.
452 import os
453 if not hasattr(os, 'fork'):
454 return
Gregory P. Smith08067492008-09-30 20:41:13 +0000455 # Skip platforms with known problems forking from a worker thread.
456 # See http://bugs.python.org/issue3863.
457 if sys.platform in ('freebsd4', 'freebsd5', 'freebsd6', 'os2emx'):
458 print >>sys.stderr, ('Skipping test_3_join_in_forked_from_thread'
459 ' due to known OS bugs on'), sys.platform
460 return
Jesse Noller5e62ca42008-07-16 20:03:47 +0000461 script = """if 1:
462 main_thread = threading.current_thread()
463 def worker():
464 childpid = os.fork()
465 if childpid != 0:
466 os.waitpid(childpid, 0)
467 sys.exit(0)
468
469 t = threading.Thread(target=joiningfunc,
470 args=(main_thread,))
471 print 'end of main'
472 t.start()
473 t.join() # Should not block: main_thread is already stopped
474
475 w = threading.Thread(target=worker)
476 w.start()
477 """
478 self._run_and_join(script)
479
480
Antoine Pitroubb0bb302009-10-27 20:02:23 +0000481class ThreadingExceptionTests(BaseTestCase):
Collin Winter50b79ce2007-06-06 00:17:35 +0000482 # A RuntimeError should be raised if Thread.start() is called
483 # multiple times.
484 def test_start_thread_again(self):
485 thread = threading.Thread()
486 thread.start()
487 self.assertRaises(RuntimeError, thread.start)
488
Collin Winter50b79ce2007-06-06 00:17:35 +0000489 def test_joining_current_thread(self):
Benjamin Peterson0fbcf692008-06-11 17:27:50 +0000490 current_thread = threading.current_thread()
491 self.assertRaises(RuntimeError, current_thread.join);
Collin Winter50b79ce2007-06-06 00:17:35 +0000492
493 def test_joining_inactive_thread(self):
494 thread = threading.Thread()
495 self.assertRaises(RuntimeError, thread.join)
496
497 def test_daemonize_active_thread(self):
498 thread = threading.Thread()
499 thread.start()
Benjamin Petersoncbae8692008-08-18 17:45:09 +0000500 self.assertRaises(RuntimeError, setattr, thread, "daemon", True)
Collin Winter50b79ce2007-06-06 00:17:35 +0000501
502
Antoine Pitrouc98efe02009-11-06 22:34:35 +0000503class LockTests(lock_tests.LockTests):
504 locktype = staticmethod(threading.Lock)
505
506class RLockTests(lock_tests.RLockTests):
507 locktype = staticmethod(threading.RLock)
508
509class EventTests(lock_tests.EventTests):
510 eventtype = staticmethod(threading.Event)
511
512class ConditionAsRLockTests(lock_tests.RLockTests):
513 # An Condition uses an RLock by default and exports its API.
514 locktype = staticmethod(threading.Condition)
515
516class ConditionTests(lock_tests.ConditionTests):
517 condtype = staticmethod(threading.Condition)
518
519class SemaphoreTests(lock_tests.SemaphoreTests):
520 semtype = staticmethod(threading.Semaphore)
521
522class BoundedSemaphoreTests(lock_tests.BoundedSemaphoreTests):
523 semtype = staticmethod(threading.BoundedSemaphore)
524
525
Tim Peters84d54892005-01-08 06:03:17 +0000526def test_main():
Antoine Pitrouc98efe02009-11-06 22:34:35 +0000527 test.test_support.run_unittest(LockTests, RLockTests, EventTests,
528 ConditionAsRLockTests, ConditionTests,
529 SemaphoreTests, BoundedSemaphoreTests,
530 ThreadTests,
Jesse Noller5e62ca42008-07-16 20:03:47 +0000531 ThreadJoinOnShutdown,
532 ThreadingExceptionTests,
533 )
Tim Peters84d54892005-01-08 06:03:17 +0000534
535if __name__ == "__main__":
536 test_main()