annotate Lib/test/test_threadsignals.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 228cfb6e2900
children 7caf54ce529e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32930
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
1 """PyUnit testing that threads honor our signal semantics"""
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
2
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
3 import unittest
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
4 import thread
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
5 import signal
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
6 import os
32937
651188a6638d add missing import!
Fred Drake <fdrake@acm.org>
parents: 32933
diff changeset
7 import sys
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
8 from test.test_support import run_unittest
32930
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
9
32933
45a170c4ca56 Add the same guard as test_signal.
Michael W. Hudson <mwh@python.net>
parents: 32930
diff changeset
10 if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos':
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
11 raise unittest.SkipTest, "Can't test signal on %s" % sys.platform
32933
45a170c4ca56 Add the same guard as test_signal.
Michael W. Hudson <mwh@python.net>
parents: 32930
diff changeset
12
32930
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
13 process_pid = os.getpid()
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
14 signalled_all=thread.allocate_lock()
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
15
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
16
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
17 def registerSignals((for_usr1, for_usr2, for_alrm)):
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
18 usr1 = signal.signal(signal.SIGUSR1, for_usr1)
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
19 usr2 = signal.signal(signal.SIGUSR2, for_usr2)
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
20 alrm = signal.signal(signal.SIGALRM, for_alrm)
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
21 return usr1, usr2, alrm
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
22
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
23
35806
44fe157255bc fix typos, mostly in comments
Fred Drake <fdrake@acm.org>
parents: 32983
diff changeset
24 # The signal handler. Just note that the signal occurred and
32930
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
25 # from who.
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
26 def handle_signals(sig,frame):
32949
9ebd405683cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32947
diff changeset
27 signal_blackboard[sig]['tripped'] += 1
32930
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
28 signal_blackboard[sig]['tripped_by'] = thread.get_ident()
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
29
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
30 # a function that will be spawned as a separate thread.
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
31 def send_signals():
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
32 os.kill(process_pid, signal.SIGUSR1)
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
33 os.kill(process_pid, signal.SIGUSR2)
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
34 signalled_all.release()
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
35
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
36 class ThreadSignals(unittest.TestCase):
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
37 """Test signal handling semantics of threads.
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
38 We spawn a thread, have the thread send two signals, and
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
39 wait for it to finish. Check that we got both signals
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
40 and that they were run by the main thread.
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
41 """
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
42 def test_signals(self):
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
43 signalled_all.acquire()
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
44 self.spawnSignallingThread()
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
45 signalled_all.acquire()
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
46 # the signals that we asked the kernel to send
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
47 # will come back, but we don't know when.
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
48 # (it might even be after the thread exits
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
49 # and might be out of order.) If we haven't seen
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
50 # the signals yet, send yet another signal and
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
51 # wait for it return.
39460
1510866c920e test_signal: Signal handling on the Tru64 buildbot
Tim Peters <tim.peters@gmail.com>
parents: 35806
diff changeset
52 if signal_blackboard[signal.SIGUSR1]['tripped'] == 0 \
32930
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
53 or signal_blackboard[signal.SIGUSR2]['tripped'] == 0:
32949
9ebd405683cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32947
diff changeset
54 signal.alarm(1)
9ebd405683cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32947
diff changeset
55 signal.pause()
9ebd405683cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32947
diff changeset
56 signal.alarm(0)
32930
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
57
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
58 self.assertEqual( signal_blackboard[signal.SIGUSR1]['tripped'], 1)
32949
9ebd405683cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32947
diff changeset
59 self.assertEqual( signal_blackboard[signal.SIGUSR1]['tripped_by'],
32930
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
60 thread.get_ident())
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
61 self.assertEqual( signal_blackboard[signal.SIGUSR2]['tripped'], 1)
32949
9ebd405683cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32947
diff changeset
62 self.assertEqual( signal_blackboard[signal.SIGUSR2]['tripped_by'],
32930
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
63 thread.get_ident())
32958
e9c6414dbdb6 To ever run this test "you must import TestSkipped" from the right
Michael W. Hudson <mwh@python.net>
parents: 32949
diff changeset
64 signalled_all.release()
32930
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
65
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
66 def spawnSignallingThread(self):
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
67 thread.start_new_thread(send_signals, ())
32949
9ebd405683cd Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32947
diff changeset
68
32930
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
69
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
70 def test_main():
32958
e9c6414dbdb6 To ever run this test "you must import TestSkipped" from the right
Michael W. Hudson <mwh@python.net>
parents: 32949
diff changeset
71 global signal_blackboard
32983
726db5854746 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 32958
diff changeset
72
32958
e9c6414dbdb6 To ever run this test "you must import TestSkipped" from the right
Michael W. Hudson <mwh@python.net>
parents: 32949
diff changeset
73 signal_blackboard = { signal.SIGUSR1 : {'tripped': 0, 'tripped_by': 0 },
e9c6414dbdb6 To ever run this test "you must import TestSkipped" from the right
Michael W. Hudson <mwh@python.net>
parents: 32949
diff changeset
74 signal.SIGUSR2 : {'tripped': 0, 'tripped_by': 0 },
e9c6414dbdb6 To ever run this test "you must import TestSkipped" from the right
Michael W. Hudson <mwh@python.net>
parents: 32949
diff changeset
75 signal.SIGALRM : {'tripped': 0, 'tripped_by': 0 } }
e9c6414dbdb6 To ever run this test "you must import TestSkipped" from the right
Michael W. Hudson <mwh@python.net>
parents: 32949
diff changeset
76
32930
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
77 oldsigs = registerSignals((handle_signals, handle_signals, handle_signals))
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
78 try:
32958
e9c6414dbdb6 To ever run this test "you must import TestSkipped" from the right
Michael W. Hudson <mwh@python.net>
parents: 32949
diff changeset
79 run_unittest(ThreadSignals)
32930
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
80 finally:
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
81 registerSignals(oldsigs)
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
82
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
83 if __name__ == '__main__':
5c2149f36c3e Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
84 test_main()