blob: 892f519d1d787a92443b965339fe76feab022dd5 [file] [log] [blame]
Georg Brandl9f2b93e2007-08-24 18:07:52 +00001import unittest
2from test import test_support
Guido van Rossum4f17e3e1995-03-16 15:07:38 +00003import signal
Michael W. Hudson34f20ea2002-05-27 15:08:24 +00004import os, sys, time
Guido van Rossumcc5a91d1997-04-16 00:29:15 +00005
Armin Rigo8b2cbfd2004-08-07 21:27:43 +00006class HandlerBCalled(Exception):
7 pass
Guido van Rossum4f17e3e1995-03-16 15:07:38 +00008
Georg Brandl9f2b93e2007-08-24 18:07:52 +00009class InterProcessSignalTests(unittest.TestCase):
10 MAX_DURATION = 20 # Entire test should last at most 20 sec.
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000011
Georg Brandl9f2b93e2007-08-24 18:07:52 +000012 # Set up a child to send signals to us (the parent) after waiting
13 # long enough to receive the alarm. It seems we miss the alarm
14 # for some reason. This will hopefully stop the hangs on
15 # Tru64/Alpha. Alas, it doesn't. Tru64 appears to miss all the
16 # signals at times, or seemingly random subsets of them, and
17 # nothing done in force_test_exit so far has actually helped.
18 def spawn_force_test_exit_process(self, parent_pid):
19 # Sigh, both imports seem necessary to avoid errors.
20 import os
21 fork_pid = os.fork()
22 if fork_pid:
23 # In parent.
24 return fork_pid
Tim Peters1742f332006-08-12 04:42:47 +000025
Georg Brandl9f2b93e2007-08-24 18:07:52 +000026 # In child.
27 import os, time
28 try:
29 # Wait 5 seconds longer than the expected alarm to give enough
30 # time for the normal sequence of events to occur. This is
31 # just a stop-gap to try to prevent the test from hanging.
32 time.sleep(self.MAX_DURATION + 5)
33 print >> sys.__stdout__, " child should not have to kill parent"
34 for signame in "SIGHUP", "SIGUSR1", "SIGUSR2", "SIGALRM":
35 os.kill(parent_pid, getattr(signal, signame))
36 print >> sys.__stdout__, " child sent", signame, "to", \
37 parent_pid
38 time.sleep(1)
39 finally:
40 os._exit(0)
Tim Peters1742f332006-08-12 04:42:47 +000041
Georg Brandl9f2b93e2007-08-24 18:07:52 +000042 def handlerA(self, *args):
43 self.a_called = True
44 if test_support.verbose:
45 print "handlerA invoked", args
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000046
Georg Brandl9f2b93e2007-08-24 18:07:52 +000047 def handlerB(self, *args):
48 self.b_called = True
49 if test_support.verbose:
50 print "handlerB invoked", args
51 raise HandlerBCalled(*args)
Neal Norwitz9730bcb2006-01-23 07:50:06 +000052
Georg Brandl9f2b93e2007-08-24 18:07:52 +000053 def test_main(self):
54 self.assertEquals(signal.getsignal(signal.SIGHUP), self.handlerA)
55 self.assertEquals(signal.getsignal(signal.SIGUSR1), self.handlerB)
56 self.assertEquals(signal.getsignal(signal.SIGUSR2), signal.SIG_IGN)
57 self.assertEquals(signal.getsignal(signal.SIGALRM),
58 signal.default_int_handler)
Fred Drake004d5e62000-10-23 17:22:08 +000059
Georg Brandl9f2b93e2007-08-24 18:07:52 +000060 # Launch an external script to send us signals.
61 # We expect the external script to:
62 # send HUP, which invokes handlerA to set a_called
63 # send USR1, which invokes handlerB to set b_called and raise
64 # HandlerBCalled
65 # send USR2, which is ignored
66 #
67 # Then we expect the alarm to go off, and its handler raises
68 # KeyboardInterrupt, finally getting us out of the loop.
Michael W. Hudson5c26e862004-06-11 18:09:28 +000069
Georg Brandl9f2b93e2007-08-24 18:07:52 +000070 if test_support.verbose:
71 verboseflag = '-x'
72 else:
73 verboseflag = '+x'
Tim Peters1742f332006-08-12 04:42:47 +000074
Georg Brandl9f2b93e2007-08-24 18:07:52 +000075 pid = self.pid
76 if test_support.verbose:
77 print "test runner's pid is", pid
Tim Peters1742f332006-08-12 04:42:47 +000078
Georg Brandl9f2b93e2007-08-24 18:07:52 +000079 # Shell script that will send us asynchronous signals
80 script = """
81 (
82 set %(verboseflag)s
83 sleep 2
84 kill -HUP %(pid)d
85 sleep 2
86 kill -USR1 %(pid)d
87 sleep 2
88 kill -USR2 %(pid)d
89 ) &
90 """ % vars()
Tim Peters1742f332006-08-12 04:42:47 +000091
Georg Brandl9f2b93e2007-08-24 18:07:52 +000092 signal.alarm(self.MAX_DURATION)
Michael W. Hudson5c26e862004-06-11 18:09:28 +000093
Georg Brandl9f2b93e2007-08-24 18:07:52 +000094 handler_b_exception_raised = False
Neal Norwitz313f8a92006-07-30 19:20:42 +000095
Georg Brandl9f2b93e2007-08-24 18:07:52 +000096 os.system(script)
97 try:
98 if test_support.verbose:
99 print "starting pause() loop..."
100 while 1:
101 try:
102 if test_support.verbose:
103 print "call pause()..."
104 signal.pause()
105 if test_support.verbose:
106 print "pause() returned"
107 except HandlerBCalled:
108 handler_b_exception_raised = True
109 if test_support.verbose:
110 print "HandlerBCalled exception caught"
Neal Norwitzec3c5e32006-07-30 19:18:38 +0000111
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000112 except KeyboardInterrupt:
113 if test_support.verbose:
114 print "KeyboardInterrupt (the alarm() went off)"
Neal Norwitzec3c5e32006-07-30 19:18:38 +0000115
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000116 self.assert_(self.a_called)
117 self.assert_(self.b_called)
118 self.assert_(handler_b_exception_raised)
Tim Peters1742f332006-08-12 04:42:47 +0000119
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000120 def setUp(self):
121 # Install handlers.
122 self.hup = signal.signal(signal.SIGHUP, self.handlerA)
123 self.usr1 = signal.signal(signal.SIGUSR1, self.handlerB)
124 self.usr2 = signal.signal(signal.SIGUSR2, signal.SIG_IGN)
125 self.alrm = signal.signal(signal.SIGALRM,
126 signal.default_int_handler)
127 self.a_called = False
128 self.b_called = False
129 self.pid = os.getpid()
130 self.fork_pid = self.spawn_force_test_exit_process(self.pid)
131
132 def tearDown(self):
133 # Forcibly kill the child we created to ping us if there was a
134 # test error.
135 try:
136 # Make sure we don't kill ourself if there was a fork
137 # error.
138 if self.fork_pid > 0:
139 os.kill(self.fork_pid, signal.SIGKILL)
140 except:
141 # If the child killed us, it has probably exited. Killing
142 # a non-existent process will raise an error which we
143 # don't care about.
144 pass
145
146 # Restore handlers.
147 signal.alarm(0) # cancel alarm in case we died early
148 signal.signal(signal.SIGHUP, self.hup)
149 signal.signal(signal.SIGUSR1, self.usr1)
150 signal.signal(signal.SIGUSR2, self.usr2)
151 signal.signal(signal.SIGALRM, self.alrm)
152
153
154class BasicSignalTests(unittest.TestCase):
155 def test_out_of_range_signal_number_raises_error(self):
156 self.assertRaises(ValueError, signal.getsignal, 4242)
157
158 def trivial_signal_handler(*args):
159 pass
160
161 self.assertRaises(ValueError, signal.signal, 4242,
162 trivial_signal_handler)
163
164 def test_setting_signal_handler_to_none_raises_error(self):
165 self.assertRaises(TypeError, signal.signal,
166 signal.SIGUSR1, None)
167
168def test_main():
169 if sys.platform[:3] in ('win', 'os2') or sys.platform == 'riscos':
170 raise test_support.TestSkipped("Can't test signal on %s" % \
171 sys.platform)
172
173 test_support.run_unittest(BasicSignalTests, InterProcessSignalTests)
174
175
176if __name__ == "__main__":
177 test_main()