blob: 2238842b607a8850529b4cbed3d8b1e751e5eaa4 [file] [log] [blame]
Georg Brandl9f2b93e2007-08-24 18:07:52 +00001import unittest
2from test import test_support
Georg Brandlc29863e2009-06-18 22:24:26 +00003from contextlib import closing
Neal Norwitzbb89e682008-03-25 07:00:39 +00004import gc
Jeffrey Yasskincf26f542008-03-21 05:02:44 +00005import pickle
6import select
Guido van Rossum4f17e3e1995-03-16 15:07:38 +00007import signal
Jeffrey Yasskin413f5882008-03-21 18:25:06 +00008import subprocess
Jeffrey Yasskincf26f542008-03-21 05:02:44 +00009import traceback
Christian Heimesacfd8ed2008-02-28 21:00:45 +000010import sys, os, time, errno
11
Brian Curtin24af0e92010-08-06 19:41:01 +000012if sys.platform == 'os2' or sys.platform == 'riscos':
Benjamin Peterson888a39b2009-03-26 20:48:25 +000013 raise unittest.SkipTest("Can't test signal on %s" % \
Christian Heimesacfd8ed2008-02-28 21:00:45 +000014 sys.platform)
15
Guido van Rossumcc5a91d1997-04-16 00:29:15 +000016
Armin Rigo8b2cbfd2004-08-07 21:27:43 +000017class HandlerBCalled(Exception):
18 pass
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000019
Jeffrey Yasskincf26f542008-03-21 05:02:44 +000020
21def exit_subprocess():
22 """Use os._exit(0) to exit the current subprocess.
23
24 Otherwise, the test catches the SystemExit and continues executing
25 in parallel with the original test, so you wind up with an
26 exponential number of tests running concurrently.
27 """
28 os._exit(0)
29
30
Jeffrey Yasskinab561312008-04-02 04:07:44 +000031def ignoring_eintr(__func, *args, **kwargs):
32 try:
33 return __func(*args, **kwargs)
Jeffrey Yasskin2b860db2008-04-04 04:51:19 +000034 except EnvironmentError as e:
Jeffrey Yasskine71d8122008-04-04 16:48:19 +000035 if e.errno != errno.EINTR:
Jeffrey Yasskinab561312008-04-02 04:07:44 +000036 raise
37 return None
38
39
Brian Curtin24af0e92010-08-06 19:41:01 +000040@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
Georg Brandl9f2b93e2007-08-24 18:07:52 +000041class InterProcessSignalTests(unittest.TestCase):
42 MAX_DURATION = 20 # Entire test should last at most 20 sec.
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000043
Neal Norwitzbb89e682008-03-25 07:00:39 +000044 def setUp(self):
45 self.using_gc = gc.isenabled()
46 gc.disable()
47
48 def tearDown(self):
49 if self.using_gc:
50 gc.enable()
51
Jeffrey Yasskinee767772008-04-06 23:04:28 +000052 def format_frame(self, frame, limit=None):
53 return ''.join(traceback.format_stack(frame, limit=limit))
54
55 def handlerA(self, signum, frame):
Georg Brandl9f2b93e2007-08-24 18:07:52 +000056 self.a_called = True
57 if test_support.verbose:
Jeffrey Yasskinee767772008-04-06 23:04:28 +000058 print "handlerA invoked from signal %s at:\n%s" % (
59 signum, self.format_frame(frame, limit=1))
Guido van Rossum4f17e3e1995-03-16 15:07:38 +000060
Jeffrey Yasskinee767772008-04-06 23:04:28 +000061 def handlerB(self, signum, frame):
Georg Brandl9f2b93e2007-08-24 18:07:52 +000062 self.b_called = True
63 if test_support.verbose:
Jeffrey Yasskinee767772008-04-06 23:04:28 +000064 print "handlerB invoked from signal %s at:\n%s" % (
65 signum, self.format_frame(frame, limit=1))
66 raise HandlerBCalled(signum, self.format_frame(frame))
Neal Norwitz9730bcb2006-01-23 07:50:06 +000067
Jeffrey Yasskin413f5882008-03-21 18:25:06 +000068 def wait(self, child):
69 """Wait for child to finish, ignoring EINTR."""
Jeffrey Yasskincf26f542008-03-21 05:02:44 +000070 while True:
71 try:
Jeffrey Yasskin413f5882008-03-21 18:25:06 +000072 child.wait()
Jeffrey Yasskin6cda88e2008-03-21 05:51:37 +000073 return
Jeffrey Yasskincf26f542008-03-21 05:02:44 +000074 except OSError as e:
75 if e.errno != errno.EINTR:
76 raise
Fred Drake004d5e62000-10-23 17:22:08 +000077
Jeffrey Yasskincf26f542008-03-21 05:02:44 +000078 def run_test(self):
79 # Install handlers. This function runs in a sub-process, so we
80 # don't worry about re-setting the default handlers.
81 signal.signal(signal.SIGHUP, self.handlerA)
82 signal.signal(signal.SIGUSR1, self.handlerB)
83 signal.signal(signal.SIGUSR2, signal.SIG_IGN)
84 signal.signal(signal.SIGALRM, signal.default_int_handler)
Michael W. Hudson5c26e862004-06-11 18:09:28 +000085
Jeffrey Yasskincf26f542008-03-21 05:02:44 +000086 # Variables the signals will modify:
87 self.a_called = False
88 self.b_called = False
Tim Peters1742f332006-08-12 04:42:47 +000089
Jeffrey Yasskincf26f542008-03-21 05:02:44 +000090 # Let the sub-processes know who to send signals to.
91 pid = os.getpid()
Georg Brandl9f2b93e2007-08-24 18:07:52 +000092 if test_support.verbose:
93 print "test runner's pid is", pid
Tim Peters1742f332006-08-12 04:42:47 +000094
Jeffrey Yasskinab561312008-04-02 04:07:44 +000095 child = ignoring_eintr(subprocess.Popen, ['kill', '-HUP', str(pid)])
96 if child:
97 self.wait(child)
98 if not self.a_called:
99 time.sleep(1) # Give the signal time to be delivered.
Jeffrey Yasskincf26f542008-03-21 05:02:44 +0000100 self.assertTrue(self.a_called)
101 self.assertFalse(self.b_called)
102 self.a_called = False
Tim Peters1742f332006-08-12 04:42:47 +0000103
Jeffrey Yasskinee767772008-04-06 23:04:28 +0000104 # Make sure the signal isn't delivered while the previous
105 # Popen object is being destroyed, because __del__ swallows
106 # exceptions.
107 del child
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000108 try:
Jeffrey Yasskin413f5882008-03-21 18:25:06 +0000109 child = subprocess.Popen(['kill', '-USR1', str(pid)])
Jeffrey Yasskincf26f542008-03-21 05:02:44 +0000110 # This wait should be interrupted by the signal's exception.
111 self.wait(child)
Jeffrey Yasskinab561312008-04-02 04:07:44 +0000112 time.sleep(1) # Give the signal time to be delivered.
Jeffrey Yasskincf26f542008-03-21 05:02:44 +0000113 self.fail('HandlerBCalled exception not thrown')
114 except HandlerBCalled:
Jeffrey Yasskincf26f542008-03-21 05:02:44 +0000115 self.assertTrue(self.b_called)
116 self.assertFalse(self.a_called)
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000117 if test_support.verbose:
Jeffrey Yasskincf26f542008-03-21 05:02:44 +0000118 print "HandlerBCalled exception caught"
Neal Norwitzec3c5e32006-07-30 19:18:38 +0000119
Jeffrey Yasskinab561312008-04-02 04:07:44 +0000120 child = ignoring_eintr(subprocess.Popen, ['kill', '-USR2', str(pid)])
121 if child:
122 self.wait(child) # Nothing should happen.
Jeffrey Yasskincf26f542008-03-21 05:02:44 +0000123
124 try:
125 signal.alarm(1)
126 # The race condition in pause doesn't matter in this case,
127 # since alarm is going to raise a KeyboardException, which
128 # will skip the call.
129 signal.pause()
Jeffrey Yasskinab561312008-04-02 04:07:44 +0000130 # But if another signal arrives before the alarm, pause
131 # may return early.
132 time.sleep(1)
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000133 except KeyboardInterrupt:
134 if test_support.verbose:
135 print "KeyboardInterrupt (the alarm() went off)"
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000136 except:
Jeffrey Yasskinab561312008-04-02 04:07:44 +0000137 self.fail("Some other exception woke us from pause: %s" %
Jeffrey Yasskincf26f542008-03-21 05:02:44 +0000138 traceback.format_exc())
139 else:
Jeffrey Yasskinab561312008-04-02 04:07:44 +0000140 self.fail("pause returned of its own accord, and the signal"
141 " didn't arrive after another second.")
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000142
Stefan Krah68b4e012010-04-20 07:59:10 +0000143 # Issue 3864. Unknown if this affects earlier versions of freebsd also.
R. David Murraye0e8a872010-04-17 05:26:26 +0000144 @unittest.skipIf(sys.platform=='freebsd6',
145 'inter process signals not reliable (do not mix well with threading) '
146 'on freebsd6')
Jeffrey Yasskincf26f542008-03-21 05:02:44 +0000147 def test_main(self):
148 # This function spawns a child process to insulate the main
149 # test-running process from all the signals. It then
150 # communicates with that child process over a pipe and
151 # re-raises information about any exceptions the child
152 # throws. The real work happens in self.run_test().
153 os_done_r, os_done_w = os.pipe()
Georg Brandlc29863e2009-06-18 22:24:26 +0000154 with closing(os.fdopen(os_done_r)) as done_r, \
155 closing(os.fdopen(os_done_w, 'w')) as done_w:
Jeffrey Yasskincf26f542008-03-21 05:02:44 +0000156 child = os.fork()
157 if child == 0:
158 # In the child process; run the test and report results
159 # through the pipe.
160 try:
161 done_r.close()
162 # Have to close done_w again here because
163 # exit_subprocess() will skip the enclosing with block.
164 with closing(done_w):
165 try:
166 self.run_test()
167 except:
168 pickle.dump(traceback.format_exc(), done_w)
169 else:
170 pickle.dump(None, done_w)
171 except:
172 print 'Uh oh, raised from pickle.'
173 traceback.print_exc()
174 finally:
175 exit_subprocess()
176
177 done_w.close()
178 # Block for up to MAX_DURATION seconds for the test to finish.
179 r, w, x = select.select([done_r], [], [], self.MAX_DURATION)
180 if done_r in r:
181 tb = pickle.load(done_r)
182 if tb:
183 self.fail(tb)
184 else:
185 os.kill(child, signal.SIGKILL)
186 self.fail('Test deadlocked after %d seconds.' %
187 self.MAX_DURATION)
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000188
189
Brian Curtin24af0e92010-08-06 19:41:01 +0000190@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000191class BasicSignalTests(unittest.TestCase):
Jeffrey Yasskincf26f542008-03-21 05:02:44 +0000192 def trivial_signal_handler(self, *args):
193 pass
194
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000195 def test_out_of_range_signal_number_raises_error(self):
196 self.assertRaises(ValueError, signal.getsignal, 4242)
197
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000198 self.assertRaises(ValueError, signal.signal, 4242,
Jeffrey Yasskincf26f542008-03-21 05:02:44 +0000199 self.trivial_signal_handler)
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000200
201 def test_setting_signal_handler_to_none_raises_error(self):
202 self.assertRaises(TypeError, signal.signal,
203 signal.SIGUSR1, None)
204
Jeffrey Yasskincf26f542008-03-21 05:02:44 +0000205 def test_getsignal(self):
206 hup = signal.signal(signal.SIGHUP, self.trivial_signal_handler)
207 self.assertEquals(signal.getsignal(signal.SIGHUP),
208 self.trivial_signal_handler)
209 signal.signal(signal.SIGHUP, hup)
210 self.assertEquals(signal.getsignal(signal.SIGHUP), hup)
211
212
Brian Curtin24af0e92010-08-06 19:41:01 +0000213@unittest.skipUnless(sys.platform == "win32", "Windows specific")
214class WindowsSignalTests(unittest.TestCase):
215 def test_issue9324(self):
216 handler = lambda x, y: None
217 signal.signal(signal.SIGABRT, handler)
218 signal.signal(signal.SIGFPE, handler)
219 signal.signal(signal.SIGILL, handler)
220 signal.signal(signal.SIGINT, handler)
221 signal.signal(signal.SIGSEGV, handler)
222 signal.signal(signal.SIGTERM, handler)
223
224 with self.assertRaises(ValueError):
225 signal.signal(-1, handler)
Brian Curtin9d310d32010-08-07 03:56:16 +0000226
227 with self.assertRaises(ValueError):
228 signal.signal(7, handler)
Brian Curtin24af0e92010-08-06 19:41:01 +0000229
230
231@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
Guido van Rossum02de8972007-12-19 19:41:06 +0000232class WakeupSignalTests(unittest.TestCase):
233 TIMEOUT_FULL = 10
234 TIMEOUT_HALF = 5
235
236 def test_wakeup_fd_early(self):
237 import select
238
239 signal.alarm(1)
240 before_time = time.time()
241 # We attempt to get a signal during the sleep,
242 # before select is called
243 time.sleep(self.TIMEOUT_FULL)
244 mid_time = time.time()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000245 self.assertTrue(mid_time - before_time < self.TIMEOUT_HALF)
Guido van Rossum02de8972007-12-19 19:41:06 +0000246 select.select([self.read], [], [], self.TIMEOUT_FULL)
247 after_time = time.time()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000248 self.assertTrue(after_time - mid_time < self.TIMEOUT_HALF)
Guido van Rossum02de8972007-12-19 19:41:06 +0000249
250 def test_wakeup_fd_during(self):
251 import select
252
253 signal.alarm(1)
254 before_time = time.time()
255 # We attempt to get a signal during the select call
256 self.assertRaises(select.error, select.select,
257 [self.read], [], [], self.TIMEOUT_FULL)
258 after_time = time.time()
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000259 self.assertTrue(after_time - before_time < self.TIMEOUT_HALF)
Guido van Rossum02de8972007-12-19 19:41:06 +0000260
261 def setUp(self):
262 import fcntl
263
264 self.alrm = signal.signal(signal.SIGALRM, lambda x,y:None)
265 self.read, self.write = os.pipe()
266 flags = fcntl.fcntl(self.write, fcntl.F_GETFL, 0)
267 flags = flags | os.O_NONBLOCK
268 fcntl.fcntl(self.write, fcntl.F_SETFL, flags)
269 self.old_wakeup = signal.set_wakeup_fd(self.write)
270
271 def tearDown(self):
272 signal.set_wakeup_fd(self.old_wakeup)
273 os.close(self.read)
274 os.close(self.write)
275 signal.signal(signal.SIGALRM, self.alrm)
276
Brian Curtin24af0e92010-08-06 19:41:01 +0000277@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
Facundo Batista7e251e82008-02-23 15:07:35 +0000278class SiginterruptTest(unittest.TestCase):
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000279
280 def setUp(self):
281 """Install a no-op signal handler that can be set to allow
282 interrupts or not, and arrange for the original signal handler to be
283 re-installed when the test is finished.
284 """
Brian Curtin24af0e92010-08-06 19:41:01 +0000285 self.signum = signal.SIGUSR1
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000286 oldhandler = signal.signal(self.signum, lambda x,y: None)
287 self.addCleanup(signal.signal, self.signum, oldhandler)
288
289 def readpipe_interrupted(self):
290 """Perform a read during which a signal will arrive. Return True if the
291 read is interrupted by the signal and raises an exception. Return False
292 if it returns normally.
293 """
294 # Create a pipe that can be used for the read. Also clean it up
295 # when the test is over, since nothing else will (but see below for
296 # the write end).
Facundo Batista7e251e82008-02-23 15:07:35 +0000297 r, w = os.pipe()
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000298 self.addCleanup(os.close, r)
299
300 # Create another process which can send a signal to this one to try
301 # to interrupt the read.
Facundo Batista7e251e82008-02-23 15:07:35 +0000302 ppid = os.getpid()
303 pid = os.fork()
304
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000305 if pid == 0:
306 # Child code: sleep to give the parent enough time to enter the
307 # read() call (there's a race here, but it's really tricky to
308 # eliminate it); then signal the parent process. Also, sleep
309 # again to make it likely that the signal is delivered to the
310 # parent process before the child exits. If the child exits
311 # first, the write end of the pipe will be closed and the test
312 # is invalid.
Facundo Batista7e251e82008-02-23 15:07:35 +0000313 try:
314 time.sleep(0.2)
315 os.kill(ppid, self.signum)
316 time.sleep(0.2)
317 finally:
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000318 # No matter what, just exit as fast as possible now.
Jeffrey Yasskincf26f542008-03-21 05:02:44 +0000319 exit_subprocess()
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000320 else:
321 # Parent code.
322 # Make sure the child is eventually reaped, else it'll be a
323 # zombie for the rest of the test suite run.
324 self.addCleanup(os.waitpid, pid, 0)
Facundo Batista7e251e82008-02-23 15:07:35 +0000325
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000326 # Close the write end of the pipe. The child has a copy, so
327 # it's not really closed until the child exits. We need it to
328 # close when the child exits so that in the non-interrupt case
329 # the read eventually completes, otherwise we could just close
330 # it *after* the test.
Facundo Batista7e251e82008-02-23 15:07:35 +0000331 os.close(w)
332
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000333 # Try the read and report whether it is interrupted or not to
334 # the caller.
Facundo Batista7e251e82008-02-23 15:07:35 +0000335 try:
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000336 d = os.read(r, 1)
Facundo Batista7e251e82008-02-23 15:07:35 +0000337 return False
338 except OSError, err:
339 if err.errno != errno.EINTR:
340 raise
341 return True
Facundo Batista7e251e82008-02-23 15:07:35 +0000342
343 def test_without_siginterrupt(self):
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000344 """If a signal handler is installed and siginterrupt is not called
345 at all, when that signal arrives, it interrupts a syscall that's in
346 progress.
347 """
348 i = self.readpipe_interrupted()
349 self.assertTrue(i)
350 # Arrival of the signal shouldn't have changed anything.
351 i = self.readpipe_interrupted()
352 self.assertTrue(i)
Facundo Batista7e251e82008-02-23 15:07:35 +0000353
354 def test_siginterrupt_on(self):
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000355 """If a signal handler is installed and siginterrupt is called with
356 a true value for the second argument, when that signal arrives, it
357 interrupts a syscall that's in progress.
358 """
359 signal.siginterrupt(self.signum, 1)
360 i = self.readpipe_interrupted()
361 self.assertTrue(i)
362 # Arrival of the signal shouldn't have changed anything.
363 i = self.readpipe_interrupted()
364 self.assertTrue(i)
Facundo Batista7e251e82008-02-23 15:07:35 +0000365
366 def test_siginterrupt_off(self):
Jean-Paul Calderonee54ddf12010-05-08 20:06:02 +0000367 """If a signal handler is installed and siginterrupt is called with
368 a false value for the second argument, when that signal arrives, it
369 does not interrupt a syscall that's in progress.
370 """
371 signal.siginterrupt(self.signum, 0)
372 i = self.readpipe_interrupted()
373 self.assertFalse(i)
374 # Arrival of the signal shouldn't have changed anything.
375 i = self.readpipe_interrupted()
376 self.assertFalse(i)
377
378
Brian Curtin24af0e92010-08-06 19:41:01 +0000379@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000380class ItimerTest(unittest.TestCase):
381 def setUp(self):
382 self.hndl_called = False
383 self.hndl_count = 0
384 self.itimer = None
Neal Norwitzbb89e682008-03-25 07:00:39 +0000385 self.old_alarm = signal.signal(signal.SIGALRM, self.sig_alrm)
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000386
387 def tearDown(self):
Neal Norwitzbb89e682008-03-25 07:00:39 +0000388 signal.signal(signal.SIGALRM, self.old_alarm)
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000389 if self.itimer is not None: # test_itimer_exc doesn't change this attr
390 # just ensure that itimer is stopped
391 signal.setitimer(self.itimer, 0)
392
393 def sig_alrm(self, *args):
394 self.hndl_called = True
395 if test_support.verbose:
396 print("SIGALRM handler invoked", args)
397
398 def sig_vtalrm(self, *args):
399 self.hndl_called = True
400
401 if self.hndl_count > 3:
402 # it shouldn't be here, because it should have been disabled.
403 raise signal.ItimerError("setitimer didn't disable ITIMER_VIRTUAL "
404 "timer.")
405 elif self.hndl_count == 3:
406 # disable ITIMER_VIRTUAL, this function shouldn't be called anymore
407 signal.setitimer(signal.ITIMER_VIRTUAL, 0)
408 if test_support.verbose:
409 print("last SIGVTALRM handler call")
410
411 self.hndl_count += 1
412
413 if test_support.verbose:
414 print("SIGVTALRM handler invoked", args)
415
416 def sig_prof(self, *args):
417 self.hndl_called = True
418 signal.setitimer(signal.ITIMER_PROF, 0)
419
420 if test_support.verbose:
421 print("SIGPROF handler invoked", args)
422
423 def test_itimer_exc(self):
424 # XXX I'm assuming -1 is an invalid itimer, but maybe some platform
425 # defines it ?
426 self.assertRaises(signal.ItimerError, signal.setitimer, -1, 0)
Neal Norwitzbb89e682008-03-25 07:00:39 +0000427 # Negative times are treated as zero on some platforms.
428 if 0:
429 self.assertRaises(signal.ItimerError,
430 signal.setitimer, signal.ITIMER_REAL, -1)
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000431
432 def test_itimer_real(self):
433 self.itimer = signal.ITIMER_REAL
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000434 signal.setitimer(self.itimer, 1.0)
435 if test_support.verbose:
436 print("\ncall pause()...")
437 signal.pause()
438
439 self.assertEqual(self.hndl_called, True)
440
Stefan Krah68b4e012010-04-20 07:59:10 +0000441 # Issue 3864. Unknown if this affects earlier versions of freebsd also.
R. David Murraye0e8a872010-04-17 05:26:26 +0000442 @unittest.skipIf(sys.platform=='freebsd6',
443 'itimer not reliable (does not mix well with threading) on freebsd6')
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000444 def test_itimer_virtual(self):
445 self.itimer = signal.ITIMER_VIRTUAL
446 signal.signal(signal.SIGVTALRM, self.sig_vtalrm)
447 signal.setitimer(self.itimer, 0.3, 0.2)
448
Mark Dickinson4b841d92009-10-31 10:36:06 +0000449 start_time = time.time()
Stefan Krah68b4e012010-04-20 07:59:10 +0000450 while time.time() - start_time < 60.0:
Mark Dickinson245d9152009-10-04 18:38:39 +0000451 # use up some virtual time by doing real work
452 _ = pow(12345, 67890, 10000019)
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000453 if signal.getitimer(self.itimer) == (0.0, 0.0):
454 break # sig_vtalrm handler stopped this itimer
Stefan Krah68b4e012010-04-20 07:59:10 +0000455 else: # Issue 8424
Benjamin Peterson5b5134b2010-05-15 17:48:55 +0000456 self.skipTest("timeout: likely cause: machine too slow or load too "
457 "high")
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000458
459 # virtual itimer should be (0.0, 0.0) now
460 self.assertEquals(signal.getitimer(self.itimer), (0.0, 0.0))
461 # and the handler should have been called
462 self.assertEquals(self.hndl_called, True)
463
Stefan Krah68b4e012010-04-20 07:59:10 +0000464 # Issue 3864. Unknown if this affects earlier versions of freebsd also.
R. David Murraye0e8a872010-04-17 05:26:26 +0000465 @unittest.skipIf(sys.platform=='freebsd6',
466 'itimer not reliable (does not mix well with threading) on freebsd6')
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000467 def test_itimer_prof(self):
468 self.itimer = signal.ITIMER_PROF
469 signal.signal(signal.SIGPROF, self.sig_prof)
Jeffrey Yasskin2b860db2008-04-04 04:51:19 +0000470 signal.setitimer(self.itimer, 0.2, 0.2)
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000471
Mark Dickinson4b841d92009-10-31 10:36:06 +0000472 start_time = time.time()
Stefan Krah68b4e012010-04-20 07:59:10 +0000473 while time.time() - start_time < 60.0:
Mark Dickinson4b841d92009-10-31 10:36:06 +0000474 # do some work
475 _ = pow(12345, 67890, 10000019)
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000476 if signal.getitimer(self.itimer) == (0.0, 0.0):
477 break # sig_prof handler stopped this itimer
Stefan Krah68b4e012010-04-20 07:59:10 +0000478 else: # Issue 8424
Benjamin Peterson5b5134b2010-05-15 17:48:55 +0000479 self.skipTest("timeout: likely cause: machine too slow or load too "
480 "high")
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000481
Jeffrey Yasskin2b860db2008-04-04 04:51:19 +0000482 # profiling itimer should be (0.0, 0.0) now
483 self.assertEquals(signal.getitimer(self.itimer), (0.0, 0.0))
484 # and the handler should have been called
Martin v. Löwisaef18b12008-03-24 13:31:16 +0000485 self.assertEqual(self.hndl_called, True)
486
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000487def test_main():
Brian Curtin9d310d32010-08-07 03:56:16 +0000488 test_support.run_unittest(BasicSignalTests, InterProcessSignalTests,
489 WakeupSignalTests, SiginterruptTest,
490 ItimerTest, WindowsSignalTests)
Georg Brandl9f2b93e2007-08-24 18:07:52 +0000491
492
493if __name__ == "__main__":
494 test_main()