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 | |
Benjamin Peterson | e900096 | 2012-04-02 11:15:17 -0400 | [diff] [blame] | 136 | def test_save_exception_state_on_error(self): |
| 137 | # See issue #14474 |
| 138 | def task(): |
| 139 | started.release() |
Benjamin Peterson | e900096 | 2012-04-02 11:15:17 -0400 | [diff] [blame] | 140 | raise SyntaxError |
| 141 | def mywrite(self, *args): |
| 142 | try: |
| 143 | raise ValueError |
| 144 | except ValueError: |
| 145 | pass |
| 146 | real_write(self, *args) |
Benjamin Peterson | e900096 | 2012-04-02 11:15:17 -0400 | [diff] [blame] | 147 | started = thread.allocate_lock() |
| 148 | with support.captured_output("stderr") as stderr: |
| 149 | real_write = stderr.write |
| 150 | stderr.write = mywrite |
| 151 | started.acquire() |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 152 | with support.wait_threads_exit(): |
| 153 | thread.start_new_thread(task, ()) |
| 154 | started.acquire() |
Benjamin Peterson | e900096 | 2012-04-02 11:15:17 -0400 | [diff] [blame] | 155 | self.assertIn("Traceback", stderr.getvalue()) |
| 156 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 157 | |
| 158 | class Barrier: |
| 159 | def __init__(self, num_threads): |
| 160 | self.num_threads = num_threads |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 161 | self.waiting = 0 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 162 | self.checkin_mutex = thread.allocate_lock() |
| 163 | self.checkout_mutex = thread.allocate_lock() |
| 164 | self.checkout_mutex.acquire() |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 165 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 166 | def enter(self): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 167 | self.checkin_mutex.acquire() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 168 | self.waiting = self.waiting + 1 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 169 | if self.waiting == self.num_threads: |
| 170 | self.waiting = self.num_threads - 1 |
| 171 | self.checkout_mutex.release() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 172 | return |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 173 | self.checkin_mutex.release() |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 174 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 175 | self.checkout_mutex.acquire() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 176 | self.waiting = self.waiting - 1 |
| 177 | if self.waiting == 0: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 178 | self.checkin_mutex.release() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 179 | return |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 180 | self.checkout_mutex.release() |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 182 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 183 | class BarrierTest(BasicThreadTest): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 184 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 185 | def test_barrier(self): |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 186 | with support.wait_threads_exit(): |
| 187 | self.bar = Barrier(NUMTASKS) |
| 188 | self.running = NUMTASKS |
| 189 | for i in range(NUMTASKS): |
| 190 | thread.start_new_thread(self.task2, (i,)) |
| 191 | verbose_print("waiting for tasks to end") |
| 192 | self.done_mutex.acquire() |
| 193 | verbose_print("tasks done") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 194 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 195 | def task2(self, ident): |
| 196 | for i in range(NUMTRIPS): |
| 197 | if ident == 0: |
| 198 | # give it a good chance to enter the next |
| 199 | # barrier before the others are all out |
| 200 | # of the current one |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 201 | delay = 0 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 202 | else: |
| 203 | with self.random_mutex: |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 204 | delay = random.random() / 10000.0 |
| 205 | verbose_print("task %s will run for %sus" % |
| 206 | (ident, round(delay * 1e6))) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 207 | time.sleep(delay) |
| 208 | verbose_print("task %s entering %s" % (ident, i)) |
| 209 | self.bar.enter() |
| 210 | verbose_print("task %s leaving barrier" % ident) |
| 211 | with self.running_mutex: |
| 212 | self.running -= 1 |
| 213 | # Must release mutex before releasing done, else the main thread can |
| 214 | # exit and set mutex to None as part of global teardown; then |
| 215 | # mutex.release() raises AttributeError. |
| 216 | finished = self.running == 0 |
| 217 | if finished: |
| 218 | self.done_mutex.release() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 219 | |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame] | 220 | class LockTests(lock_tests.LockTests): |
| 221 | locktype = thread.allocate_lock |
| 222 | |
| 223 | |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 224 | class TestForkInThread(unittest.TestCase): |
| 225 | def setUp(self): |
| 226 | self.read_fd, self.write_fd = os.pipe() |
| 227 | |
Victor Stinner | a15d155 | 2017-09-12 10:49:22 -0700 | [diff] [blame] | 228 | @unittest.skipUnless(hasattr(os, 'fork'), 'need os.fork') |
Florent Xicluna | 6709c0c | 2010-03-20 00:21:04 +0000 | [diff] [blame] | 229 | @support.reap_threads |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 230 | def test_forkinthread(self): |
Victor Stinner | a15d155 | 2017-09-12 10:49:22 -0700 | [diff] [blame] | 231 | status = "not set" |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 232 | |
Victor Stinner | a15d155 | 2017-09-12 10:49:22 -0700 | [diff] [blame] | 233 | def thread1(): |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 234 | nonlocal status |
Victor Stinner | a15d155 | 2017-09-12 10:49:22 -0700 | [diff] [blame] | 235 | |
| 236 | # fork in a thread |
| 237 | pid = os.fork() |
| 238 | if pid == 0: |
| 239 | # child |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 240 | try: |
| 241 | os.close(self.read_fd) |
| 242 | os.write(self.write_fd, b"OK") |
| 243 | finally: |
| 244 | os._exit(0) |
Victor Stinner | a15d155 | 2017-09-12 10:49:22 -0700 | [diff] [blame] | 245 | else: |
| 246 | # parent |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 247 | os.close(self.write_fd) |
Ammar Askar | 88eee44 | 2017-08-09 04:51:43 -0400 | [diff] [blame] | 248 | pid, status = os.waitpid(pid, 0) |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 249 | |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 250 | with support.wait_threads_exit(): |
| 251 | thread.start_new_thread(thread1, ()) |
| 252 | self.assertEqual(os.read(self.read_fd, 2), b"OK", |
| 253 | "Unable to fork() in thread") |
Victor Stinner | a15d155 | 2017-09-12 10:49:22 -0700 | [diff] [blame] | 254 | self.assertEqual(status, 0) |
Gregory P. Smith | 24cec9f | 2010-03-01 06:18:41 +0000 | [diff] [blame] | 255 | |
| 256 | def tearDown(self): |
| 257 | try: |
| 258 | os.close(self.read_fd) |
| 259 | except OSError: |
| 260 | pass |
| 261 | |
| 262 | try: |
| 263 | os.close(self.write_fd) |
| 264 | except OSError: |
| 265 | pass |
| 266 | |
| 267 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 268 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 269 | unittest.main() |