blob: 9f4801f47e3aaa456c87a713d71e8021dd7932e4 [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
Victor Stinner8b095002019-05-29 02:57:56 +0200136 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 Heimesdd15f6c2008-03-16 00:07:10 +0000154
155class Barrier:
156 def __init__(self, num_threads):
157 self.num_threads = num_threads
Fred Drake004d5e62000-10-23 17:22:08 +0000158 self.waiting = 0
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000159 self.checkin_mutex = thread.allocate_lock()
160 self.checkout_mutex = thread.allocate_lock()
161 self.checkout_mutex.acquire()
Guido van Rossumd3b68421994-05-23 12:17:36 +0000162
Fred Drake004d5e62000-10-23 17:22:08 +0000163 def enter(self):
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000164 self.checkin_mutex.acquire()
Fred Drake004d5e62000-10-23 17:22:08 +0000165 self.waiting = self.waiting + 1
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000166 if self.waiting == self.num_threads:
167 self.waiting = self.num_threads - 1
168 self.checkout_mutex.release()
Fred Drake004d5e62000-10-23 17:22:08 +0000169 return
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000170 self.checkin_mutex.release()
Guido van Rossumd3b68421994-05-23 12:17:36 +0000171
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000172 self.checkout_mutex.acquire()
Fred Drake004d5e62000-10-23 17:22:08 +0000173 self.waiting = self.waiting - 1
174 if self.waiting == 0:
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000175 self.checkin_mutex.release()
Fred Drake004d5e62000-10-23 17:22:08 +0000176 return
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000177 self.checkout_mutex.release()
Guido van Rossumd3b68421994-05-23 12:17:36 +0000178
Guido van Rossumd3b68421994-05-23 12:17:36 +0000179
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000180class BarrierTest(BasicThreadTest):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000181
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000182 def test_barrier(self):
Victor Stinnerff40ecd2017-09-14 13:07:24 -0700183 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 Wouters0e3f5912006-08-11 14:57:12 +0000191
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000192 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 Heimesb186d002008-03-18 15:15:01 +0000198 delay = 0
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000199 else:
200 with self.random_mutex:
Christian Heimesb186d002008-03-18 15:15:01 +0000201 delay = random.random() / 10000.0
202 verbose_print("task %s will run for %sus" %
203 (ident, round(delay * 1e6)))
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000204 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 Wouters0e3f5912006-08-11 14:57:12 +0000216
Antoine Pitrou557934f2009-11-06 22:41:14 +0000217class LockTests(lock_tests.LockTests):
218 locktype = thread.allocate_lock
219
220
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000221class TestForkInThread(unittest.TestCase):
222 def setUp(self):
223 self.read_fd, self.write_fd = os.pipe()
224
Victor Stinnera15d1552017-09-12 10:49:22 -0700225 @unittest.skipUnless(hasattr(os, 'fork'), 'need os.fork')
Florent Xicluna6709c0c2010-03-20 00:21:04 +0000226 @support.reap_threads
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000227 def test_forkinthread(self):
Victor Stinnera15d1552017-09-12 10:49:22 -0700228 status = "not set"
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000229
Victor Stinnera15d1552017-09-12 10:49:22 -0700230 def thread1():
Victor Stinnerff40ecd2017-09-14 13:07:24 -0700231 nonlocal status
Victor Stinnera15d1552017-09-12 10:49:22 -0700232
233 # fork in a thread
234 pid = os.fork()
235 if pid == 0:
236 # child
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000237 try:
238 os.close(self.read_fd)
239 os.write(self.write_fd, b"OK")
240 finally:
241 os._exit(0)
Victor Stinnera15d1552017-09-12 10:49:22 -0700242 else:
243 # parent
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000244 os.close(self.write_fd)
Ammar Askar88eee442017-08-09 04:51:43 -0400245 pid, status = os.waitpid(pid, 0)
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000246
Victor Stinnerff40ecd2017-09-14 13:07:24 -0700247 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 Stinnera15d1552017-09-12 10:49:22 -0700251 self.assertEqual(status, 0)
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000252
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 Heimesdd15f6c2008-03-16 00:07:10 +0000265if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500266 unittest.main()