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 |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [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 |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 13 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 14 | _print_mutex = thread.allocate_lock() |
| 15 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 16 | def verbose_print(arg): |
| 17 | """Helper function for printing out debugging output.""" |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 18 | if support.verbose: |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 19 | with _print_mutex: |
| 20 | print(arg) |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 21 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 22 | class BasicThreadTest(unittest.TestCase): |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 23 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 24 | def setUp(self): |
| 25 | self.done_mutex = thread.allocate_lock() |
| 26 | self.done_mutex.acquire() |
| 27 | self.running_mutex = thread.allocate_lock() |
| 28 | self.random_mutex = thread.allocate_lock() |
Antoine Pitrou | 97115d1 | 2009-10-23 18:34:17 +0000 | [diff] [blame] | 29 | self.created = 0 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 30 | self.running = 0 |
| 31 | self.next_ident = 0 |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 33 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 34 | class ThreadRunningTests(BasicThreadTest): |
| 35 | |
| 36 | def newtask(self): |
| 37 | with self.running_mutex: |
| 38 | self.next_ident += 1 |
| 39 | verbose_print("creating task %s" % self.next_ident) |
| 40 | thread.start_new_thread(self.task, (self.next_ident,)) |
Antoine Pitrou | 97115d1 | 2009-10-23 18:34:17 +0000 | [diff] [blame] | 41 | self.created += 1 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 42 | self.running += 1 |
| 43 | |
| 44 | def task(self, ident): |
| 45 | with self.random_mutex: |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 46 | delay = random.random() / 10000.0 |
| 47 | verbose_print("task %s will run for %sus" % (ident, round(delay*1e6))) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 48 | time.sleep(delay) |
| 49 | verbose_print("task %s done" % ident) |
| 50 | with self.running_mutex: |
| 51 | self.running -= 1 |
Antoine Pitrou | 97115d1 | 2009-10-23 18:34:17 +0000 | [diff] [blame] | 52 | if self.created == NUMTASKS and self.running == 0: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 53 | self.done_mutex.release() |
| 54 | |
| 55 | def test_starting_threads(self): |
| 56 | # Basic test for thread creation. |
| 57 | for i in range(NUMTASKS): |
| 58 | self.newtask() |
| 59 | verbose_print("waiting for tasks to complete...") |
| 60 | self.done_mutex.acquire() |
| 61 | verbose_print("all tasks done") |
| 62 | |
| 63 | def test_stack_size(self): |
| 64 | # Various stack size tests. |
| 65 | self.assertEquals(thread.stack_size(), 0, "intial stack size is not 0") |
| 66 | |
| 67 | thread.stack_size(0) |
| 68 | self.assertEquals(thread.stack_size(), 0, "stack_size not reset to default") |
| 69 | |
| 70 | if os.name not in ("nt", "os2", "posix"): |
| 71 | return |
| 72 | |
| 73 | tss_supported = True |
| 74 | try: |
| 75 | thread.stack_size(4096) |
| 76 | except ValueError: |
| 77 | verbose_print("caught expected ValueError setting " |
| 78 | "stack_size(4096)") |
| 79 | except thread.error: |
| 80 | tss_supported = False |
| 81 | verbose_print("platform does not support changing thread stack " |
| 82 | "size") |
| 83 | |
| 84 | if tss_supported: |
| 85 | fail_msg = "stack_size(%d) failed - should succeed" |
| 86 | for tss in (262144, 0x100000, 0): |
| 87 | thread.stack_size(tss) |
| 88 | self.assertEquals(thread.stack_size(), tss, fail_msg % tss) |
| 89 | verbose_print("successfully set stack_size(%d)" % tss) |
| 90 | |
| 91 | for tss in (262144, 0x100000): |
| 92 | verbose_print("trying stack_size = (%d)" % tss) |
| 93 | self.next_ident = 0 |
Antoine Pitrou | 97115d1 | 2009-10-23 18:34:17 +0000 | [diff] [blame] | 94 | self.created = 0 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 95 | for i in range(NUMTASKS): |
| 96 | self.newtask() |
| 97 | |
| 98 | verbose_print("waiting for all tasks to complete") |
| 99 | self.done_mutex.acquire() |
| 100 | verbose_print("all tasks done") |
| 101 | |
| 102 | thread.stack_size(0) |
| 103 | |
Antoine Pitrou | 65c9c64 | 2009-10-30 17:25:12 +0000 | [diff] [blame] | 104 | def test__count(self): |
| 105 | # Test the _count() function. |
| 106 | orig = thread._count() |
| 107 | mut = thread.allocate_lock() |
| 108 | mut.acquire() |
| 109 | started = [] |
| 110 | def task(): |
| 111 | started.append(None) |
| 112 | mut.acquire() |
| 113 | mut.release() |
| 114 | thread.start_new_thread(task, ()) |
| 115 | while not started: |
| 116 | time.sleep(0.01) |
| 117 | self.assertEquals(thread._count(), orig + 1) |
| 118 | # Allow the task to finish. |
| 119 | mut.release() |
| 120 | # The only reliable way to be sure that the thread ended from the |
| 121 | # interpreter's point of view is to wait for the function object to be |
| 122 | # destroyed. |
| 123 | done = [] |
| 124 | wr = weakref.ref(task, lambda _: done.append(None)) |
| 125 | del task |
| 126 | while not done: |
| 127 | time.sleep(0.01) |
| 128 | self.assertEquals(thread._count(), orig) |
| 129 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 130 | |
| 131 | class Barrier: |
| 132 | def __init__(self, num_threads): |
| 133 | self.num_threads = num_threads |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 134 | self.waiting = 0 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 135 | self.checkin_mutex = thread.allocate_lock() |
| 136 | self.checkout_mutex = thread.allocate_lock() |
| 137 | self.checkout_mutex.acquire() |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 138 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 139 | def enter(self): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 140 | self.checkin_mutex.acquire() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 141 | self.waiting = self.waiting + 1 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 142 | if self.waiting == self.num_threads: |
| 143 | self.waiting = self.num_threads - 1 |
| 144 | self.checkout_mutex.release() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 145 | return |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 146 | self.checkin_mutex.release() |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 147 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 148 | self.checkout_mutex.acquire() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 149 | self.waiting = self.waiting - 1 |
| 150 | if self.waiting == 0: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 151 | self.checkin_mutex.release() |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 152 | return |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 153 | self.checkout_mutex.release() |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 154 | |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 155 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 156 | class BarrierTest(BasicThreadTest): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 157 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 158 | def test_barrier(self): |
| 159 | self.bar = Barrier(NUMTASKS) |
| 160 | self.running = NUMTASKS |
| 161 | for i in range(NUMTASKS): |
| 162 | thread.start_new_thread(self.task2, (i,)) |
| 163 | verbose_print("waiting for tasks to end") |
| 164 | self.done_mutex.acquire() |
| 165 | verbose_print("tasks done") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 166 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 167 | def task2(self, ident): |
| 168 | for i in range(NUMTRIPS): |
| 169 | if ident == 0: |
| 170 | # give it a good chance to enter the next |
| 171 | # barrier before the others are all out |
| 172 | # of the current one |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 173 | delay = 0 |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 174 | else: |
| 175 | with self.random_mutex: |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 176 | delay = random.random() / 10000.0 |
| 177 | verbose_print("task %s will run for %sus" % |
| 178 | (ident, round(delay * 1e6))) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 179 | time.sleep(delay) |
| 180 | verbose_print("task %s entering %s" % (ident, i)) |
| 181 | self.bar.enter() |
| 182 | verbose_print("task %s leaving barrier" % ident) |
| 183 | with self.running_mutex: |
| 184 | self.running -= 1 |
| 185 | # Must release mutex before releasing done, else the main thread can |
| 186 | # exit and set mutex to None as part of global teardown; then |
| 187 | # mutex.release() raises AttributeError. |
| 188 | finished = self.running == 0 |
| 189 | if finished: |
| 190 | self.done_mutex.release() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 191 | |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame^] | 192 | class LockTests(lock_tests.LockTests): |
| 193 | locktype = thread.allocate_lock |
| 194 | |
| 195 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 196 | def test_main(): |
Antoine Pitrou | 557934f | 2009-11-06 22:41:14 +0000 | [diff] [blame^] | 197 | support.run_unittest(ThreadRunningTests, BarrierTest, LockTests) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 198 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 199 | if __name__ == "__main__": |
| 200 | test_main() |