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