| 1 | # Very rudimentary test of threading module
|
|---|
| 2 |
|
|---|
| 3 | import test.test_support
|
|---|
| 4 | from test.test_support import verbose
|
|---|
| 5 | import random
|
|---|
| 6 | import threading
|
|---|
| 7 | import thread
|
|---|
| 8 | import time
|
|---|
| 9 | import unittest
|
|---|
| 10 |
|
|---|
| 11 | # A trivial mutable counter.
|
|---|
| 12 | class Counter(object):
|
|---|
| 13 | def __init__(self):
|
|---|
| 14 | self.value = 0
|
|---|
| 15 | def inc(self):
|
|---|
| 16 | self.value += 1
|
|---|
| 17 | def dec(self):
|
|---|
| 18 | self.value -= 1
|
|---|
| 19 | def get(self):
|
|---|
| 20 | return self.value
|
|---|
| 21 |
|
|---|
| 22 | class TestThread(threading.Thread):
|
|---|
| 23 | def __init__(self, name, testcase, sema, mutex, nrunning):
|
|---|
| 24 | threading.Thread.__init__(self, name=name)
|
|---|
| 25 | self.testcase = testcase
|
|---|
| 26 | self.sema = sema
|
|---|
| 27 | self.mutex = mutex
|
|---|
| 28 | self.nrunning = nrunning
|
|---|
| 29 |
|
|---|
| 30 | def run(self):
|
|---|
| 31 | delay = random.random() * 2
|
|---|
| 32 | if verbose:
|
|---|
| 33 | print 'task', self.getName(), 'will run for', delay, 'sec'
|
|---|
| 34 |
|
|---|
| 35 | self.sema.acquire()
|
|---|
| 36 |
|
|---|
| 37 | self.mutex.acquire()
|
|---|
| 38 | self.nrunning.inc()
|
|---|
| 39 | if verbose:
|
|---|
| 40 | print self.nrunning.get(), 'tasks are running'
|
|---|
| 41 | self.testcase.assert_(self.nrunning.get() <= 3)
|
|---|
| 42 | self.mutex.release()
|
|---|
| 43 |
|
|---|
| 44 | time.sleep(delay)
|
|---|
| 45 | if verbose:
|
|---|
| 46 | print 'task', self.getName(), 'done'
|
|---|
| 47 |
|
|---|
| 48 | self.mutex.acquire()
|
|---|
| 49 | self.nrunning.dec()
|
|---|
| 50 | self.testcase.assert_(self.nrunning.get() >= 0)
|
|---|
| 51 | if verbose:
|
|---|
| 52 | print self.getName(), 'is finished.', self.nrunning.get(), \
|
|---|
| 53 | 'tasks are running'
|
|---|
| 54 | self.mutex.release()
|
|---|
| 55 |
|
|---|
| 56 | self.sema.release()
|
|---|
| 57 |
|
|---|
| 58 | class ThreadTests(unittest.TestCase):
|
|---|
| 59 |
|
|---|
| 60 | # Create a bunch of threads, let each do some work, wait until all are
|
|---|
| 61 | # done.
|
|---|
| 62 | def test_various_ops(self):
|
|---|
| 63 | # This takes about n/3 seconds to run (about n/3 clumps of tasks,
|
|---|
| 64 | # times about 1 second per clump).
|
|---|
| 65 | NUMTASKS = 10
|
|---|
| 66 |
|
|---|
| 67 | # no more than 3 of the 10 can run at once
|
|---|
| 68 | sema = threading.BoundedSemaphore(value=3)
|
|---|
| 69 | mutex = threading.RLock()
|
|---|
| 70 | numrunning = Counter()
|
|---|
| 71 |
|
|---|
| 72 | threads = []
|
|---|
| 73 |
|
|---|
| 74 | for i in range(NUMTASKS):
|
|---|
| 75 | t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning)
|
|---|
| 76 | threads.append(t)
|
|---|
| 77 | t.start()
|
|---|
| 78 |
|
|---|
| 79 | if verbose:
|
|---|
| 80 | print 'waiting for all tasks to complete'
|
|---|
| 81 | for t in threads:
|
|---|
| 82 | t.join(NUMTASKS)
|
|---|
| 83 | self.assert_(not t.isAlive())
|
|---|
| 84 | if verbose:
|
|---|
| 85 | print 'all tasks done'
|
|---|
| 86 | self.assertEqual(numrunning.get(), 0)
|
|---|
| 87 |
|
|---|
| 88 | # run with a small(ish) thread stack size (256kB)
|
|---|
| 89 | def test_various_ops_small_stack(self):
|
|---|
| 90 | if verbose:
|
|---|
| 91 | print 'with 256kB thread stack size...'
|
|---|
| 92 | try:
|
|---|
| 93 | threading.stack_size(262144)
|
|---|
| 94 | except thread.error:
|
|---|
| 95 | if verbose:
|
|---|
| 96 | print 'platform does not support changing thread stack size'
|
|---|
| 97 | return
|
|---|
| 98 | self.test_various_ops()
|
|---|
| 99 | threading.stack_size(0)
|
|---|
| 100 |
|
|---|
| 101 | # run with a large thread stack size (1MB)
|
|---|
| 102 | def test_various_ops_large_stack(self):
|
|---|
| 103 | if verbose:
|
|---|
| 104 | print 'with 1MB thread stack size...'
|
|---|
| 105 | try:
|
|---|
| 106 | threading.stack_size(0x100000)
|
|---|
| 107 | except thread.error:
|
|---|
| 108 | if verbose:
|
|---|
| 109 | print 'platform does not support changing thread stack size'
|
|---|
| 110 | return
|
|---|
| 111 | self.test_various_ops()
|
|---|
| 112 | threading.stack_size(0)
|
|---|
| 113 |
|
|---|
| 114 | def test_foreign_thread(self):
|
|---|
| 115 | # Check that a "foreign" thread can use the threading module.
|
|---|
| 116 | def f(mutex):
|
|---|
| 117 | # Acquiring an RLock forces an entry for the foreign
|
|---|
| 118 | # thread to get made in the threading._active map.
|
|---|
| 119 | r = threading.RLock()
|
|---|
| 120 | r.acquire()
|
|---|
| 121 | r.release()
|
|---|
| 122 | mutex.release()
|
|---|
| 123 |
|
|---|
| 124 | mutex = threading.Lock()
|
|---|
| 125 | mutex.acquire()
|
|---|
| 126 | tid = thread.start_new_thread(f, (mutex,))
|
|---|
| 127 | # Wait for the thread to finish.
|
|---|
| 128 | mutex.acquire()
|
|---|
| 129 | self.assert_(tid in threading._active)
|
|---|
| 130 | self.assert_(isinstance(threading._active[tid],
|
|---|
| 131 | threading._DummyThread))
|
|---|
| 132 | del threading._active[tid]
|
|---|
| 133 |
|
|---|
| 134 | # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently)
|
|---|
| 135 | # exposed at the Python level. This test relies on ctypes to get at it.
|
|---|
| 136 | def test_PyThreadState_SetAsyncExc(self):
|
|---|
| 137 | try:
|
|---|
| 138 | import ctypes
|
|---|
| 139 | except ImportError:
|
|---|
| 140 | if verbose:
|
|---|
| 141 | print "test_PyThreadState_SetAsyncExc can't import ctypes"
|
|---|
| 142 | return # can't do anything
|
|---|
| 143 |
|
|---|
| 144 | set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc
|
|---|
| 145 |
|
|---|
| 146 | class AsyncExc(Exception):
|
|---|
| 147 | pass
|
|---|
| 148 |
|
|---|
| 149 | exception = ctypes.py_object(AsyncExc)
|
|---|
| 150 |
|
|---|
| 151 | # `worker_started` is set by the thread when it's inside a try/except
|
|---|
| 152 | # block waiting to catch the asynchronously set AsyncExc exception.
|
|---|
| 153 | # `worker_saw_exception` is set by the thread upon catching that
|
|---|
| 154 | # exception.
|
|---|
| 155 | worker_started = threading.Event()
|
|---|
| 156 | worker_saw_exception = threading.Event()
|
|---|
| 157 |
|
|---|
| 158 | class Worker(threading.Thread):
|
|---|
| 159 | def run(self):
|
|---|
| 160 | self.id = thread.get_ident()
|
|---|
| 161 | self.finished = False
|
|---|
| 162 |
|
|---|
| 163 | try:
|
|---|
| 164 | while True:
|
|---|
| 165 | worker_started.set()
|
|---|
| 166 | time.sleep(0.1)
|
|---|
| 167 | except AsyncExc:
|
|---|
| 168 | self.finished = True
|
|---|
| 169 | worker_saw_exception.set()
|
|---|
| 170 |
|
|---|
| 171 | t = Worker()
|
|---|
| 172 | t.setDaemon(True) # so if this fails, we don't hang Python at shutdown
|
|---|
| 173 | t.start()
|
|---|
| 174 | if verbose:
|
|---|
| 175 | print " started worker thread"
|
|---|
| 176 |
|
|---|
| 177 | # Try a thread id that doesn't make sense.
|
|---|
| 178 | if verbose:
|
|---|
| 179 | print " trying nonsensical thread id"
|
|---|
| 180 | result = set_async_exc(ctypes.c_long(-1), exception)
|
|---|
| 181 | self.assertEqual(result, 0) # no thread states modified
|
|---|
| 182 |
|
|---|
| 183 | # Now raise an exception in the worker thread.
|
|---|
| 184 | if verbose:
|
|---|
| 185 | print " waiting for worker thread to get started"
|
|---|
| 186 | worker_started.wait()
|
|---|
| 187 | if verbose:
|
|---|
| 188 | print " verifying worker hasn't exited"
|
|---|
| 189 | self.assert_(not t.finished)
|
|---|
| 190 | if verbose:
|
|---|
| 191 | print " attempting to raise asynch exception in worker"
|
|---|
| 192 | result = set_async_exc(ctypes.c_long(t.id), exception)
|
|---|
| 193 | self.assertEqual(result, 1) # one thread state modified
|
|---|
| 194 | if verbose:
|
|---|
| 195 | print " waiting for worker to say it caught the exception"
|
|---|
| 196 | worker_saw_exception.wait(timeout=10)
|
|---|
| 197 | self.assert_(t.finished)
|
|---|
| 198 | if verbose:
|
|---|
| 199 | print " all OK -- joining worker"
|
|---|
| 200 | if t.finished:
|
|---|
| 201 | t.join()
|
|---|
| 202 | # else the thread is still running, and we have no way to kill it
|
|---|
| 203 |
|
|---|
| 204 | def test_main():
|
|---|
| 205 | test.test_support.run_unittest(ThreadTests)
|
|---|
| 206 |
|
|---|
| 207 | if __name__ == "__main__":
|
|---|
| 208 | test_main()
|
|---|