Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_threadsignals.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | b36cb4602e21 |
| children |
| 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 signal |
|
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
5 import os |
| 32937 | 6 import sys |
|
63249
c3f5833db843
Issue #7825: fix transient refleak in test_threadsignals.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
61046
diff
changeset
|
7 from test.test_support import run_unittest, import_module, reap_threads |
|
61046
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
58669
diff
changeset
|
8 thread = import_module('thread') |
|
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 |
|
63556
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
39460
diff
changeset
|
17 def registerSignals(for_usr1, for_usr2, for_alrm): |
|
32930
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 """ |
|
63249
c3f5833db843
Issue #7825: fix transient refleak in test_threadsignals.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
61046
diff
changeset
|
42 @reap_threads |
|
32930
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
43 def test_signals(self): |
|
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
44 signalled_all.acquire() |
|
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
45 self.spawnSignallingThread() |
|
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
46 signalled_all.acquire() |
|
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
47 # 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
|
48 # 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
|
49 # (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
|
50 # 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
|
51 # 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
|
52 # wait for it return. |
|
39460
1510866c920e
test_signal: Signal handling on the Tru64 buildbot
Tim Peters <tim.peters@gmail.com>
parents:
35806
diff
changeset
|
53 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
|
54 or signal_blackboard[signal.SIGUSR2]['tripped'] == 0: |
|
32949
9ebd405683cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32947
diff
changeset
|
55 signal.alarm(1) |
|
9ebd405683cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32947
diff
changeset
|
56 signal.pause() |
|
9ebd405683cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32947
diff
changeset
|
57 signal.alarm(0) |
|
32930
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
58 |
|
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
59 self.assertEqual( signal_blackboard[signal.SIGUSR1]['tripped'], 1) |
|
32949
9ebd405683cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32947
diff
changeset
|
60 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
|
61 thread.get_ident()) |
|
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
62 self.assertEqual( signal_blackboard[signal.SIGUSR2]['tripped'], 1) |
|
32949
9ebd405683cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32947
diff
changeset
|
63 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
|
64 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
|
65 signalled_all.release() |
|
32930
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
66 |
|
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
67 def spawnSignallingThread(self): |
|
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
68 thread.start_new_thread(send_signals, ()) |
|
32949
9ebd405683cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32947
diff
changeset
|
69 |
|
32930
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
70 |
|
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
71 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
|
72 global signal_blackboard |
|
32983
726db5854746
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
32958
diff
changeset
|
73 |
|
32958
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_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
|
75 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
|
76 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
|
77 |
|
63556
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
39460
diff
changeset
|
78 oldsigs = registerSignals(handle_signals, handle_signals, handle_signals) |
|
32930
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
79 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
|
80 run_unittest(ThreadSignals) |
|
32930
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
81 finally: |
|
63556
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
39460
diff
changeset
|
82 registerSignals(*oldsigs) |
|
32930
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
83 |
|
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
84 if __name__ == '__main__': |
|
5c2149f36c3e
Argh! This was meant to be part of patch #960406.
Michael W. Hudson <mwh@python.net>
parents:
diff
changeset
|
85 test_main() |
