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