blob: 614490199aea5bf782a646e97a83f223e086e86c [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
Victor Stinner45df8202010-04-28 22:31:17 +00005thread = support.import_module('_thread')
Guido van Rossumcc544171994-04-14 20:28:41 +00006import time
Gregory P. Smith24cec9f2010-03-01 06:18:41 +00007import sys
Antoine Pitrou65c9c642009-10-30 17:25:12 +00008import weakref
Guido van Rossumcc544171994-04-14 20:28:41 +00009
Antoine Pitrou557934f2009-11-06 22:41:14 +000010from test import lock_tests
Guido van Rossumcc544171994-04-14 20:28:41 +000011
Christian Heimesdd15f6c2008-03-16 00:07:10 +000012NUMTASKS = 10
13NUMTRIPS = 3
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
Christian Heimesdd15f6c2008-03-16 00:07:10 +000023class BasicThreadTest(unittest.TestCase):
Guido van Rossumcc544171994-04-14 20:28:41 +000024
Christian Heimesdd15f6c2008-03-16 00:07:10 +000025 def setUp(self):
26 self.done_mutex = thread.allocate_lock()
27 self.done_mutex.acquire()
28 self.running_mutex = thread.allocate_lock()
29 self.random_mutex = thread.allocate_lock()
Antoine Pitrou97115d12009-10-23 18:34:17 +000030 self.created = 0
Christian Heimesdd15f6c2008-03-16 00:07:10 +000031 self.running = 0
32 self.next_ident = 0
Guido van Rossumcc544171994-04-14 20:28:41 +000033
Guido van Rossumd3b68421994-05-23 12:17:36 +000034
Christian Heimesdd15f6c2008-03-16 00:07:10 +000035class ThreadRunningTests(BasicThreadTest):
36
37 def newtask(self):
38 with self.running_mutex:
39 self.next_ident += 1
40 verbose_print("creating task %s" % self.next_ident)
41 thread.start_new_thread(self.task, (self.next_ident,))
Antoine Pitrou97115d12009-10-23 18:34:17 +000042 self.created += 1
Christian Heimesdd15f6c2008-03-16 00:07:10 +000043 self.running += 1
44
45 def task(self, ident):
46 with self.random_mutex:
Christian Heimesb186d002008-03-18 15:15:01 +000047 delay = random.random() / 10000.0
48 verbose_print("task %s will run for %sus" % (ident, round(delay*1e6)))
Christian Heimesdd15f6c2008-03-16 00:07:10 +000049 time.sleep(delay)
50 verbose_print("task %s done" % ident)
51 with self.running_mutex:
52 self.running -= 1
Antoine Pitrou97115d12009-10-23 18:34:17 +000053 if self.created == NUMTASKS and self.running == 0:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000054 self.done_mutex.release()
55
56 def test_starting_threads(self):
57 # Basic test for thread creation.
58 for i in range(NUMTASKS):
59 self.newtask()
60 verbose_print("waiting for tasks to complete...")
61 self.done_mutex.acquire()
62 verbose_print("all tasks done")
63
64 def test_stack_size(self):
65 # Various stack size tests.
Florent Xicluna6709c0c2010-03-20 00:21:04 +000066 self.assertEqual(thread.stack_size(), 0, "initial stack size is not 0")
Christian Heimesdd15f6c2008-03-16 00:07:10 +000067
68 thread.stack_size(0)
Florent Xicluna6709c0c2010-03-20 00:21:04 +000069 self.assertEqual(thread.stack_size(), 0, "stack_size not reset to default")
Christian Heimesdd15f6c2008-03-16 00:07:10 +000070
Zachary Ware101d9e72013-12-08 00:44:27 -060071 @unittest.skipIf(os.name not in ("nt", "posix"), 'test meant for nt and posix')
Zachary Ware9fe6d862013-12-08 00:20:35 -060072 def test_nt_and_posix_stack_size(self):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000073 try:
74 thread.stack_size(4096)
75 except ValueError:
76 verbose_print("caught expected ValueError setting "
77 "stack_size(4096)")
78 except thread.error:
Zachary Ware9fe6d862013-12-08 00:20:35 -060079 self.skipTest("platform does not support changing thread stack "
80 "size")
Christian Heimesdd15f6c2008-03-16 00:07:10 +000081
Zachary Ware9fe6d862013-12-08 00:20:35 -060082 fail_msg = "stack_size(%d) failed - should succeed"
83 for tss in (262144, 0x100000, 0):
84 thread.stack_size(tss)
85 self.assertEqual(thread.stack_size(), tss, fail_msg % tss)
86 verbose_print("successfully set stack_size(%d)" % tss)
Christian Heimesdd15f6c2008-03-16 00:07:10 +000087
Zachary Ware9fe6d862013-12-08 00:20:35 -060088 for tss in (262144, 0x100000):
89 verbose_print("trying stack_size = (%d)" % tss)
90 self.next_ident = 0
91 self.created = 0
92 for i in range(NUMTASKS):
93 self.newtask()
Christian Heimesdd15f6c2008-03-16 00:07:10 +000094
Zachary Ware9fe6d862013-12-08 00:20:35 -060095 verbose_print("waiting for all tasks to complete")
96 self.done_mutex.acquire()
97 verbose_print("all tasks done")
Christian Heimesdd15f6c2008-03-16 00:07:10 +000098
Zachary Ware9fe6d862013-12-08 00:20:35 -060099 thread.stack_size(0)
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000100
Antoine Pitrou65c9c642009-10-30 17:25:12 +0000101 def test__count(self):
102 # Test the _count() function.
103 orig = thread._count()
104 mut = thread.allocate_lock()
105 mut.acquire()
106 started = []
107 def task():
108 started.append(None)
109 mut.acquire()
110 mut.release()
111 thread.start_new_thread(task, ())
112 while not started:
113 time.sleep(0.01)
Florent Xicluna6709c0c2010-03-20 00:21:04 +0000114 self.assertEqual(thread._count(), orig + 1)
Antoine Pitrou65c9c642009-10-30 17:25:12 +0000115 # Allow the task to finish.
116 mut.release()
117 # The only reliable way to be sure that the thread ended from the
118 # interpreter's point of view is to wait for the function object to be
119 # destroyed.
120 done = []
121 wr = weakref.ref(task, lambda _: done.append(None))
122 del task
123 while not done:
124 time.sleep(0.01)
Florent Xicluna6709c0c2010-03-20 00:21:04 +0000125 self.assertEqual(thread._count(), orig)
Antoine Pitrou65c9c642009-10-30 17:25:12 +0000126
Benjamin Petersone9000962012-04-02 11:15:17 -0400127 def test_save_exception_state_on_error(self):
128 # See issue #14474
129 def task():
130 started.release()
Benjamin Petersone9000962012-04-02 11:15:17 -0400131 raise SyntaxError
132 def mywrite(self, *args):
133 try:
134 raise ValueError
135 except ValueError:
136 pass
137 real_write(self, *args)
138 c = thread._count()
139 started = thread.allocate_lock()
140 with support.captured_output("stderr") as stderr:
141 real_write = stderr.write
142 stderr.write = mywrite
143 started.acquire()
144 thread.start_new_thread(task, ())
145 started.acquire()
146 while thread._count() > c:
Benjamin Peterson7a436c52012-04-23 10:08:14 -0400147 time.sleep(0.01)
Benjamin Petersone9000962012-04-02 11:15:17 -0400148 self.assertIn("Traceback", stderr.getvalue())
149
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000150
151class Barrier:
152 def __init__(self, num_threads):
153 self.num_threads = num_threads
Fred Drake004d5e62000-10-23 17:22:08 +0000154 self.waiting = 0
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000155 self.checkin_mutex = thread.allocate_lock()
156 self.checkout_mutex = thread.allocate_lock()
157 self.checkout_mutex.acquire()
Guido van Rossumd3b68421994-05-23 12:17:36 +0000158
Fred Drake004d5e62000-10-23 17:22:08 +0000159 def enter(self):
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000160 self.checkin_mutex.acquire()
Fred Drake004d5e62000-10-23 17:22:08 +0000161 self.waiting = self.waiting + 1
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000162 if self.waiting == self.num_threads:
163 self.waiting = self.num_threads - 1
164 self.checkout_mutex.release()
Fred Drake004d5e62000-10-23 17:22:08 +0000165 return
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000166 self.checkin_mutex.release()
Guido van Rossumd3b68421994-05-23 12:17:36 +0000167
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000168 self.checkout_mutex.acquire()
Fred Drake004d5e62000-10-23 17:22:08 +0000169 self.waiting = self.waiting - 1
170 if self.waiting == 0:
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000171 self.checkin_mutex.release()
Fred Drake004d5e62000-10-23 17:22:08 +0000172 return
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000173 self.checkout_mutex.release()
Guido van Rossumd3b68421994-05-23 12:17:36 +0000174
Guido van Rossumd3b68421994-05-23 12:17:36 +0000175
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000176class BarrierTest(BasicThreadTest):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000177
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000178 def test_barrier(self):
179 self.bar = Barrier(NUMTASKS)
180 self.running = NUMTASKS
181 for i in range(NUMTASKS):
182 thread.start_new_thread(self.task2, (i,))
183 verbose_print("waiting for tasks to end")
184 self.done_mutex.acquire()
185 verbose_print("tasks done")
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000186
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000187 def task2(self, ident):
188 for i in range(NUMTRIPS):
189 if ident == 0:
190 # give it a good chance to enter the next
191 # barrier before the others are all out
192 # of the current one
Christian Heimesb186d002008-03-18 15:15:01 +0000193 delay = 0
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000194 else:
195 with self.random_mutex:
Christian Heimesb186d002008-03-18 15:15:01 +0000196 delay = random.random() / 10000.0
197 verbose_print("task %s will run for %sus" %
198 (ident, round(delay * 1e6)))
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000199 time.sleep(delay)
200 verbose_print("task %s entering %s" % (ident, i))
201 self.bar.enter()
202 verbose_print("task %s leaving barrier" % ident)
203 with self.running_mutex:
204 self.running -= 1
205 # Must release mutex before releasing done, else the main thread can
206 # exit and set mutex to None as part of global teardown; then
207 # mutex.release() raises AttributeError.
208 finished = self.running == 0
209 if finished:
210 self.done_mutex.release()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000211
Antoine Pitrou557934f2009-11-06 22:41:14 +0000212class LockTests(lock_tests.LockTests):
213 locktype = thread.allocate_lock
214
215
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000216class TestForkInThread(unittest.TestCase):
217 def setUp(self):
218 self.read_fd, self.write_fd = os.pipe()
219
220 @unittest.skipIf(sys.platform.startswith('win'),
Florent Xicluna6709c0c2010-03-20 00:21:04 +0000221 "This test is only appropriate for POSIX-like systems.")
222 @support.reap_threads
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000223 def test_forkinthread(self):
224 def thread1():
225 try:
226 pid = os.fork() # fork in a thread
227 except RuntimeError:
228 os._exit(1) # exit the child
229
230 if pid == 0: # child
231 try:
232 os.close(self.read_fd)
233 os.write(self.write_fd, b"OK")
234 finally:
235 os._exit(0)
236 else: # parent
237 os.close(self.write_fd)
238
239 thread.start_new_thread(thread1, ())
240 self.assertEqual(os.read(self.read_fd, 2), b"OK",
241 "Unable to fork() in thread")
242
243 def tearDown(self):
244 try:
245 os.close(self.read_fd)
246 except OSError:
247 pass
248
249 try:
250 os.close(self.write_fd)
251 except OSError:
252 pass
253
254
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000255def test_main():
Gregory P. Smith24cec9f2010-03-01 06:18:41 +0000256 support.run_unittest(ThreadRunningTests, BarrierTest, LockTests,
257 TestForkInThread)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000258
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000259if __name__ == "__main__":
260 test_main()