Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_thread.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 0eb56a8e6262 |
| children | d5f50ae4f1f7 |
| 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 | |
| 7 | |
|
57159
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
8 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
|
9 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
10 NUMTASKS = 10 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
11 NUMTRIPS = 3 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
12 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
13 |
|
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
|
14 _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
|
15 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
16 def verbose_print(arg): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
17 """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
|
18 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
|
19 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
|
20 print arg |
| 1588 | 21 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
22 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
23 class BasicThreadTest(unittest.TestCase): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
24 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
25 def setUp(self): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
26 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
|
27 self.done_mutex.acquire() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
28 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
|
29 self.random_mutex = thread.allocate_lock() |
|
56780
4169d3cecb9d
Merged revisions 75633 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
45836
diff
changeset
|
30 self.created = 0 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
31 self.running = 0 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
32 self.next_ident = 0 |
|
1647
03e097ddeca2
added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents:
1588
diff
changeset
|
33 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
34 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
35 class ThreadRunningTests(BasicThreadTest): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
36 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
37 def newtask(self): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
38 with self.running_mutex: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
39 self.next_ident += 1 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
40 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
|
41 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
|
42 self.created += 1 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
43 self.running += 1 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
44 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
45 def task(self, ident): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
46 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
|
47 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
|
48 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
|
49 time.sleep(delay) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
50 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
|
51 with self.running_mutex: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
52 self.running -= 1 |
|
56780
4169d3cecb9d
Merged revisions 75633 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
45836
diff
changeset
|
53 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
|
54 self.done_mutex.release() |
| 1588 | 55 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
56 def test_starting_threads(self): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
57 # Basic test for thread creation. |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
58 for i in range(NUMTASKS): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
59 self.newtask() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
60 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
|
61 self.done_mutex.acquire() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
62 verbose_print("all tasks done") |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
63 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
64 def test_stack_size(self): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
65 # Various stack size tests. |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
66 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
|
67 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
68 thread.stack_size(0) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
69 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
|
70 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
71 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
|
72 return |
| 1588 | 73 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
74 tss_supported = True |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
75 try: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
76 thread.stack_size(4096) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
77 except ValueError: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
78 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
|
79 "stack_size(4096)") |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
80 except thread.error: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
81 tss_supported = False |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
82 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
|
83 "size") |
| 1588 | 84 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
85 if tss_supported: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
86 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
|
87 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
|
88 thread.stack_size(tss) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
89 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
|
90 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
|
91 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
92 for tss in (262144, 0x100000): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
93 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
|
94 self.next_ident = 0 |
|
56780
4169d3cecb9d
Merged revisions 75633 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
45836
diff
changeset
|
95 self.created = 0 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
96 for i in range(NUMTASKS): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
97 self.newtask() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
98 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
99 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
|
100 self.done_mutex.acquire() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
101 verbose_print("all tasks done") |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
102 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
103 thread.stack_size(0) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
104 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
105 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
106 class Barrier: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
107 def __init__(self, num_threads): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
108 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
|
109 self.waiting = 0 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
110 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
|
111 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
|
112 self.checkout_mutex.acquire() |
|
1647
03e097ddeca2
added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents:
1588
diff
changeset
|
113 |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
8394
diff
changeset
|
114 def enter(self): |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 return |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
121 self.checkin_mutex.release() |
|
1647
03e097ddeca2
added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents:
1588
diff
changeset
|
122 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
123 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
|
124 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
|
125 if self.waiting == 0: |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
126 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
|
127 return |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
128 self.checkout_mutex.release() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
129 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
130 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
131 class BarrierTest(BasicThreadTest): |
|
1647
03e097ddeca2
added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents:
1588
diff
changeset
|
132 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
133 def test_barrier(self): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
134 self.bar = Barrier(NUMTASKS) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
135 self.running = NUMTASKS |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
136 for i in range(NUMTASKS): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
137 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
|
138 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
|
139 self.done_mutex.acquire() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
140 verbose_print("tasks done") |
|
1647
03e097ddeca2
added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents:
1588
diff
changeset
|
141 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
142 def task2(self, ident): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
143 for i in range(NUMTRIPS): |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
144 if ident == 0: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
145 # 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
|
146 # 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
|
147 # 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
|
148 delay = 0 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
149 else: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
150 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
|
151 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
|
152 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
|
153 (ident, round(delay * 1e6))) |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
154 time.sleep(delay) |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
155 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
|
156 self.bar.enter() |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
157 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
|
158 with self.running_mutex: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
159 self.running -= 1 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
160 # 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
|
161 # 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
|
162 # mutex.release() raises AttributeError. |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
163 finished = self.running == 0 |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
164 if finished: |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
165 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
|
166 |
|
f7d23bca599f
Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
38592
diff
changeset
|
167 |
|
57159
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
168 class LockTests(lock_tests.LockTests): |
|
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
169 locktype = thread.allocate_lock |
|
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
170 |
|
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
171 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
172 def test_main(): |
|
57159
0eb56a8e6262
Merged revisions 76137,76172 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
56780
diff
changeset
|
173 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
|
174 |
|
45749
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
175 if __name__ == "__main__": |
|
6a33d4f6f16f
Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents:
38802
diff
changeset
|
176 test_main() |
