blob: 93690a60b2ff582d50bcea5229c99fded04a82a6 [file] [log] [blame]
Brett Cannon66865d22008-03-13 20:27:00 +00001import os
2import unittest
Guido van Rossumb26a1b41998-05-20 17:05:52 +00003import random
Victor Stinner14635182018-06-04 23:53:52 +02004from test import support
5thread = support.import_module('thread')
Guido van Rossumcc544171994-04-14 20:28:41 +00006import time
Gregory P. Smith9e5d1322010-03-01 01:22:39 +00007import sys
Antoine Pitrou59c44f32009-10-30 17:07:08 +00008import weakref
Guido van Rossumcc544171994-04-14 20:28:41 +00009
Antoine Pitrouc98efe02009-11-06 22:34:35 +000010from test import lock_tests
Guido van Rossumcc544171994-04-14 20:28:41 +000011
Brett Cannon66865d22008-03-13 20:27:00 +000012NUMTASKS = 10
13NUMTRIPS = 3
Guido van Rossumd3b68421994-05-23 12:17:36 +000014
Guido van Rossumcc544171994-04-14 20:28:41 +000015
Jeffrey Yasskina1458532008-03-18 04:56:06 +000016_print_mutex = thread.allocate_lock()
17
Brett Cannon66865d22008-03-13 20:27:00 +000018def verbose_print(arg):
19 """Helper function for printing out debugging output."""
Victor Stinner14635182018-06-04 23:53:52 +020020 if support.verbose:
Jeffrey Yasskina1458532008-03-18 04:56:06 +000021 with _print_mutex:
22 print arg
Guido van Rossumcc544171994-04-14 20:28:41 +000023
Guido van Rossumcc544171994-04-14 20:28:41 +000024
grzgrzgrz36924ed52017-05-15 21:01:07 +020025
Brett Cannon66865d22008-03-13 20:27:00 +000026class BasicThreadTest(unittest.TestCase):
Guido van Rossumd3b68421994-05-23 12:17:36 +000027
Brett Cannon66865d22008-03-13 20:27:00 +000028 def setUp(self):
29 self.done_mutex = thread.allocate_lock()
30 self.done_mutex.acquire()
31 self.running_mutex = thread.allocate_lock()
32 self.random_mutex = thread.allocate_lock()
Antoine Pitroub5cf8a02009-10-23 18:32:15 +000033 self.created = 0
Brett Cannon66865d22008-03-13 20:27:00 +000034 self.running = 0
35 self.next_ident = 0
36
Victor Stinner14635182018-06-04 23:53:52 +020037 key = support.threading_setup()
38 self.addCleanup(support.threading_cleanup, *key)
grzgrzgrz36924ed52017-05-15 21:01:07 +020039
Brett Cannon66865d22008-03-13 20:27:00 +000040
41class ThreadRunningTests(BasicThreadTest):
42
43 def newtask(self):
44 with self.running_mutex:
45 self.next_ident += 1
46 verbose_print("creating task %s" % self.next_ident)
47 thread.start_new_thread(self.task, (self.next_ident,))
Antoine Pitroub5cf8a02009-10-23 18:32:15 +000048 self.created += 1
Brett Cannon66865d22008-03-13 20:27:00 +000049 self.running += 1
50
51 def task(self, ident):
52 with self.random_mutex:
Jeffrey Yasskina1458532008-03-18 04:56:06 +000053 delay = random.random() / 10000.0
54 verbose_print("task %s will run for %sus" % (ident, round(delay*1e6)))
Brett Cannon66865d22008-03-13 20:27:00 +000055 time.sleep(delay)
56 verbose_print("task %s done" % ident)
57 with self.running_mutex:
58 self.running -= 1
Antoine Pitroub5cf8a02009-10-23 18:32:15 +000059 if self.created == NUMTASKS and self.running == 0:
Brett Cannon66865d22008-03-13 20:27:00 +000060 self.done_mutex.release()
61
62 def test_starting_threads(self):
Victor Stinner14635182018-06-04 23:53:52 +020063 with support.wait_threads_exit():
64 # Basic test for thread creation.
65 for i in range(NUMTASKS):
66 self.newtask()
67 verbose_print("waiting for tasks to complete...")
68 self.done_mutex.acquire()
69 verbose_print("all tasks done")
Brett Cannon66865d22008-03-13 20:27:00 +000070
71 def test_stack_size(self):
72 # Various stack size tests.
Florent Xicluna2e6d2622010-03-20 00:17:46 +000073 self.assertEqual(thread.stack_size(), 0, "initial stack size is not 0")
Brett Cannon66865d22008-03-13 20:27:00 +000074
75 thread.stack_size(0)
Florent Xicluna2e6d2622010-03-20 00:17:46 +000076 self.assertEqual(thread.stack_size(), 0, "stack_size not reset to default")
Brett Cannon66865d22008-03-13 20:27:00 +000077
Zachary Ware1f702212013-12-10 14:09:20 -060078 @unittest.skipIf(os.name not in ("nt", "os2", "posix"), 'test meant for nt, os2, and posix')
79 def test_nt_and_posix_stack_size(self):
Brett Cannon66865d22008-03-13 20:27:00 +000080 try:
81 thread.stack_size(4096)
82 except ValueError:
83 verbose_print("caught expected ValueError setting "
84 "stack_size(4096)")
85 except thread.error:
Zachary Ware1f702212013-12-10 14:09:20 -060086 self.skipTest("platform does not support changing thread stack "
87 "size")
Brett Cannon66865d22008-03-13 20:27:00 +000088
Zachary Ware1f702212013-12-10 14:09:20 -060089 fail_msg = "stack_size(%d) failed - should succeed"
90 for tss in (262144, 0x100000, 0):
91 thread.stack_size(tss)
92 self.assertEqual(thread.stack_size(), tss, fail_msg % tss)
93 verbose_print("successfully set stack_size(%d)" % tss)
Brett Cannon66865d22008-03-13 20:27:00 +000094
Zachary Ware1f702212013-12-10 14:09:20 -060095 for tss in (262144, 0x100000):
96 verbose_print("trying stack_size = (%d)" % tss)
97 self.next_ident = 0
98 self.created = 0
Victor Stinner14635182018-06-04 23:53:52 +020099 with support.wait_threads_exit():
100 for i in range(NUMTASKS):
101 self.newtask()
Brett Cannon66865d22008-03-13 20:27:00 +0000102
Victor Stinner14635182018-06-04 23:53:52 +0200103 verbose_print("waiting for all tasks to complete")
104 self.done_mutex.acquire()
105 verbose_print("all tasks done")
Brett Cannon66865d22008-03-13 20:27:00 +0000106
Zachary Ware1f702212013-12-10 14:09:20 -0600107 thread.stack_size(0)
Brett Cannon66865d22008-03-13 20:27:00 +0000108
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000109 def test__count(self):
110 # Test the _count() function.
111 orig = thread._count()
112 mut = thread.allocate_lock()
113 mut.acquire()
114 started = []
Victor Stinner14635182018-06-04 23:53:52 +0200115
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000116 def task():
117 started.append(None)
118 mut.acquire()
119 mut.release()
Victor Stinner14635182018-06-04 23:53:52 +0200120
121 with support.wait_threads_exit():
122 thread.start_new_thread(task, ())
123 while not started:
124 time.sleep(0.01)
125 self.assertEqual(thread._count(), orig + 1)
126 # Allow the task to finish.
127 mut.release()
128 # The only reliable way to be sure that the thread ended from the
129 # interpreter's point of view is to wait for the function object to be
130 # destroyed.
131 done = []
132 wr = weakref.ref(task, lambda _: done.append(None))
133 del task
134 while not done:
135 time.sleep(0.01)
136 self.assertEqual(thread._count(), orig)
Antoine Pitrou59c44f32009-10-30 17:07:08 +0000137
Benjamin Petersonf73813a2012-04-02 11:15:17 -0400138 def test_save_exception_state_on_error(self):
139 # See issue #14474
140 def task():
141 started.release()
Benjamin Petersonf73813a2012-04-02 11:15:17 -0400142 raise SyntaxError
143 def mywrite(self, *args):
144 try:
145 raise ValueError
146 except ValueError:
147 pass
148 real_write(self, *args)
149 c = thread._count()
150 started = thread.allocate_lock()
Victor Stinner14635182018-06-04 23:53:52 +0200151 with support.captured_output("stderr") as stderr:
Benjamin Petersonf73813a2012-04-02 11:15:17 -0400152 real_write = stderr.write
153 stderr.write = mywrite
154 started.acquire()
Victor Stinner14635182018-06-04 23:53:52 +0200155 with support.wait_threads_exit():
156 thread.start_new_thread(task, ())
157 started.acquire()
Benjamin Petersonf73813a2012-04-02 11:15:17 -0400158 self.assertIn("Traceback", stderr.getvalue())
159
Brett Cannon66865d22008-03-13 20:27:00 +0000160
161class Barrier:
162 def __init__(self, num_threads):
163 self.num_threads = num_threads
Fred Drake004d5e62000-10-23 17:22:08 +0000164 self.waiting = 0
Brett Cannon66865d22008-03-13 20:27:00 +0000165 self.checkin_mutex = thread.allocate_lock()
166 self.checkout_mutex = thread.allocate_lock()
167 self.checkout_mutex.acquire()
Guido van Rossumd3b68421994-05-23 12:17:36 +0000168
Fred Drake004d5e62000-10-23 17:22:08 +0000169 def enter(self):
Brett Cannon66865d22008-03-13 20:27:00 +0000170 self.checkin_mutex.acquire()
Fred Drake004d5e62000-10-23 17:22:08 +0000171 self.waiting = self.waiting + 1
Brett Cannon66865d22008-03-13 20:27:00 +0000172 if self.waiting == self.num_threads:
173 self.waiting = self.num_threads - 1
174 self.checkout_mutex.release()
Fred Drake004d5e62000-10-23 17:22:08 +0000175 return
Brett Cannon66865d22008-03-13 20:27:00 +0000176 self.checkin_mutex.release()
Guido van Rossumd3b68421994-05-23 12:17:36 +0000177
Brett Cannon66865d22008-03-13 20:27:00 +0000178 self.checkout_mutex.acquire()
Fred Drake004d5e62000-10-23 17:22:08 +0000179 self.waiting = self.waiting - 1
180 if self.waiting == 0:
Brett Cannon66865d22008-03-13 20:27:00 +0000181 self.checkin_mutex.release()
Fred Drake004d5e62000-10-23 17:22:08 +0000182 return
Brett Cannon66865d22008-03-13 20:27:00 +0000183 self.checkout_mutex.release()
Guido van Rossumd3b68421994-05-23 12:17:36 +0000184
Guido van Rossumd3b68421994-05-23 12:17:36 +0000185
Brett Cannon66865d22008-03-13 20:27:00 +0000186class BarrierTest(BasicThreadTest):
Andrew MacIntyre92913322006-06-13 15:04:24 +0000187
Brett Cannon66865d22008-03-13 20:27:00 +0000188 def test_barrier(self):
Victor Stinner14635182018-06-04 23:53:52 +0200189 with support.wait_threads_exit():
190 self.bar = Barrier(NUMTASKS)
191 self.running = NUMTASKS
192 for i in range(NUMTASKS):
193 thread.start_new_thread(self.task2, (i,))
194 verbose_print("waiting for tasks to end")
195 self.done_mutex.acquire()
196 verbose_print("tasks done")
Andrew MacIntyre92913322006-06-13 15:04:24 +0000197
Brett Cannon66865d22008-03-13 20:27:00 +0000198 def task2(self, ident):
199 for i in range(NUMTRIPS):
200 if ident == 0:
201 # give it a good chance to enter the next
202 # barrier before the others are all out
203 # of the current one
Jeffrey Yasskina1458532008-03-18 04:56:06 +0000204 delay = 0
Brett Cannon66865d22008-03-13 20:27:00 +0000205 else:
206 with self.random_mutex:
Jeffrey Yasskina1458532008-03-18 04:56:06 +0000207 delay = random.random() / 10000.0
208 verbose_print("task %s will run for %sus" %
209 (ident, round(delay * 1e6)))
Brett Cannon66865d22008-03-13 20:27:00 +0000210 time.sleep(delay)
211 verbose_print("task %s entering %s" % (ident, i))
212 self.bar.enter()
213 verbose_print("task %s leaving barrier" % ident)
214 with self.running_mutex:
215 self.running -= 1
216 # Must release mutex before releasing done, else the main thread can
217 # exit and set mutex to None as part of global teardown; then
218 # mutex.release() raises AttributeError.
219 finished = self.running == 0
220 if finished:
221 self.done_mutex.release()
Andrew MacIntyre92913322006-06-13 15:04:24 +0000222
Andrew MacIntyre92913322006-06-13 15:04:24 +0000223
Antoine Pitrouc98efe02009-11-06 22:34:35 +0000224class LockTests(lock_tests.LockTests):
225 locktype = thread.allocate_lock
226
227
Gregory P. Smith9e5d1322010-03-01 01:22:39 +0000228class TestForkInThread(unittest.TestCase):
229 def setUp(self):
230 self.read_fd, self.write_fd = os.pipe()
231
Gregory P. Smith7512a902010-03-01 06:01:02 +0000232 @unittest.skipIf(sys.platform.startswith('win'),
Florent Xicluna2e6d2622010-03-20 00:17:46 +0000233 "This test is only appropriate for POSIX-like systems.")
Victor Stinner14635182018-06-04 23:53:52 +0200234 @support.reap_threads
Gregory P. Smith9e5d1322010-03-01 01:22:39 +0000235 def test_forkinthread(self):
Victor Stinner14635182018-06-04 23:53:52 +0200236 non_local = {'status': None}
Gregory P. Smith9e5d1322010-03-01 01:22:39 +0000237 def thread1():
238 try:
239 pid = os.fork() # fork in a thread
240 except RuntimeError:
241 sys.exit(0) # exit the child
242
243 if pid == 0: # child
244 os.close(self.read_fd)
245 os.write(self.write_fd, "OK")
Martin Panter20c8cd92016-03-08 07:07:28 +0000246 # Exiting the thread normally in the child process can leave
247 # any additional threads (such as the one started by
248 # importing _tkinter) still running, and this can prevent
249 # the half-zombie child process from being cleaned up. See
250 # Issue #26456.
251 os._exit(0)
Gregory P. Smith9e5d1322010-03-01 01:22:39 +0000252 else: # parent
253 os.close(self.write_fd)
Ammar Askar425680b2017-08-09 10:54:53 -0400254 pid, status = os.waitpid(pid, 0)
Victor Stinner14635182018-06-04 23:53:52 +0200255 non_local['status'] = status
Gregory P. Smith9e5d1322010-03-01 01:22:39 +0000256
Victor Stinner14635182018-06-04 23:53:52 +0200257 with support.wait_threads_exit():
258 thread.start_new_thread(thread1, ())
259 self.assertEqual(os.read(self.read_fd, 2), "OK",
260 "Unable to fork() in thread")
261 self.assertEqual(non_local['status'], 0)
Gregory P. Smith9e5d1322010-03-01 01:22:39 +0000262
263 def tearDown(self):
264 try:
265 os.close(self.read_fd)
266 except OSError:
267 pass
268
269 try:
270 os.close(self.write_fd)
271 except OSError:
272 pass
273
274
Brett Cannon66865d22008-03-13 20:27:00 +0000275def test_main():
Victor Stinner14635182018-06-04 23:53:52 +0200276 support.run_unittest(ThreadRunningTests, BarrierTest, LockTests,
Gregory P. Smith9e5d1322010-03-01 01:22:39 +0000277 TestForkInThread)
Andrew MacIntyre92913322006-06-13 15:04:24 +0000278
Brett Cannon66865d22008-03-13 20:27:00 +0000279if __name__ == "__main__":
280 test_main()