blob: f4eb830cf6d746967a9e0934a8adb15de543cad1 [file] [log] [blame]
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001import os
2import unittest
Guido van Rossumb26a1b41998-05-20 17:05:52 +00003import random
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Antoine Pitrou88c60c92017-09-18 23:50:44 +02005import _thread as thread
Guido van Rossumcc544171994-04-14 20:28:41 +00006import time
Antoine Pitrou65c9c642009-10-30 17:25:12 +00007import weakref
Guido van Rossumcc544171994-04-14 20:28:41 +00008
Antoine Pitrou557934f2009-11-06 22:41:14 +00009from test import lock_tests
Guido van Rossumcc544171994-04-14 20:28:41 +000010
Christian Heimesdd15f6c2008-03-16 00:07:10 +000011NUMTASKS = 10
12NUMTRIPS = 3
Victor Stinnera15d1552017-09-12 10:49:22 -070013POLL_SLEEP = 0.010 # seconds = 10 ms
Guido van Rossumd3b68421994-05-23 12:17:36 +000014
Christian Heimesb186d002008-03-18 15:15:01 +000015_print_mutex = thread.allocate_lock()
16
Christian Heimesdd15f6c2008-03-16 00:07:10 +000017def verbose_print(arg):
18 """Helper function for printing out debugging output."""
Benjamin Petersonee8712c2008-05-20 21:35:26 +000019 if support.verbose:
Christian Heimesb186d002008-03-18 15:15:01 +000020 with _print_mutex:
21 print(arg)
Guido van Rossumcc544171994-04-14 20:28:41 +000022
Victor Stinner79ef7f82017-05-15 17:55:32 +020023
Christian Heimesdd15f6c2008-03-16 00:07:10 +000024class BasicThreadTest(unittest.TestCase):
Guido van Rossumcc544171994-04-14 20:28:41 +000025
Christian Heimesdd15f6c2008-03-16 00:07:10 +000026 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 Pitrou97115d12009-10-23 18:34:17 +000031 self.created = 0
Christian Heimesdd15f6c2008-03-16 00:07:10 +000032 self.running = 0
33 self.next_ident = 0
Guido van Rossumcc544171994-04-14 20:28:41 +000034
Victor Stinner79ef7f82017-05-15 17:55:32 +020035 key = support.threading_setup()
36 self.addCleanup(support.threading_cleanup, *key)
37
Guido van Rossumd3b68421994-05-23 12:17:36 +000038
Christian Heimesdd15f6c2008-03-16 00:07:10 +000039class 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 Pitrou97115d12009-10-23 18:34:17 +000046 self.created += 1
Christian Heimesdd15f6c2008-03-16 00:07:10 +000047 self.running += 1
48
49 def task(self, ident):
50 with self.random_mutex:
Christian Heimesb186d002008-03-18 15:15:01 +000051 delay = random.random() / 10000.0
52 verbose_print("task %s will run for %sus" % (ident, round(delay*1e6)))
Christian Heimesdd15f6c2008-03-16 00:07:10 +000053 time.sleep(delay)
54 verbose_print("task %s done" % ident)
55 with self.running_mutex:
56 self.running -= 1
Antoine Pitrou97115d12009-10-23 18:34:17 +000057 if self.created == NUMTASKS and self.running == 0:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000058 self.done_mutex.release()
59
60 def test_starting_threads(self):
Victor Stinnerff40ecd2017-09-14 13:07:24 -070061 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 Heimesdd15f6c2008-03-16 00:07:10 +000068
69 def test_stack_size(self):
70 # Various stack size tests.
Florent Xicluna6709c0c2010-03-20 00:21:04 +000071 self.assertEqual(thread.stack_size(), 0, "initial stack size is not 0")
Christian Heimesdd15f6c2008-03-16 00:07:10 +000072
73 thread.stack_size(0)
Florent Xicluna6709c0c2010-03-20 00:21:04 +000074 self.assertEqual(thread.stack_size(), 0, "stack_size not reset to default")
Christian Heimesdd15f6c2008-03-16 00:07:10 +000075
Zachary Ware101d9e72013-12-08 00:44:27 -060076 @unittest.skipIf(os.name not in ("nt", "posix"), 'test meant for nt and posix')
Zachary Ware9fe6d862013-12-08 00:20:35 -060077 def test_nt_and_posix_stack_size(self):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000078 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 Ware9fe6d862013-12-08 00:20:35 -060084 self.skipTest("platform does not support changing thread stack "
85 "size")
Christian Heimesdd15f6c2008-03-16 00:07:10 +000086
Zachary Ware9fe6d862013-12-08 00:20:35 -060087 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 Heimesdd15f6c2008-03-16 00:07:10 +000092
Zachary Ware9fe6d862013-12-08 00:20:35 -060093 for tss in (262144, 0x100000):
94 verbose_print("trying stack_size = (%d)" % tss)
95 self.next_ident = 0
96 self.created = 0
Victor Stinnerff40ecd2017-09-14 13:07:24 -070097 with support.wait_threads_exit():
98 for i in range(NUMTASKS):
99 self.newtask()
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000100
Victor Stinnerff40ecd2017-09-14 13:07:24 -0700101 verbose_print("waiting for all tasks to complete")
102 self.done_mutex.acquire()
103 verbose_print("all tasks done")
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000104
Zachary Ware9fe6d862013-12-08 00:20:35 -0600105 thread.stack_size(0)
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000106
Antoine Pitrou65c9c642009-10-30 17:25:12 +0000107 def test__count(self):
108 # Test the _count() function.
109 orig = thread._count()
110 mut = thread.allocate_lock()
111 mut.acquire()
112 started = []
Victor Stinnerff40ecd2017-09-14 13:07:24 -0700113
Antoine Pitrou65c9c642009-10-30 17:25:12 +0000114 def task():
115 started.append(None)
116 mut.acquire()
117 mut.release()
Victor Stinnerff40ecd2017-09-14 13:07:24 -0700118
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 Pitrou65c9c642009-10-30 17:25:12 +0000135
Benjamin Petersone9000962012-04-02 11:15:17 -0400136 def test_save_exception_state_on_error(self):
137 # See issue #14474
138 def task():
139 started.release()
Benjamin Petersone9000962012-04-02 11:15:17 -0400140 raise SyntaxError
141 def mywrite(self, *args):
142 try:
143 raise ValueError
144 except ValueError:
145 pass
146 real_write(self, *args)
Benjamin Petersone9000962012-04-02 11:15:17 -0400147 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 Stinnerff40ecd2017-09-14 13:07:24 -0700152 with support.wait_threads_exit():
153 thread.start_new_thread(task, ())
154 started.acquire()
Benjamin Petersone9000962012-04-02 11:15:17 -0400155 self.assertIn("Traceback", stderr.getvalue())
156
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000157
158class Barrier:
159 def __init__(self, num_threads):
160 self.num_threads = num_threads
Fred Drake004d5e62000-10-23 17:22:08 +0000161 self.waiting = 0
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000162 self.checkin_mutex = thread.allocate_lock()
163 self.checkout_mutex = thread.allocate_lock()
164 self.checkout_mutex.acquire()
Guido van Rossumd3b68421994-05-23 12:17:36 +0000165
Fred Drake004d5e62000-10-23 17:22:08 +0000166 def enter(self):
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000167 self.checkin_mutex.acquire()
Fred Drake004d5e62000-10-23 17:22:08 +0000168 self.waiting = self.waiting + 1
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000169 if self.waiting == self.num_threads:
170 self.waiting = self.num_threads - 1
171 self.checkout_mutex.release()
Fred Drake004d5e62000-10-23 17:22:08 +0000172 return
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000173 self.checkin_mutex.release()
Guido van Rossumd3b68421994-05-23 12:17:36 +0000174
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000175 self.checkout_mutex.acquire()
Fred Drake004d5e62000-10-23 17:22:08 +0000176 self.waiting = self.waiting - 1
177 if self.waiting == 0:
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000178 self.checkin_mutex.release()
Fred Drake004d5e62000-10-23 17:22:08 +0000179 return
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000180 self.checkout_mutex.release()
Guido van Rossumd3b68421994-05-23 12:17:36 +0000181
Guido van Rossumd3b68421994-05-23 12:17:36 +0000182
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000183class BarrierTest(BasicThreadTest):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000184
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000185 def test_barrier(self):
Victor Stinnerff40ecd2017-09-14 13:07:24 -0700186 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 Wouters0e3f5912006-08-11 14:57:12 +0000194
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000195 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 Heimesb186d002008-03-18 15:15:01 +0000201 delay = 0
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000202 else:
203 with self.random_mutex:
Christian Heimesb186d002008-03-18 15:15:01 +0000204 delay = random.random() / 10000.0
205 verbose_print("task %s will run for %sus" %
206 (ident, round(delay * 1e6)))
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000207 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 Wouters0e3f5912006-08-11 14:57:12 +0000219
Antoine Pitrou557934f2009-11-06 22:41:14 +0000220class LockTests(lock_tests.LockTests):
221 locktype = thread.allocate_lock
222
223
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000224class TestForkInThread(unittest.TestCase):
225 def setUp(self):
226 self.read_fd, self.write_fd = os.pipe()
227
Victor Stinnera15d1552017-09-12 10:49:22 -0700228 @unittest.skipUnless(hasattr(os, 'fork'), 'need os.fork')
Florent Xicluna6709c0c2010-03-20 00:21:04 +0000229 @support.reap_threads
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000230 def test_forkinthread(self):
Victor Stinnera15d1552017-09-12 10:49:22 -0700231 status = "not set"
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000232
Victor Stinnera15d1552017-09-12 10:49:22 -0700233 def thread1():
Victor Stinnerff40ecd2017-09-14 13:07:24 -0700234 nonlocal status
Victor Stinnera15d1552017-09-12 10:49:22 -0700235
236 # fork in a thread
237 pid = os.fork()
238 if pid == 0:
239 # child
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000240 try:
241 os.close(self.read_fd)
242 os.write(self.write_fd, b"OK")
243 finally:
244 os._exit(0)
Victor Stinnera15d1552017-09-12 10:49:22 -0700245 else:
246 # parent
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000247 os.close(self.write_fd)
Ammar Askar88eee442017-08-09 04:51:43 -0400248 pid, status = os.waitpid(pid, 0)
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000249
Victor Stinnerff40ecd2017-09-14 13:07:24 -0700250 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 Stinnera15d1552017-09-12 10:49:22 -0700254 self.assertEqual(status, 0)
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000255
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 Heimesdd15f6c2008-03-16 00:07:10 +0000268if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500269 unittest.main()