Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_thread.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | cd360ac58cd2 |
| children | 4afc50d15544 |
| rev | line source |
|---|---|
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
1 import os |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
2 import unittest |
|
8394
93aa08c77e60
Use random instead of whrandom.
Guido van Rossum <guido@python.org>
parents:
5845
diff
changeset
|
3 import random |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
4 from test import test_support |
| 1588 | 5 import thread |
| 6 import time | |
|
56980
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
7 import weakref |
| 1588 | 8 |
|
57129
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
9 from test import lock_tests |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
10 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
11 NUMTASKS = 10 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
12 NUMTRIPS = 3 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
13 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
14 |
|
45836
2546313a8b27
Speed test_thread up from 51.328s to 0.081s by reducing its sleep times. We
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45749
diff
changeset
|
15 _print_mutex = thread.allocate_lock() |
|
2546313a8b27
Speed test_thread up from 51.328s to 0.081s by reducing its sleep times. We
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45749
diff
changeset
|
16 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
17 def verbose_print(arg): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
18 """Helper function for printing out debugging output.""" |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
19 if test_support.verbose: |
|
45836
2546313a8b27
Speed test_thread up from 51.328s to 0.081s by reducing its sleep times. We
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45749
diff
changeset
|
20 with _print_mutex: |
|
2546313a8b27
Speed test_thread up from 51.328s to 0.081s by reducing its sleep times. We
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45749
diff
changeset
|
21 print arg |
| 1588 | 22 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
23 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
24 class BasicThreadTest(unittest.TestCase): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
25 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
26 def setUp(self): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
27 self.done_mutex = thread.allocate_lock() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
28 self.done_mutex.acquire() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
29 self.running_mutex = thread.allocate_lock() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
30 self.random_mutex = thread.allocate_lock() |
|
56780
4169d3cecb9d
Merged revisions 75633 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
45836
diff
changeset
|
31 self.created = 0 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
32 self.running = 0 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
33 self.next_ident = 0 |
|
1647
03e097ddeca2
added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents:
1588
diff
changeset
|
34 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
35 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
36 class ThreadRunningTests(BasicThreadTest): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
37 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
38 def newtask(self): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
39 with self.running_mutex: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
40 self.next_ident += 1 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
41 verbose_print("creating task %s" % self.next_ident) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
42 thread.start_new_thread(self.task, (self.next_ident,)) |
|
56780
4169d3cecb9d
Merged revisions 75633 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
45836
diff
changeset
|
43 self.created += 1 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
44 self.running += 1 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
45 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
46 def task(self, ident): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
47 with self.random_mutex: |
|
45836
2546313a8b27
Speed test_thread up from 51.328s to 0.081s by reducing its sleep times. We
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45749
diff
changeset
|
48 delay = random.random() / 10000.0 |
|
2546313a8b27
Speed test_thread up from 51.328s to 0.081s by reducing its sleep times. We
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45749
diff
changeset
|
49 verbose_print("task %s will run for %sus" % (ident, round(delay*1e6))) |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
50 time.sleep(delay) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
51 verbose_print("task %s done" % ident) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
52 with self.running_mutex: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
53 self.running -= 1 |
|
56780
4169d3cecb9d
Merged revisions 75633 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
45836
diff
changeset
|
54 if self.created == NUMTASKS and self.running == 0: |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
55 self.done_mutex.release() |
| 1588 | 56 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
57 def test_starting_threads(self): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
58 # Basic test for thread creation. |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
59 for i in range(NUMTASKS): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
60 self.newtask() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
61 verbose_print("waiting for tasks to complete...") |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
62 self.done_mutex.acquire() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
63 verbose_print("all tasks done") |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
64 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
65 def test_stack_size(self): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
66 # Various stack size tests. |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
67 self.assertEquals(thread.stack_size(), 0, "intial stack size is not 0") |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
68 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
69 thread.stack_size(0) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
70 self.assertEquals(thread.stack_size(), 0, "stack_size not reset to default") |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
71 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
72 if os.name not in ("nt", "os2", "posix"): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
73 return |
| 1588 | 74 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
75 tss_supported = True |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
76 try: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
77 thread.stack_size(4096) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
78 except ValueError: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
79 verbose_print("caught expected ValueError setting " |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
80 "stack_size(4096)") |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
81 except thread.error: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
82 tss_supported = False |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
83 verbose_print("platform does not support changing thread stack " |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
84 "size") |
| 1588 | 85 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
86 if tss_supported: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
87 fail_msg = "stack_size(%d) failed - should succeed" |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
88 for tss in (262144, 0x100000, 0): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
89 thread.stack_size(tss) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
90 self.assertEquals(thread.stack_size(), tss, fail_msg % tss) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
91 verbose_print("successfully set stack_size(%d)" % tss) |
|
1647
03e097ddeca2
added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents:
1588
diff
changeset
|
92 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
93 for tss in (262144, 0x100000): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
94 verbose_print("trying stack_size = (%d)" % tss) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
95 self.next_ident = 0 |
|
56780
4169d3cecb9d
Merged revisions 75633 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
45836
diff
changeset
|
96 self.created = 0 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
97 for i in range(NUMTASKS): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
98 self.newtask() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
99 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
100 verbose_print("waiting for all tasks to complete") |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
101 self.done_mutex.acquire() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
102 verbose_print("all tasks done") |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
103 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
104 thread.stack_size(0) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
105 |
|
56980
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
106 def test__count(self): |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
107 # Test the _count() function. |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
108 orig = thread._count() |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
109 mut = thread.allocate_lock() |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
110 mut.acquire() |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
111 started = [] |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
112 def task(): |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
113 started.append(None) |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
114 mut.acquire() |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
115 mut.release() |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
116 thread.start_new_thread(task, ()) |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
117 while not started: |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
118 time.sleep(0.01) |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
119 self.assertEquals(thread._count(), orig + 1) |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
120 # Allow the task to finish. |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
121 mut.release() |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
122 # The only reliable way to be sure that the thread ended from the |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
123 # interpreter's point of view is to wait for the function object to be |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
124 # destroyed. |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
125 done = [] |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
126 wr = weakref.ref(task, lambda _: done.append(None)) |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
127 del task |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
128 while not done: |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
129 time.sleep(0.01) |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
130 self.assertEquals(thread._count(), orig) |
|
29fe5e7bd6ee
Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
131 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
132 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
133 class Barrier: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
134 def __init__(self, num_threads): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
135 self.num_threads = num_threads |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8394
diff
changeset
|
136 self.waiting = 0 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
137 self.checkin_mutex = thread.allocate_lock() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
138 self.checkout_mutex = thread.allocate_lock() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
139 self.checkout_mutex.acquire() |
|
1647
03e097ddeca2
added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents:
1588
diff
changeset
|
140 |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8394
diff
changeset
|
141 def enter(self): |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
142 self.checkin_mutex.acquire() |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8394
diff
changeset
|
143 self.waiting = self.waiting + 1 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
144 if self.waiting == self.num_threads: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
145 self.waiting = self.num_threads - 1 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
146 self.checkout_mutex.release() |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8394
diff
changeset
|
147 return |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
148 self.checkin_mutex.release() |
|
1647
03e097ddeca2
added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents:
1588
diff
changeset
|
149 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
150 self.checkout_mutex.acquire() |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8394
diff
changeset
|
151 self.waiting = self.waiting - 1 |
|
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8394
diff
changeset
|
152 if self.waiting == 0: |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
153 self.checkin_mutex.release() |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8394
diff
changeset
|
154 return |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
155 self.checkout_mutex.release() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
156 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
157 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
158 class BarrierTest(BasicThreadTest): |
|
1647
03e097ddeca2
added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents:
1588
diff
changeset
|
159 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
160 def test_barrier(self): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
161 self.bar = Barrier(NUMTASKS) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
162 self.running = NUMTASKS |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
163 for i in range(NUMTASKS): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
164 thread.start_new_thread(self.task2, (i,)) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
165 verbose_print("waiting for tasks to end") |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
166 self.done_mutex.acquire() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
167 verbose_print("tasks done") |
|
1647
03e097ddeca2
added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents:
1588
diff
changeset
|
168 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
169 def task2(self, ident): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
170 for i in range(NUMTRIPS): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
171 if ident == 0: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
172 # give it a good chance to enter the next |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
173 # barrier before the others are all out |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
174 # of the current one |
|
45836
2546313a8b27
Speed test_thread up from 51.328s to 0.081s by reducing its sleep times. We
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45749
diff
changeset
|
175 delay = 0 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
176 else: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
177 with self.random_mutex: |
|
45836
2546313a8b27
Speed test_thread up from 51.328s to 0.081s by reducing its sleep times. We
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45749
diff
changeset
|
178 delay = random.random() / 10000.0 |
|
2546313a8b27
Speed test_thread up from 51.328s to 0.081s by reducing its sleep times. We
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45749
diff
changeset
|
179 verbose_print("task %s will run for %sus" % |
|
2546313a8b27
Speed test_thread up from 51.328s to 0.081s by reducing its sleep times. We
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45749
diff
changeset
|
180 (ident, round(delay * 1e6))) |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
181 time.sleep(delay) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
182 verbose_print("task %s entering %s" % (ident, i)) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
183 self.bar.enter() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
184 verbose_print("task %s leaving barrier" % ident) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
185 with self.running_mutex: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
186 self.running -= 1 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
187 # Must release mutex before releasing done, else the main thread can |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
188 # exit and set mutex to None as part of global teardown; then |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
189 # mutex.release() raises AttributeError. |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
190 finished = self.running == 0 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
191 if finished: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
192 self.done_mutex.release() |
|
38794
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
193 |
|
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
194 |
|
57129
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
195 class LockTests(lock_tests.LockTests): |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
196 locktype = thread.allocate_lock |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
197 |
|
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
198 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
199 def test_main(): |
|
57129
cd360ac58cd2
Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents:
56980
diff
changeset
|
200 test_support.run_unittest(ThreadRunningTests, BarrierTest, LockTests) |
|
38794
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
201 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
202 if __name__ == "__main__": |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
203 test_main() |
