blob: 332001f6a20e0c772680a82b18afebfeba237671 [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
Facundo Batista7e251e82008-02-23 15:07:35 +00004import os, sys, time, errno
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
Guido van Rossum02de8972007-12-19 19:41:06 +0000168class WakeupSignalTests(unittest.TestCase):
169 TIMEOUT_FULL = 10
170 TIMEOUT_HALF = 5
171
172 def test_wakeup_fd_early(self):
173 import select
174
175 signal.alarm(1)
176 before_time = time.time()
177 # We attempt to get a signal during the sleep,
178 # before select is called
179 time.sleep(self.TIMEOUT_FULL)
180 mid_time = time.time()
181 self.assert_(mid_time - before_time < self.TIMEOUT_HALF)
182 select.select([self.read], [], [], self.TIMEOUT_FULL)
183 after_time = time.time()
184 self.assert_(after_time - mid_time < self.TIMEOUT_HALF)
185
186 def test_wakeup_fd_during(self):
187 import select
188
189 signal.alarm(1)
190 before_time = time.time()
191 # We attempt to get a signal during the select call
192 self.assertRaises(select.error, select.select,
193 [self.read], [], [], self.TIMEOUT_FULL)
194 after_time = time.time()
195 self.assert_(after_time - before_time < self.TIMEOUT_HALF)
196
197 def setUp(self):
198 import fcntl
199
200 self.alrm = signal.signal(signal.SIGALRM, lambda x,y:None)
201 self.read, self.write = os.pipe()
202 flags = fcntl.fcntl(self.write, fcntl.F_GETFL, 0)
203 flags = flags | os.O_NONBLOCK
204 fcntl.fcntl(self.write, fcntl.F_SETFL, flags)
205 self.old_wakeup = signal.set_wakeup_fd(self.write)
206
207 def tearDown(self):
208 signal.set_wakeup_fd(self.old_wakeup)
209 os.close(self.read)
210 os.close(self.write)
211 signal.signal(signal.SIGALRM, self.alrm)
212
Facundo Batista7e251e82008-02-23 15:07:35 +0000213class SiginterruptTest(unittest.TestCase):
214 signum = signal.SIGUSR1
215 def readpipe_interrupted(self, cb):
216 r, w = os.pipe()
217 ppid = os.getpid()
218 pid = os.fork()
219
220 oldhandler = signal.signal(self.signum, lambda x,y: None)
221 cb()
222 if pid==0:
223 # child code: sleep, kill, sleep. and then exit,
224 # which closes the pipe from which the parent process reads
225 try:
226 time.sleep(0.2)
227 os.kill(ppid, self.signum)
228 time.sleep(0.2)
229 finally:
230 os._exit(0)
231
232 try:
233 os.close(w)
234
235 try:
236 d=os.read(r, 1)
237 return False
238 except OSError, err:
239 if err.errno != errno.EINTR:
240 raise
241 return True
242 finally:
243 signal.signal(self.signum, oldhandler)
244 os.waitpid(pid, 0)
245
246 def test_without_siginterrupt(self):
247 i=self.readpipe_interrupted(lambda: None)
248 self.assertEquals(i, True)
249
250 def test_siginterrupt_on(self):
251 i=self.readpipe_interrupted(lambda: signal.siginterrupt(self.signum, 1))
252 self.assertEquals(i, True)
253
254 def test_siginterrupt_off(self):
255 i=self.readpipe_interrupted(lambda: signal.siginterrupt(self.signum, 0))
256 self.assertEquals(i, False)
Guido van Rossum02de8972007-12-19 19:41:06 +0000257
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000258def test_main():
259 if sys.platform[:3] in ('win', 'os2') or sys.platform == 'riscos':
260 raise test_support.TestSkipped("Can't test signal on %s" % \
261 sys.platform)
262
Guido van Rossum02de8972007-12-19 19:41:06 +0000263 test_support.run_unittest(BasicSignalTests, InterProcessSignalTests,
Facundo Batista7e251e82008-02-23 15:07:35 +0000264 WakeupSignalTests, SiginterruptTest)
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000265
266
267if __name__ == "__main__":
268 test_main()