annotate Lib/test/test_dummy_thread.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 71acd4482e65
children b82c3e593db6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Generic thread tests.
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 Meant to be used by dummy_thread and thread. To allow for different modules
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 to be used, test_main() can be called with the module to use as the thread
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 implementation as its sole argument.
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 """
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
8 import _dummy_thread as _thread
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 import time
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 41642
diff changeset
10 import queue
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 import random
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 import unittest
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
13 from test import support
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
47266
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
15 DELAY = 0 # Set > 0 when testing a module other than _dummy_thread, such as
7fe3a8648ce2 Rename thread to _thread and dummy_thread to _dummy_thread. Issue #2875.
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
16 # the '_thread' module.
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 class LockTests(unittest.TestCase):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 """Test lock objects."""
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 def setUp(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # Create a lock
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 self.lock = _thread.allocate_lock()
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 def test_initlock(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 #Make sure locks start locked
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
27 self.assertTrue(not self.lock.locked(),
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 "Lock object is not initialized unlocked.")
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 def test_release(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # Test self.lock.release()
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 self.lock.acquire()
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 self.lock.release()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
34 self.assertTrue(not self.lock.locked(),
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 "Lock object did not release properly.")
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26890
diff changeset
36
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 def test_improper_release(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 #Make sure release of an unlocked thread raises _thread.error
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
39 self.assertRaises(_thread.error, self.lock.release)
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 def test_cond_acquire_success(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 #Make sure the conditional acquiring of the lock works.
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
43 self.assertTrue(self.lock.acquire(0),
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 "Conditional acquiring of the lock failed.")
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 def test_cond_acquire_fail(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 #Test acquiring locked lock returns False
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 self.lock.acquire(0)
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
49 self.assertTrue(not self.lock.acquire(0),
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 "Conditional acquiring of a locked lock incorrectly "
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 "succeeded.")
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 def test_uncond_acquire_success(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 #Make sure unconditional acquiring of a lock works.
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 self.lock.acquire()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
56 self.assertTrue(self.lock.locked(),
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 "Uncondional locking failed.")
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 def test_uncond_acquire_return_val(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 #Make sure that an unconditional locking returns True.
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
61 self.assertTrue(self.lock.acquire(1) is True,
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 "Unconditional locking did not return True.")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
63 self.assertTrue(self.lock.acquire() is True)
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26890
diff changeset
64
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 def test_uncond_acquire_blocking(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 #Make sure that unconditional acquiring of a locked lock blocks.
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 def delay_unlock(to_unlock, delay):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 """Hold on to lock for a set amount of time before unlocking."""
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 time.sleep(delay)
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 to_unlock.release()
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 self.lock.acquire()
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 start_time = int(time.time())
28902
039ab31e1034 Make time.sleep calls go to 0 for common testing.
Brett Cannon <bcannon@gmail.com>
parents: 27963
diff changeset
74 _thread.start_new_thread(delay_unlock,(self.lock, DELAY))
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
75 if support.verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 29488
diff changeset
76 print()
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 29488
diff changeset
77 print("*** Waiting for thread to release the lock "\
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 29488
diff changeset
78 "(approx. %s sec.) ***" % DELAY)
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 self.lock.acquire()
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 end_time = int(time.time())
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
81 if support.verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 29488
diff changeset
82 print("done")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
83 self.assertTrue((end_time - start_time) >= DELAY,
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 "Blocking by unconditional acquiring failed.")
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 class MiscTests(unittest.TestCase):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 """Miscellaneous tests."""
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 def test_exit(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 #Make sure _thread.exit() raises SystemExit
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
91 self.assertRaises(SystemExit, _thread.exit)
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 def test_ident(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 #Test sanity of _thread.get_ident()
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
95 self.assertIsInstance(_thread.get_ident(), int,
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
96 "_thread.get_ident() returned a non-integer")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
97 self.assertTrue(_thread.get_ident() != 0,
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 "_thread.get_ident() returned 0")
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 def test_LockType(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 #Make sure _thread.LockType is the same type as _thread.allocate_locke()
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
102 self.assertIsInstance(_thread.allocate_lock(), _thread.LockType,
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
103 "_thread.LockType is not an instance of what "
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
104 "is returned by _thread.allocate_lock()")
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
29446
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
106 def test_interrupt_main(self):
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
107 #Calling start_new_thread with a function that executes interrupt_main
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
108 # should raise KeyboardInterrupt upon completion.
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
109 def call_interrupt():
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
110 _thread.interrupt_main()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
111 self.assertRaises(KeyboardInterrupt, _thread.start_new_thread,
29446
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
112 call_interrupt, tuple())
29488
5143cfe7cc75 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 29447
diff changeset
113
29447
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
114 def test_interrupt_in_main(self):
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
115 # Make sure that if interrupt_main is called in main threat that
2cb969ffe1bf Cause calling interrupt_main in main thread raise KeyboardInterrupt instantly.
Brett Cannon <bcannon@gmail.com>
parents: 29446
diff changeset
116 # KeyboardInterrupt is raised instantly.
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
117 self.assertRaises(KeyboardInterrupt, _thread.interrupt_main)
29446
e9953b399d55 dummy_thread modified to have interrupt_main and to behave appropriately when
Brett Cannon <bcannon@gmail.com>
parents: 28917
diff changeset
118
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 class ThreadTests(unittest.TestCase):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 """Test thread creation."""
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 def test_arg_passing(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 #Make sure that parameter passing works.
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 def arg_tester(queue, arg1=False, arg2=False):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 """Use to test _thread.start_new_thread() passes args properly."""
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 queue.put((arg1, arg2))
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 41642
diff changeset
128 testing_queue = queue.Queue(1)
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 _thread.start_new_thread(arg_tester, (testing_queue, True, True))
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 result = testing_queue.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
131 self.assertTrue(result[0] and result[1],
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 "Argument passing for thread creation using tuple failed")
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 _thread.start_new_thread(arg_tester, tuple(), {'queue':testing_queue,
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 'arg1':True, 'arg2':True})
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 result = testing_queue.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
136 self.assertTrue(result[0] and result[1],
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 "Argument passing for thread creation using kwargs failed")
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 _thread.start_new_thread(arg_tester, (testing_queue, True), {'arg2':True})
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 result = testing_queue.get()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
140 self.assertTrue(result[0] and result[1],
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 "Argument passing for thread creation using both tuple"
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 " and kwargs failed")
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26890
diff changeset
143
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 def test_multi_creation(self):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 #Make sure multiple threads can be created.
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 def queue_mark(queue, delay):
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 """Wait for ``delay`` seconds and then put something into ``queue``"""
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 time.sleep(delay)
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 queue.put(_thread.get_ident())
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26890
diff changeset
150
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 thread_count = 5
46899
bd2c95c165ea Rename Queue module to queue.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 41642
diff changeset
152 testing_queue = queue.Queue(thread_count)
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
153 if support.verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 29488
diff changeset
154 print()
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 29488
diff changeset
155 print("*** Testing multiple thread creation "\
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 29488
diff changeset
156 "(will take approx. %s to %s sec.) ***" % (DELAY, thread_count))
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
157 for count in range(thread_count):
28902
039ab31e1034 Make time.sleep calls go to 0 for common testing.
Brett Cannon <bcannon@gmail.com>
parents: 27963
diff changeset
158 if DELAY:
039ab31e1034 Make time.sleep calls go to 0 for common testing.
Brett Cannon <bcannon@gmail.com>
parents: 27963
diff changeset
159 local_delay = round(random.random(), 1)
039ab31e1034 Make time.sleep calls go to 0 for common testing.
Brett Cannon <bcannon@gmail.com>
parents: 27963
diff changeset
160 else:
039ab31e1034 Make time.sleep calls go to 0 for common testing.
Brett Cannon <bcannon@gmail.com>
parents: 27963
diff changeset
161 local_delay = 0
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 _thread.start_new_thread(queue_mark,
28902
039ab31e1034 Make time.sleep calls go to 0 for common testing.
Brett Cannon <bcannon@gmail.com>
parents: 27963
diff changeset
163 (testing_queue, local_delay))
039ab31e1034 Make time.sleep calls go to 0 for common testing.
Brett Cannon <bcannon@gmail.com>
parents: 27963
diff changeset
164 time.sleep(DELAY)
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
165 if support.verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 29488
diff changeset
166 print('done')
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48453
diff changeset
167 self.assertTrue(testing_queue.qsize() == thread_count,
27963
226463445697 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 26890
diff changeset
168 "Not all %s threads executed properly after %s sec." %
28902
039ab31e1034 Make time.sleep calls go to 0 for common testing.
Brett Cannon <bcannon@gmail.com>
parents: 27963
diff changeset
169 (thread_count, DELAY))
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 def test_main(imported_module=None):
28902
039ab31e1034 Make time.sleep calls go to 0 for common testing.
Brett Cannon <bcannon@gmail.com>
parents: 27963
diff changeset
172 global _thread, DELAY
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 if imported_module:
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 _thread = imported_module
28902
039ab31e1034 Make time.sleep calls go to 0 for common testing.
Brett Cannon <bcannon@gmail.com>
parents: 27963
diff changeset
175 DELAY = 2
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
176 if support.verbose:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 29488
diff changeset
177 print()
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 29488
diff changeset
178 print("*** Using %s as _thread module ***" % _thread)
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 46899
diff changeset
179 support.run_unittest(LockTests, MiscTests, ThreadTests)
26890
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 if __name__ == '__main__':
8053bc0b8bb5 Brett Cannon's dummy_thread and dummy_threading modules (SF patch
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 test_main()