annotate Lib/test/test_thread.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 2546313a8b27
children 4169d3cecb9d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 import thread
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import time
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
8
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
9 NUMTASKS = 10
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
10 NUMTRIPS = 3
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
11
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
12
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
13 _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
14
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
15 def verbose_print(arg):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
16 """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
17 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
18 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
19 print arg
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
21
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
22 class BasicThreadTest(unittest.TestCase):
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 def setUp(self):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
25 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
26 self.done_mutex.acquire()
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
27 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
28 self.random_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.running = 0
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
30 self.next_ident = 0
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
31
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
32
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
33 class ThreadRunningTests(BasicThreadTest):
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 def newtask(self):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
36 with self.running_mutex:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
37 self.next_ident += 1
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
38 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
39 thread.start_new_thread(self.task, (self.next_ident,))
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
40 self.running += 1
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
41
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
42 def task(self, ident):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
43 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
44 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
45 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
46 time.sleep(delay)
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
47 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
48 with self.running_mutex:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
49 self.running -= 1
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
50 if self.running == 0:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
51 self.done_mutex.release()
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
53 def test_starting_threads(self):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
54 # Basic test for thread creation.
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
55 for i in range(NUMTASKS):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
56 self.newtask()
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
57 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
58 self.done_mutex.acquire()
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
59 verbose_print("all tasks done")
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
60
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
61 def test_stack_size(self):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
62 # Various stack size tests.
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
63 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
64
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
65 thread.stack_size(0)
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, "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
67
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
68 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
69 return
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
71 tss_supported = True
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
72 try:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
73 thread.stack_size(4096)
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
74 except ValueError:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
75 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
76 "stack_size(4096)")
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
77 except thread.error:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
78 tss_supported = False
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
79 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
80 "size")
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
82 if tss_supported:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
83 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
84 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
85 thread.stack_size(tss)
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
86 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
87 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
88
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
89 for tss in (262144, 0x100000):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
90 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
91 self.next_ident = 0
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
92 for i in range(NUMTASKS):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
93 self.newtask()
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
94
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
95 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
96 self.done_mutex.acquire()
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
97 verbose_print("all tasks done")
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 thread.stack_size(0)
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
100
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
101
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
102 class Barrier:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
103 def __init__(self, num_threads):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
104 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
105 self.waiting = 0
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
106 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
107 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
108 self.checkout_mutex.acquire()
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
109
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 8394
diff changeset
110 def enter(self):
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
111 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
112 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
113 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
114 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
115 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
116 return
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
117 self.checkin_mutex.release()
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
118
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
119 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
120 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
121 if self.waiting == 0:
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
122 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
123 return
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
124 self.checkout_mutex.release()
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
125
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
126
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
127 class BarrierTest(BasicThreadTest):
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
128
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
129 def test_barrier(self):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
130 self.bar = Barrier(NUMTASKS)
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
131 self.running = NUMTASKS
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
132 for i in range(NUMTASKS):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
133 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
134 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
135 self.done_mutex.acquire()
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
136 verbose_print("tasks done")
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
137
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
138 def task2(self, ident):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
139 for i in range(NUMTRIPS):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
140 if ident == 0:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
141 # 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
142 # 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
143 # 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
144 delay = 0
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
145 else:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
146 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
147 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
148 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
149 (ident, round(delay * 1e6)))
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
150 time.sleep(delay)
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
151 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
152 self.bar.enter()
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
153 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
154 with self.running_mutex:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
155 self.running -= 1
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
156 # 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
157 # 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
158 # mutex.release() raises AttributeError.
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
159 finished = self.running == 0
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
160 if finished:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
161 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
162
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
163
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
164 def test_main():
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
165 test_support.run_unittest(ThreadRunningTests, BarrierTest)
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
166
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
167 if __name__ == "__main__":
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
168 test_main()