blob: b88565cf6313d92ddb25c14cd3132cb2e91d99bb [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
Christian Heimesacfd8ed2008-02-28 21:00:45 +00004import sys, os, time, errno
5
6if sys.platform[:3] in ('win', 'os2') or sys.platform == 'riscos':
7 raise test_support.TestSkipped("Can't test signal on %s" % \
8 sys.platform)
9
Guido van Rossumcc5a91d1997-04-16 00:29:15 +000010
Armin Rigo8b2cbfd2004-08-07 21:27:43 +000011class HandlerBCalled(Exception):
12 pass
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000013
Georg Brandl9f2b93e2007-08-24 18:07:52 +000014class InterProcessSignalTests(unittest.TestCase):
15 MAX_DURATION = 20 # Entire test should last at most 20 sec.
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000016
Georg Brandl9f2b93e2007-08-24 18:07:52 +000017 # Set up a child to send signals to us (the parent) after waiting
18 # long enough to receive the alarm. It seems we miss the alarm
19 # for some reason. This will hopefully stop the hangs on
20 # Tru64/Alpha. Alas, it doesn't. Tru64 appears to miss all the
21 # signals at times, or seemingly random subsets of them, and
22 # nothing done in force_test_exit so far has actually helped.
23 def spawn_force_test_exit_process(self, parent_pid):
24 # Sigh, both imports seem necessary to avoid errors.
25 import os
26 fork_pid = os.fork()
27 if fork_pid:
28 # In parent.
29 return fork_pid
Tim Peters1742f332006-08-12 04:42:47 +000030
Georg Brandl9f2b93e2007-08-24 18:07:52 +000031 # In child.
32 import os, time
33 try:
34 # Wait 5 seconds longer than the expected alarm to give enough
35 # time for the normal sequence of events to occur. This is
36 # just a stop-gap to try to prevent the test from hanging.
37 time.sleep(self.MAX_DURATION + 5)
38 print >> sys.__stdout__, " child should not have to kill parent"
39 for signame in "SIGHUP", "SIGUSR1", "SIGUSR2", "SIGALRM":
40 os.kill(parent_pid, getattr(signal, signame))
41 print >> sys.__stdout__, " child sent", signame, "to", \
42 parent_pid
43 time.sleep(1)
44 finally:
45 os._exit(0)
Tim Peters1742f332006-08-12 04:42:47 +000046
Georg Brandl9f2b93e2007-08-24 18:07:52 +000047 def handlerA(self, *args):
48 self.a_called = True
49 if test_support.verbose:
50 print "handlerA invoked", args
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000051
Georg Brandl9f2b93e2007-08-24 18:07:52 +000052 def handlerB(self, *args):
53 self.b_called = True
54 if test_support.verbose:
55 print "handlerB invoked", args
56 raise HandlerBCalled(*args)
Neal Norwitz9730bcb2006-01-23 07:50:06 +000057
Georg Brandl9f2b93e2007-08-24 18:07:52 +000058 def test_main(self):
59 self.assertEquals(signal.getsignal(signal.SIGHUP), self.handlerA)
60 self.assertEquals(signal.getsignal(signal.SIGUSR1), self.handlerB)
61 self.assertEquals(signal.getsignal(signal.SIGUSR2), signal.SIG_IGN)
62 self.assertEquals(signal.getsignal(signal.SIGALRM),
63 signal.default_int_handler)
Fred Drake004d5e62000-10-23 17:22:08 +000064
Georg Brandl9f2b93e2007-08-24 18:07:52 +000065 # Launch an external script to send us signals.
66 # We expect the external script to:
67 # send HUP, which invokes handlerA to set a_called
68 # send USR1, which invokes handlerB to set b_called and raise
69 # HandlerBCalled
70 # send USR2, which is ignored
71 #
72 # Then we expect the alarm to go off, and its handler raises
73 # KeyboardInterrupt, finally getting us out of the loop.
Michael W. Hudson5c26e862004-06-11 18:09:28 +000074
Georg Brandl9f2b93e2007-08-24 18:07:52 +000075 if test_support.verbose:
76 verboseflag = '-x'
77 else:
78 verboseflag = '+x'
Tim Peters1742f332006-08-12 04:42:47 +000079
Georg Brandl9f2b93e2007-08-24 18:07:52 +000080 pid = self.pid
81 if test_support.verbose:
82 print "test runner's pid is", pid
Tim Peters1742f332006-08-12 04:42:47 +000083
Georg Brandl9f2b93e2007-08-24 18:07:52 +000084 # Shell script that will send us asynchronous signals
85 script = """
86 (
87 set %(verboseflag)s
88 sleep 2
89 kill -HUP %(pid)d
90 sleep 2
91 kill -USR1 %(pid)d
92 sleep 2
93 kill -USR2 %(pid)d
94 ) &
95 """ % vars()
Tim Peters1742f332006-08-12 04:42:47 +000096
Georg Brandl9f2b93e2007-08-24 18:07:52 +000097 signal.alarm(self.MAX_DURATION)
Michael W. Hudson5c26e862004-06-11 18:09:28 +000098
Georg Brandl9f2b93e2007-08-24 18:07:52 +000099 handler_b_exception_raised = False
Neal Norwitz313f8a92006-07-30 19:20:42 +0000100
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000101 os.system(script)
102 try:
103 if test_support.verbose:
104 print "starting pause() loop..."
105 while 1:
106 try:
107 if test_support.verbose:
108 print "call pause()..."
109 signal.pause()
110 if test_support.verbose:
111 print "pause() returned"
112 except HandlerBCalled:
113 handler_b_exception_raised = True
114 if test_support.verbose:
115 print "HandlerBCalled exception caught"
Neal Norwitzec3c5e32006-07-30 19:18:38 +0000116
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000117 except KeyboardInterrupt:
118 if test_support.verbose:
119 print "KeyboardInterrupt (the alarm() went off)"
Neal Norwitzec3c5e32006-07-30 19:18:38 +0000120
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000121 self.assert_(self.a_called)
122 self.assert_(self.b_called)
123 self.assert_(handler_b_exception_raised)
Tim Peters1742f332006-08-12 04:42:47 +0000124
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000125 def setUp(self):
126 # Install handlers.
127 self.hup = signal.signal(signal.SIGHUP, self.handlerA)
128 self.usr1 = signal.signal(signal.SIGUSR1, self.handlerB)
129 self.usr2 = signal.signal(signal.SIGUSR2, signal.SIG_IGN)
130 self.alrm = signal.signal(signal.SIGALRM,
131 signal.default_int_handler)
132 self.a_called = False
133 self.b_called = False
134 self.pid = os.getpid()
135 self.fork_pid = self.spawn_force_test_exit_process(self.pid)
136
137 def tearDown(self):
138 # Forcibly kill the child we created to ping us if there was a
139 # test error.
140 try:
141 # Make sure we don't kill ourself if there was a fork
142 # error.
143 if self.fork_pid > 0:
144 os.kill(self.fork_pid, signal.SIGKILL)
145 except:
146 # If the child killed us, it has probably exited. Killing
147 # a non-existent process will raise an error which we
148 # don't care about.
149 pass
150
151 # Restore handlers.
152 signal.alarm(0) # cancel alarm in case we died early
153 signal.signal(signal.SIGHUP, self.hup)
154 signal.signal(signal.SIGUSR1, self.usr1)
155 signal.signal(signal.SIGUSR2, self.usr2)
156 signal.signal(signal.SIGALRM, self.alrm)
157
158
159class BasicSignalTests(unittest.TestCase):
160 def test_out_of_range_signal_number_raises_error(self):
161 self.assertRaises(ValueError, signal.getsignal, 4242)
162
163 def trivial_signal_handler(*args):
164 pass
165
166 self.assertRaises(ValueError, signal.signal, 4242,
167 trivial_signal_handler)
168
169 def test_setting_signal_handler_to_none_raises_error(self):
170 self.assertRaises(TypeError, signal.signal,
171 signal.SIGUSR1, None)
172
Guido van Rossum02de8972007-12-19 19:41:06 +0000173class WakeupSignalTests(unittest.TestCase):
174 TIMEOUT_FULL = 10
175 TIMEOUT_HALF = 5
176
177 def test_wakeup_fd_early(self):
178 import select
179
180 signal.alarm(1)
181 before_time = time.time()
182 # We attempt to get a signal during the sleep,
183 # before select is called
184 time.sleep(self.TIMEOUT_FULL)
185 mid_time = time.time()
186 self.assert_(mid_time - before_time < self.TIMEOUT_HALF)
187 select.select([self.read], [], [], self.TIMEOUT_FULL)
188 after_time = time.time()
189 self.assert_(after_time - mid_time < self.TIMEOUT_HALF)
190
191 def test_wakeup_fd_during(self):
192 import select
193
194 signal.alarm(1)
195 before_time = time.time()
196 # We attempt to get a signal during the select call
197 self.assertRaises(select.error, select.select,
198 [self.read], [], [], self.TIMEOUT_FULL)
199 after_time = time.time()
200 self.assert_(after_time - before_time < self.TIMEOUT_HALF)
201
202 def setUp(self):
203 import fcntl
204
205 self.alrm = signal.signal(signal.SIGALRM, lambda x,y:None)
206 self.read, self.write = os.pipe()
207 flags = fcntl.fcntl(self.write, fcntl.F_GETFL, 0)
208 flags = flags | os.O_NONBLOCK
209 fcntl.fcntl(self.write, fcntl.F_SETFL, flags)
210 self.old_wakeup = signal.set_wakeup_fd(self.write)
211
212 def tearDown(self):
213 signal.set_wakeup_fd(self.old_wakeup)
214 os.close(self.read)
215 os.close(self.write)
216 signal.signal(signal.SIGALRM, self.alrm)
217
Facundo Batista7e251e82008-02-23 15:07:35 +0000218class SiginterruptTest(unittest.TestCase):
219 signum = signal.SIGUSR1
220 def readpipe_interrupted(self, cb):
221 r, w = os.pipe()
222 ppid = os.getpid()
223 pid = os.fork()
224
225 oldhandler = signal.signal(self.signum, lambda x,y: None)
226 cb()
227 if pid==0:
228 # child code: sleep, kill, sleep. and then exit,
229 # which closes the pipe from which the parent process reads
230 try:
231 time.sleep(0.2)
232 os.kill(ppid, self.signum)
233 time.sleep(0.2)
234 finally:
235 os._exit(0)
236
237 try:
238 os.close(w)
239
240 try:
241 d=os.read(r, 1)
242 return False
243 except OSError, err:
244 if err.errno != errno.EINTR:
245 raise
246 return True
247 finally:
248 signal.signal(self.signum, oldhandler)
249 os.waitpid(pid, 0)
250
251 def test_without_siginterrupt(self):
252 i=self.readpipe_interrupted(lambda: None)
253 self.assertEquals(i, True)
254
255 def test_siginterrupt_on(self):
256 i=self.readpipe_interrupted(lambda: signal.siginterrupt(self.signum, 1))
257 self.assertEquals(i, True)
258
259 def test_siginterrupt_off(self):
260 i=self.readpipe_interrupted(lambda: signal.siginterrupt(self.signum, 0))
261 self.assertEquals(i, False)
Guido van Rossum02de8972007-12-19 19:41:06 +0000262
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000263def test_main():
Guido van Rossum02de8972007-12-19 19:41:06 +0000264 test_support.run_unittest(BasicSignalTests, InterProcessSignalTests,
Facundo Batista7e251e82008-02-23 15:07:35 +0000265 WakeupSignalTests, SiginterruptTest)
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000266
267
268if __name__ == "__main__":
269 test_main()