Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1 | import os |
| 2 | import unittest |
Guido van Rossum | b26a1b4 | 1998-05-20 17:05:52 +0000 | [diff] [blame] | 3 | import random |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 5 | import _thread as thread |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 6 | import time |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 7 | import sys |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 8 | import weakref |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 9 | |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 10 | from test import lock_tests |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 11 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 12 | NUMTASKS = 10 |
| 13 | NUMTRIPS = 3 |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 14 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 15 | _print_mutex = thread.allocate_lock() |
| 16 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 17 | def verbose_print(arg): |
| 18 | """Helper function for printing out debugging output.""" |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 19 | if support.verbose: |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 20 | with _print_mutex: |
| 21 | print(arg) |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 22 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 23 | class BasicThreadTest(unittest.TestCase): |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 24 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 25 | def setUp(self): |
| 26 | self.done_mutex = thread.allocate_lock() |
| 27 | self.done_mutex.acquire() |
| 28 | self.running_mutex = thread.allocate_lock() |
| 29 | self.random_mutex = thread.allocate_lock() |
Antoine Pitrou | 97115d1 | 2009-10-23 18:34:17 +0000 | [diff] [blame] | 30 | self.created = 0 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 31 | self.running = 0 |
| 32 | self.next_ident = 0 |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 34 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 35 | class ThreadRunningTests(BasicThreadTest): |
| 36 | |
| 37 | def newtask(self): |
| 38 | with self.running_mutex: |
| 39 | self.next_ident += 1 |
| 40 | verbose_print("creating task %s" % self.next_ident) |
| 41 | thread.start_new_thread(self.task, (self.next_ident,)) |
Antoine Pitrou | 97115d1 | 2009-10-23 18:34:17 +0000 | [diff] [blame] | 42 | self.created += 1 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 43 | self.running += 1 |
| 44 | |
| 45 | def task(self, ident): |
| 46 | with self.random_mutex: |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 47 | delay = random.random() / 10000.0 |
| 48 | verbose_print("task %s will run for %sus" % (ident, round(delay*1e6))) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 49 | time.sleep(delay) |
| 50 | verbose_print("task %s done" % ident) |
| 51 | with self.running_mutex: |
| 52 | self.running -= 1 |
Antoine Pitrou | 97115d1 | 2009-10-23 18:34:17 +0000 | [diff] [blame] | 53 | if self.created == NUMTASKS and self.running == 0: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 54 | self.done_mutex.release() |
| 55 | |
| 56 | def test_starting_threads(self): |
| 57 | # Basic test for thread creation. |
| 58 | for i in range(NUMTASKS): |
| 59 | self.newtask() |
| 60 | verbose_print("waiting for tasks to complete...") |
| 61 | self.done_mutex.acquire() |
| 62 | verbose_print("all tasks done") |
| 63 | |
| 64 | def test_stack_size(self): |
| 65 | # Various stack size tests. |
Florent Xicluna | 6709c0c | 2010-03-20 00:21:04 +0000 | [diff] [blame^] | 66 | self.assertEqual(thread.stack_size(), 0, "initial stack size is not 0") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 67 | |
| 68 | thread.stack_size(0) |
Florent Xicluna | 6709c0c | 2010-03-20 00:21:04 +0000 | [diff] [blame^] | 69 | self.assertEqual(thread.stack_size(), 0, "stack_size not reset to default") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 70 | |
| 71 | if os.name not in ("nt", "os2", "posix"): |
| 72 | return |
| 73 | |
| 74 | tss_supported = True |
| 75 | try: |
| 76 | thread.stack_size(4096) |
| 77 | except ValueError: |
| 78 | verbose_print("caught expected ValueError setting " |
| 79 | "stack_size(4096)") |
| 80 | except thread.error: |
| 81 | tss_supported = False |
| 82 | verbose_print("platform does not support changing thread stack " |
| 83 | "size") |
| 84 | |
| 85 | if tss_supported: |
| 86 | fail_msg = "stack_size(%d) failed - should succeed" |
| 87 | for tss in (262144, 0x100000, 0): |
| 88 | thread.stack_size(tss) |
Florent Xicluna | 6709c0c | 2010-03-20 00:21:04 +0000 | [diff] [blame^] | 89 | self.assertEqual(thread.stack_size(), tss, fail_msg % tss) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 90 | verbose_print("successfully set stack_size(%d)" % tss) |
| 91 | |
| 92 | for tss in (262144, 0x100000): |
| 93 | verbose_print("trying stack_size = (%d)" % tss) |
| 94 | self.next_ident = 0 |
Antoine Pitrou | 97115d1 | 2009-10-23 18:34:17 +0000 | [diff] [blame] | 95 | self.created = 0 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 96 | for i in range(NUMTASKS): |
| 97 | self.newtask() |
| 98 | |
| 99 | verbose_print("waiting for all tasks to complete") |
| 100 | self.done_mutex.acquire() |
| 101 | verbose_print("all tasks done") |
| 102 | |
| 103 | thread.stack_size(0) |
| 104 | |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 105 | def test__count(self): |
| 106 | # Test the _count() function. |
| 107 | orig = thread._count() |
| 108 | mut = thread.allocate_lock() |
| 109 | mut.acquire() |
| 110 | started = [] |
| 111 | def task(): |
| 112 | started.append(None) |
| 113 | mut.acquire() |
| 114 | mut.release() |
| 115 | thread.start_new_thread(task, ()) |
| 116 | while not started: |
| 117 | time.sleep(0.01) |
Florent Xicluna | 6709c0c | 2010-03-20 00:21:04 +0000 | [diff] [blame^] | 118 | self.assertEqual(thread._count(), orig + 1) |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 119 | # Allow the task to finish. |
| 120 | mut.release() |
| 121 | # The only reliable way to be sure that the thread ended from the |
| 122 | # interpreter's point of view is to wait for the function object to be |
| 123 | # destroyed. |
| 124 | done = [] |
| 125 | wr = weakref.ref(task, lambda _: done.append(None)) |
| 126 | del task |
| 127 | while not done: |
| 128 | time.sleep(0.01) |
Florent Xicluna | 6709c0c | 2010-03-20 00:21:04 +0000 | [diff] [blame^] | 129 | self.assertEqual(thread._count(), orig) |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 130 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 131 | |
| 132 | class Barrier: |
| 133 | def __init__(self, num_threads): |
| 134 | self.num_threads = num_threads |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 135 | self.waiting = 0 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 136 | self.checkin_mutex = thread.allocate_lock() |
| 137 | self.checkout_mutex = thread.allocate_lock() |
| 138 | self.checkout_mutex.acquire() |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 139 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 140 | def enter(self): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 141 | self.checkin_mutex.acquire() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 142 | self.waiting = self.waiting + 1 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 143 | if self.waiting == self.num_threads: |
| 144 | self.waiting = self.num_threads - 1 |
| 145 | self.checkout_mutex.release() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 146 | return |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 147 | self.checkin_mutex.release() |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 148 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 149 | self.checkout_mutex.acquire() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 150 | self.waiting = self.waiting - 1 |
| 151 | if self.waiting == 0: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 152 | self.checkin_mutex.release() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 153 | return |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 154 | self.checkout_mutex.release() |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 156 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 157 | class BarrierTest(BasicThreadTest): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 158 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 159 | def test_barrier(self): |
| 160 | self.bar = Barrier(NUMTASKS) |
| 161 | self.running = NUMTASKS |
| 162 | for i in range(NUMTASKS): |
| 163 | thread.start_new_thread(self.task2, (i,)) |
| 164 | verbose_print("waiting for tasks to end") |
| 165 | self.done_mutex.acquire() |
| 166 | verbose_print("tasks done") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 167 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 168 | def task2(self, ident): |
| 169 | for i in range(NUMTRIPS): |
| 170 | if ident == 0: |
| 171 | # give it a good chance to enter the next |
| 172 | # barrier before the others are all out |
| 173 | # of the current one |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 174 | delay = 0 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 175 | else: |
| 176 | with self.random_mutex: |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 177 | delay = random.random() / 10000.0 |
| 178 | verbose_print("task %s will run for %sus" % |
| 179 | (ident, round(delay * 1e6))) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 180 | time.sleep(delay) |
| 181 | verbose_print("task %s entering %s" % (ident, i)) |
| 182 | self.bar.enter() |
| 183 | verbose_print("task %s leaving barrier" % ident) |
| 184 | with self.running_mutex: |
| 185 | self.running -= 1 |
| 186 | # Must release mutex before releasing done, else the main thread can |
| 187 | # exit and set mutex to None as part of global teardown; then |
| 188 | # mutex.release() raises AttributeError. |
| 189 | finished = self.running == 0 |
| 190 | if finished: |
| 191 | self.done_mutex.release() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 192 | |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 193 | class LockTests(lock_tests.LockTests): |
| 194 | locktype = thread.allocate_lock |
| 195 | |
| 196 | |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 197 | class TestForkInThread(unittest.TestCase): |
| 198 | def setUp(self): |
| 199 | self.read_fd, self.write_fd = os.pipe() |
| 200 | |
| 201 | @unittest.skipIf(sys.platform.startswith('win'), |
Florent Xicluna | 6709c0c | 2010-03-20 00:21:04 +0000 | [diff] [blame^] | 202 | "This test is only appropriate for POSIX-like systems.") |
| 203 | @support.reap_threads |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 204 | def test_forkinthread(self): |
| 205 | def thread1(): |
| 206 | try: |
| 207 | pid = os.fork() # fork in a thread |
| 208 | except RuntimeError: |
| 209 | os._exit(1) # exit the child |
| 210 | |
| 211 | if pid == 0: # child |
| 212 | try: |
| 213 | os.close(self.read_fd) |
| 214 | os.write(self.write_fd, b"OK") |
| 215 | finally: |
| 216 | os._exit(0) |
| 217 | else: # parent |
| 218 | os.close(self.write_fd) |
| 219 | |
| 220 | thread.start_new_thread(thread1, ()) |
| 221 | self.assertEqual(os.read(self.read_fd, 2), b"OK", |
| 222 | "Unable to fork() in thread") |
| 223 | |
| 224 | def tearDown(self): |
| 225 | try: |
| 226 | os.close(self.read_fd) |
| 227 | except OSError: |
| 228 | pass |
| 229 | |
| 230 | try: |
| 231 | os.close(self.write_fd) |
| 232 | except OSError: |
| 233 | pass |
| 234 | |
| 235 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 236 | def test_main(): |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 237 | support.run_unittest(ThreadRunningTests, BarrierTest, LockTests, |
| 238 | TestForkInThread) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 239 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 240 | if __name__ == "__main__": |
| 241 | test_main() |