annotate Lib/test/test_thread.py @ 65035:87f4b32905df 2.7

Cosmetic fix to use print function.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Fri, 01 Oct 2010 10:48:47 +0000
parents 3a0484200034
children b36cb4602e21
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
61046
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 59749
diff changeset
5 thread = test_support.import_module('thread')
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 import time
59210
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
7 import sys
56980
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
8 import weakref
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
57129
cd360ac58cd2 Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents: 56980
diff changeset
10 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
11
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
12 NUMTASKS = 10
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
13 NUMTRIPS = 3
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
14
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
15
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
16 _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
17
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
18 def verbose_print(arg):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
19 """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
20 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
21 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
22 print arg
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
45749
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 class BasicThreadTest(unittest.TestCase):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
26
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
27 def setUp(self):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
28 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
29 self.done_mutex.acquire()
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
30 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
31 self.random_mutex = thread.allocate_lock()
56780
4169d3cecb9d Merged revisions 75633 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 45836
diff changeset
32 self.created = 0
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
33 self.running = 0
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
34 self.next_ident = 0
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
35
45749
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 class ThreadRunningTests(BasicThreadTest):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
38
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
39 def newtask(self):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
40 with self.running_mutex:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
41 self.next_ident += 1
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
42 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
43 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
44 self.created += 1
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
45 self.running += 1
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
46
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
47 def task(self, ident):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
48 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
49 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
50 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
51 time.sleep(delay)
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
52 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
53 with self.running_mutex:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
54 self.running -= 1
56780
4169d3cecb9d Merged revisions 75633 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 45836
diff changeset
55 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
56 self.done_mutex.release()
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
58 def test_starting_threads(self):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
59 # Basic test for thread creation.
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
60 for i in range(NUMTASKS):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
61 self.newtask()
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
62 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
63 self.done_mutex.acquire()
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
64 verbose_print("all tasks done")
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
65
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
66 def test_stack_size(self):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
67 # Various stack size tests.
59749
13ff85b10db0 #8178 Cleanup the threads after test_thread.TestForkInThread.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59233
diff changeset
68 self.assertEqual(thread.stack_size(), 0, "initial stack size is not 0")
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
69
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
70 thread.stack_size(0)
59749
13ff85b10db0 #8178 Cleanup the threads after test_thread.TestForkInThread.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59233
diff changeset
71 self.assertEqual(thread.stack_size(), 0, "stack_size not reset to default")
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
72
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
73 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
74 return
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
76 tss_supported = True
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
77 try:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
78 thread.stack_size(4096)
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
79 except ValueError:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
80 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
81 "stack_size(4096)")
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
82 except thread.error:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
83 tss_supported = False
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
84 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
85 "size")
1588
bcf0b8dfdae9 Module to test threads
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
87 if tss_supported:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
88 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
89 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
90 thread.stack_size(tss)
59749
13ff85b10db0 #8178 Cleanup the threads after test_thread.TestForkInThread.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59233
diff changeset
91 self.assertEqual(thread.stack_size(), tss, fail_msg % tss)
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
92 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
93
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
94 for tss in (262144, 0x100000):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
95 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
96 self.next_ident = 0
56780
4169d3cecb9d Merged revisions 75633 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 45836
diff changeset
97 self.created = 0
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
98 for i in range(NUMTASKS):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
99 self.newtask()
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 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
102 self.done_mutex.acquire()
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
103 verbose_print("all tasks done")
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 thread.stack_size(0)
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
106
56980
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
107 def test__count(self):
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
108 # Test the _count() function.
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
109 orig = thread._count()
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
110 mut = thread.allocate_lock()
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
111 mut.acquire()
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
112 started = []
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
113 def task():
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
114 started.append(None)
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
115 mut.acquire()
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
116 mut.release()
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
117 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
118 while not started:
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
119 time.sleep(0.01)
59749
13ff85b10db0 #8178 Cleanup the threads after test_thread.TestForkInThread.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59233
diff changeset
120 self.assertEqual(thread._count(), orig + 1)
56980
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
121 # 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
122 mut.release()
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
123 # 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
124 # 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
125 # destroyed.
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
126 done = []
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
127 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
128 del task
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
129 while not done:
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
130 time.sleep(0.01)
59749
13ff85b10db0 #8178 Cleanup the threads after test_thread.TestForkInThread.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59233
diff changeset
131 self.assertEqual(thread._count(), orig)
56980
29fe5e7bd6ee Issue #7222: Make thread "reaping" more reliable so that reference
Antoine Pitrou <solipsis@pitrou.net>
parents: 56780
diff changeset
132
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
133
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
134 class Barrier:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
135 def __init__(self, num_threads):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
136 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
137 self.waiting = 0
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
138 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
139 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
140 self.checkout_mutex.acquire()
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
141
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 8394
diff changeset
142 def enter(self):
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
143 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
144 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
145 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
146 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
147 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
148 return
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
149 self.checkin_mutex.release()
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
150
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
151 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
152 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
153 if self.waiting == 0:
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
154 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
155 return
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
156 self.checkout_mutex.release()
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
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
159 class BarrierTest(BasicThreadTest):
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
160
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
161 def test_barrier(self):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
162 self.bar = Barrier(NUMTASKS)
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
163 self.running = NUMTASKS
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
164 for i in range(NUMTASKS):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
165 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
166 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
167 self.done_mutex.acquire()
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
168 verbose_print("tasks done")
1647
03e097ddeca2 added barrier test (by Tim Peters)
Guido van Rossum <guido@python.org>
parents: 1588
diff changeset
169
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
170 def task2(self, ident):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
171 for i in range(NUMTRIPS):
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
172 if ident == 0:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
173 # 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
174 # 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
175 # 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
176 delay = 0
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
177 else:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
178 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
179 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
180 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
181 (ident, round(delay * 1e6)))
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
182 time.sleep(delay)
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
183 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
184 self.bar.enter()
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
185 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
186 with self.running_mutex:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
187 self.running -= 1
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
188 # 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
189 # 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
190 # mutex.release() raises AttributeError.
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
191 finished = self.running == 0
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
192 if finished:
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
193 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
194
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
195
57129
cd360ac58cd2 Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents: 56980
diff changeset
196 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
197 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
198
cd360ac58cd2 Issue #7270: Add some dedicated unit tests for multi-thread synchronization
Antoine Pitrou <solipsis@pitrou.net>
parents: 56980
diff changeset
199
59210
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
200 class TestForkInThread(unittest.TestCase):
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
201 def setUp(self):
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
202 self.read_fd, self.write_fd = os.pipe()
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
203
59233
46ae164b15ba Fix test to be skipped on windows.
Gregory P. Smith <greg@mad-scientist.com>
parents: 59210
diff changeset
204 @unittest.skipIf(sys.platform.startswith('win'),
59749
13ff85b10db0 #8178 Cleanup the threads after test_thread.TestForkInThread.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59233
diff changeset
205 "This test is only appropriate for POSIX-like systems.")
13ff85b10db0 #8178 Cleanup the threads after test_thread.TestForkInThread.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59233
diff changeset
206 @test_support.reap_threads
59210
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
207 def test_forkinthread(self):
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
208 def thread1():
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
209 try:
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
210 pid = os.fork() # fork in a thread
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
211 except RuntimeError:
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
212 sys.exit(0) # exit the child
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
213
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
214 if pid == 0: # child
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
215 os.close(self.read_fd)
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
216 os.write(self.write_fd, "OK")
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
217 sys.exit(0)
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
218 else: # parent
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
219 os.close(self.write_fd)
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
220
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
221 thread.start_new_thread(thread1, ())
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
222 self.assertEqual(os.read(self.read_fd, 2), "OK",
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
223 "Unable to fork() in thread")
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
224
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
225 def tearDown(self):
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
226 try:
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
227 os.close(self.read_fd)
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
228 except OSError:
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
229 pass
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
230
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
231 try:
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
232 os.close(self.write_fd)
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
233 except OSError:
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
234 pass
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
235
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
236
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
237 def test_main():
59210
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
238 test_support.run_unittest(ThreadRunningTests, BarrierTest, LockTests,
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 57129
diff changeset
239 TestForkInThread)
38794
f7d23bca599f Patch #1454481: Make thread stack size runtime tunable.
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 38592
diff changeset
240
45749
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
241 if __name__ == "__main__":
6a33d4f6f16f Move test_thread over to unittest. Commits GHOP 237.
Brett Cannon <bcannon@gmail.com>
parents: 38802
diff changeset
242 test_main()