Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Raymond Hettinger | 722d8c3 | 2009-06-18 22:21:03 +0000 | [diff] [blame] | 3 | from contextlib import closing |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 4 | import gc |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 5 | import pickle |
| 6 | import select |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 7 | import signal |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 8 | import subprocess |
| 9 | import traceback |
Christian Heimes | c06950e | 2008-02-28 21:17:00 +0000 | [diff] [blame] | 10 | import sys, os, time, errno |
| 11 | |
Brian Curtin | c734b31 | 2010-09-06 16:04:10 +0000 | [diff] [blame] | 12 | if sys.platform in ('os2', 'riscos'): |
| 13 | raise unittest.SkipTest("Can't test signal on %s" % sys.platform) |
Christian Heimes | c06950e | 2008-02-28 21:17:00 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | cc5a91d | 1997-04-16 00:29:15 +0000 | [diff] [blame] | 15 | |
Armin Rigo | 8b2cbfd | 2004-08-07 21:27:43 +0000 | [diff] [blame] | 16 | class HandlerBCalled(Exception): |
| 17 | pass |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 18 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 19 | |
| 20 | def exit_subprocess(): |
| 21 | """Use os._exit(0) to exit the current subprocess. |
| 22 | |
| 23 | Otherwise, the test catches the SystemExit and continues executing |
| 24 | in parallel with the original test, so you wind up with an |
| 25 | exponential number of tests running concurrently. |
| 26 | """ |
| 27 | os._exit(0) |
| 28 | |
| 29 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 30 | def ignoring_eintr(__func, *args, **kwargs): |
| 31 | try: |
| 32 | return __func(*args, **kwargs) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 33 | except EnvironmentError as e: |
| 34 | if e.errno != errno.EINTR: |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 35 | raise |
| 36 | return None |
| 37 | |
| 38 | |
Brian Curtin | ef9efbd | 2010-08-06 19:27:32 +0000 | [diff] [blame] | 39 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 40 | class InterProcessSignalTests(unittest.TestCase): |
| 41 | MAX_DURATION = 20 # Entire test should last at most 20 sec. |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 42 | |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 43 | def setUp(self): |
| 44 | self.using_gc = gc.isenabled() |
| 45 | gc.disable() |
| 46 | |
| 47 | def tearDown(self): |
| 48 | if self.using_gc: |
| 49 | gc.enable() |
| 50 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 51 | def format_frame(self, frame, limit=None): |
| 52 | return ''.join(traceback.format_stack(frame, limit=limit)) |
| 53 | |
| 54 | def handlerA(self, signum, frame): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 55 | self.a_called = True |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 56 | if support.verbose: |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 57 | print("handlerA invoked from signal %s at:\n%s" % ( |
| 58 | signum, self.format_frame(frame, limit=1))) |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 59 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 60 | def handlerB(self, signum, frame): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 61 | self.b_called = True |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 62 | if support.verbose: |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 63 | print ("handlerB invoked from signal %s at:\n%s" % ( |
| 64 | signum, self.format_frame(frame, limit=1))) |
| 65 | raise HandlerBCalled(signum, self.format_frame(frame)) |
Neal Norwitz | 9730bcb | 2006-01-23 07:50:06 +0000 | [diff] [blame] | 66 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 67 | def wait(self, child): |
| 68 | """Wait for child to finish, ignoring EINTR.""" |
| 69 | while True: |
| 70 | try: |
| 71 | child.wait() |
| 72 | return |
| 73 | except OSError as e: |
| 74 | if e.errno != errno.EINTR: |
| 75 | raise |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 76 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 77 | def run_test(self): |
| 78 | # Install handlers. This function runs in a sub-process, so we |
| 79 | # don't worry about re-setting the default handlers. |
| 80 | signal.signal(signal.SIGHUP, self.handlerA) |
| 81 | signal.signal(signal.SIGUSR1, self.handlerB) |
| 82 | signal.signal(signal.SIGUSR2, signal.SIG_IGN) |
| 83 | signal.signal(signal.SIGALRM, signal.default_int_handler) |
Michael W. Hudson | 5c26e86 | 2004-06-11 18:09:28 +0000 | [diff] [blame] | 84 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 85 | # Variables the signals will modify: |
| 86 | self.a_called = False |
| 87 | self.b_called = False |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 88 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 89 | # Let the sub-processes know who to send signals to. |
| 90 | pid = os.getpid() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 91 | if support.verbose: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 92 | print("test runner's pid is", pid) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 93 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 94 | child = ignoring_eintr(subprocess.Popen, ['kill', '-HUP', str(pid)]) |
| 95 | if child: |
| 96 | self.wait(child) |
| 97 | if not self.a_called: |
| 98 | time.sleep(1) # Give the signal time to be delivered. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 99 | self.assertTrue(self.a_called) |
| 100 | self.assertFalse(self.b_called) |
| 101 | self.a_called = False |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 102 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 103 | # Make sure the signal isn't delivered while the previous |
| 104 | # Popen object is being destroyed, because __del__ swallows |
| 105 | # exceptions. |
| 106 | del child |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 107 | try: |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 108 | child = subprocess.Popen(['kill', '-USR1', str(pid)]) |
| 109 | # This wait should be interrupted by the signal's exception. |
| 110 | self.wait(child) |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 111 | time.sleep(1) # Give the signal time to be delivered. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 112 | self.fail('HandlerBCalled exception not thrown') |
| 113 | except HandlerBCalled: |
| 114 | self.assertTrue(self.b_called) |
| 115 | self.assertFalse(self.a_called) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 116 | if support.verbose: |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 117 | print("HandlerBCalled exception caught") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 118 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 119 | child = ignoring_eintr(subprocess.Popen, ['kill', '-USR2', str(pid)]) |
| 120 | if child: |
| 121 | self.wait(child) # Nothing should happen. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 122 | |
| 123 | try: |
| 124 | signal.alarm(1) |
| 125 | # The race condition in pause doesn't matter in this case, |
| 126 | # since alarm is going to raise a KeyboardException, which |
| 127 | # will skip the call. |
| 128 | signal.pause() |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 129 | # But if another signal arrives before the alarm, pause |
| 130 | # may return early. |
| 131 | time.sleep(1) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 132 | except KeyboardInterrupt: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 133 | if support.verbose: |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 134 | print("KeyboardInterrupt (the alarm() went off)") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 135 | except: |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 136 | self.fail("Some other exception woke us from pause: %s" % |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 137 | traceback.format_exc()) |
| 138 | else: |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 139 | self.fail("pause returned of its own accord, and the signal" |
| 140 | " didn't arrive after another second.") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 141 | |
R. David Murray | edcfeba | 2010-04-21 01:51:57 +0000 | [diff] [blame] | 142 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
| 143 | @unittest.skipIf(sys.platform=='freebsd6', |
| 144 | 'inter process signals not reliable (do not mix well with threading) ' |
| 145 | 'on freebsd6') |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 146 | def test_main(self): |
| 147 | # This function spawns a child process to insulate the main |
| 148 | # test-running process from all the signals. It then |
| 149 | # communicates with that child process over a pipe and |
| 150 | # re-raises information about any exceptions the child |
| 151 | # throws. The real work happens in self.run_test(). |
| 152 | os_done_r, os_done_w = os.pipe() |
Raymond Hettinger | 686057b | 2009-06-04 00:11:54 +0000 | [diff] [blame] | 153 | with closing(os.fdopen(os_done_r, 'rb')) as done_r, \ |
| 154 | closing(os.fdopen(os_done_w, 'wb')) as done_w: |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 155 | child = os.fork() |
| 156 | if child == 0: |
| 157 | # In the child process; run the test and report results |
| 158 | # through the pipe. |
| 159 | try: |
| 160 | done_r.close() |
| 161 | # Have to close done_w again here because |
| 162 | # exit_subprocess() will skip the enclosing with block. |
| 163 | with closing(done_w): |
| 164 | try: |
| 165 | self.run_test() |
| 166 | except: |
| 167 | pickle.dump(traceback.format_exc(), done_w) |
| 168 | else: |
| 169 | pickle.dump(None, done_w) |
| 170 | except: |
| 171 | print('Uh oh, raised from pickle.') |
| 172 | traceback.print_exc() |
| 173 | finally: |
| 174 | exit_subprocess() |
| 175 | |
| 176 | done_w.close() |
| 177 | # Block for up to MAX_DURATION seconds for the test to finish. |
| 178 | r, w, x = select.select([done_r], [], [], self.MAX_DURATION) |
| 179 | if done_r in r: |
| 180 | tb = pickle.load(done_r) |
| 181 | if tb: |
| 182 | self.fail(tb) |
| 183 | else: |
| 184 | os.kill(child, signal.SIGKILL) |
| 185 | self.fail('Test deadlocked after %d seconds.' % |
| 186 | self.MAX_DURATION) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 187 | |
| 188 | |
Brian Curtin | ef9efbd | 2010-08-06 19:27:32 +0000 | [diff] [blame] | 189 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 190 | class BasicSignalTests(unittest.TestCase): |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 191 | def trivial_signal_handler(self, *args): |
| 192 | pass |
| 193 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 194 | def test_out_of_range_signal_number_raises_error(self): |
| 195 | self.assertRaises(ValueError, signal.getsignal, 4242) |
| 196 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 197 | self.assertRaises(ValueError, signal.signal, 4242, |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 198 | self.trivial_signal_handler) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 199 | |
| 200 | def test_setting_signal_handler_to_none_raises_error(self): |
| 201 | self.assertRaises(TypeError, signal.signal, |
| 202 | signal.SIGUSR1, None) |
| 203 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 204 | def test_getsignal(self): |
| 205 | hup = signal.signal(signal.SIGHUP, self.trivial_signal_handler) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 206 | self.assertEqual(signal.getsignal(signal.SIGHUP), |
| 207 | self.trivial_signal_handler) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 208 | signal.signal(signal.SIGHUP, hup) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 209 | self.assertEqual(signal.getsignal(signal.SIGHUP), hup) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 210 | |
| 211 | |
Brian Curtin | ef9efbd | 2010-08-06 19:27:32 +0000 | [diff] [blame] | 212 | @unittest.skipUnless(sys.platform == "win32", "Windows specific") |
| 213 | class WindowsSignalTests(unittest.TestCase): |
| 214 | def test_issue9324(self): |
Brian Curtin | 9e88b5a | 2010-10-01 14:49:24 +0000 | [diff] [blame] | 215 | # Updated for issue #10003, adding SIGBREAK |
Brian Curtin | ef9efbd | 2010-08-06 19:27:32 +0000 | [diff] [blame] | 216 | handler = lambda x, y: None |
Brian Curtin | 9e88b5a | 2010-10-01 14:49:24 +0000 | [diff] [blame] | 217 | for sig in (signal.SIGABRT, signal.SIGBREAK, signal.SIGFPE, |
| 218 | signal.SIGILL, signal.SIGINT, signal.SIGSEGV, |
| 219 | signal.SIGTERM): |
| 220 | # Set and then reset a handler for signals that work on windows |
| 221 | signal.signal(sig, signal.signal(sig, handler)) |
Brian Curtin | ef9efbd | 2010-08-06 19:27:32 +0000 | [diff] [blame] | 222 | |
| 223 | with self.assertRaises(ValueError): |
| 224 | signal.signal(-1, handler) |
Brian Curtin | 4214356 | 2010-08-07 03:47:21 +0000 | [diff] [blame] | 225 | |
| 226 | with self.assertRaises(ValueError): |
| 227 | signal.signal(7, handler) |
Brian Curtin | ef9efbd | 2010-08-06 19:27:32 +0000 | [diff] [blame] | 228 | |
| 229 | |
| 230 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 231 | class WakeupSignalTests(unittest.TestCase): |
| 232 | TIMEOUT_FULL = 10 |
| 233 | TIMEOUT_HALF = 5 |
| 234 | |
| 235 | def test_wakeup_fd_early(self): |
| 236 | import select |
| 237 | |
| 238 | signal.alarm(1) |
| 239 | before_time = time.time() |
| 240 | # We attempt to get a signal during the sleep, |
| 241 | # before select is called |
| 242 | time.sleep(self.TIMEOUT_FULL) |
| 243 | mid_time = time.time() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 244 | self.assertTrue(mid_time - before_time < self.TIMEOUT_HALF) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 245 | select.select([self.read], [], [], self.TIMEOUT_FULL) |
| 246 | after_time = time.time() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 247 | self.assertTrue(after_time - mid_time < self.TIMEOUT_HALF) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 248 | |
| 249 | def test_wakeup_fd_during(self): |
| 250 | import select |
| 251 | |
| 252 | signal.alarm(1) |
| 253 | before_time = time.time() |
| 254 | # We attempt to get a signal during the select call |
| 255 | self.assertRaises(select.error, select.select, |
| 256 | [self.read], [], [], self.TIMEOUT_FULL) |
| 257 | after_time = time.time() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 258 | self.assertTrue(after_time - before_time < self.TIMEOUT_HALF) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 259 | |
| 260 | def setUp(self): |
| 261 | import fcntl |
| 262 | |
| 263 | self.alrm = signal.signal(signal.SIGALRM, lambda x,y:None) |
| 264 | self.read, self.write = os.pipe() |
| 265 | flags = fcntl.fcntl(self.write, fcntl.F_GETFL, 0) |
| 266 | flags = flags | os.O_NONBLOCK |
| 267 | fcntl.fcntl(self.write, fcntl.F_SETFL, flags) |
| 268 | self.old_wakeup = signal.set_wakeup_fd(self.write) |
| 269 | |
| 270 | def tearDown(self): |
| 271 | signal.set_wakeup_fd(self.old_wakeup) |
| 272 | os.close(self.read) |
| 273 | os.close(self.write) |
| 274 | signal.signal(signal.SIGALRM, self.alrm) |
| 275 | |
Brian Curtin | ef9efbd | 2010-08-06 19:27:32 +0000 | [diff] [blame] | 276 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 277 | class SiginterruptTest(unittest.TestCase): |
Jean-Paul Calderone | 6f137ca | 2010-05-09 03:18:57 +0000 | [diff] [blame] | 278 | |
| 279 | def setUp(self): |
| 280 | """Install a no-op signal handler that can be set to allow |
| 281 | interrupts or not, and arrange for the original signal handler to be |
| 282 | re-installed when the test is finished. |
| 283 | """ |
Brian Curtin | ef9efbd | 2010-08-06 19:27:32 +0000 | [diff] [blame] | 284 | self.signum = signal.SIGUSR1 |
Jean-Paul Calderone | 6f137ca | 2010-05-09 03:18:57 +0000 | [diff] [blame] | 285 | oldhandler = signal.signal(self.signum, lambda x,y: None) |
| 286 | self.addCleanup(signal.signal, self.signum, oldhandler) |
| 287 | |
| 288 | def readpipe_interrupted(self): |
| 289 | """Perform a read during which a signal will arrive. Return True if the |
| 290 | read is interrupted by the signal and raises an exception. Return False |
| 291 | if it returns normally. |
| 292 | """ |
| 293 | # Create a pipe that can be used for the read. Also clean it up |
| 294 | # when the test is over, since nothing else will (but see below for |
| 295 | # the write end). |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 296 | r, w = os.pipe() |
Jean-Paul Calderone | 6f137ca | 2010-05-09 03:18:57 +0000 | [diff] [blame] | 297 | self.addCleanup(os.close, r) |
| 298 | |
| 299 | # Create another process which can send a signal to this one to try |
| 300 | # to interrupt the read. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 301 | ppid = os.getpid() |
| 302 | pid = os.fork() |
| 303 | |
Jean-Paul Calderone | 6f137ca | 2010-05-09 03:18:57 +0000 | [diff] [blame] | 304 | if pid == 0: |
| 305 | # Child code: sleep to give the parent enough time to enter the |
| 306 | # read() call (there's a race here, but it's really tricky to |
| 307 | # eliminate it); then signal the parent process. Also, sleep |
| 308 | # again to make it likely that the signal is delivered to the |
| 309 | # parent process before the child exits. If the child exits |
| 310 | # first, the write end of the pipe will be closed and the test |
| 311 | # is invalid. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 312 | try: |
| 313 | time.sleep(0.2) |
| 314 | os.kill(ppid, self.signum) |
| 315 | time.sleep(0.2) |
| 316 | finally: |
Jean-Paul Calderone | 6f137ca | 2010-05-09 03:18:57 +0000 | [diff] [blame] | 317 | # No matter what, just exit as fast as possible now. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 318 | exit_subprocess() |
Jean-Paul Calderone | 6f137ca | 2010-05-09 03:18:57 +0000 | [diff] [blame] | 319 | else: |
| 320 | # Parent code. |
| 321 | # Make sure the child is eventually reaped, else it'll be a |
| 322 | # zombie for the rest of the test suite run. |
| 323 | self.addCleanup(os.waitpid, pid, 0) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 324 | |
Jean-Paul Calderone | 6f137ca | 2010-05-09 03:18:57 +0000 | [diff] [blame] | 325 | # Close the write end of the pipe. The child has a copy, so |
| 326 | # it's not really closed until the child exits. We need it to |
| 327 | # close when the child exits so that in the non-interrupt case |
| 328 | # the read eventually completes, otherwise we could just close |
| 329 | # it *after* the test. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 330 | os.close(w) |
| 331 | |
Jean-Paul Calderone | 6f137ca | 2010-05-09 03:18:57 +0000 | [diff] [blame] | 332 | # Try the read and report whether it is interrupted or not to |
| 333 | # the caller. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 334 | try: |
Jean-Paul Calderone | 6f137ca | 2010-05-09 03:18:57 +0000 | [diff] [blame] | 335 | d = os.read(r, 1) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 336 | return False |
| 337 | except OSError as err: |
| 338 | if err.errno != errno.EINTR: |
| 339 | raise |
| 340 | return True |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 341 | |
| 342 | def test_without_siginterrupt(self): |
Jean-Paul Calderone | 6f137ca | 2010-05-09 03:18:57 +0000 | [diff] [blame] | 343 | """If a signal handler is installed and siginterrupt is not called |
| 344 | at all, when that signal arrives, it interrupts a syscall that's in |
| 345 | progress. |
| 346 | """ |
| 347 | i = self.readpipe_interrupted() |
| 348 | self.assertTrue(i) |
| 349 | # Arrival of the signal shouldn't have changed anything. |
| 350 | i = self.readpipe_interrupted() |
| 351 | self.assertTrue(i) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 352 | |
| 353 | def test_siginterrupt_on(self): |
Jean-Paul Calderone | 6f137ca | 2010-05-09 03:18:57 +0000 | [diff] [blame] | 354 | """If a signal handler is installed and siginterrupt is called with |
| 355 | a true value for the second argument, when that signal arrives, it |
| 356 | interrupts a syscall that's in progress. |
| 357 | """ |
| 358 | signal.siginterrupt(self.signum, 1) |
| 359 | i = self.readpipe_interrupted() |
| 360 | self.assertTrue(i) |
| 361 | # Arrival of the signal shouldn't have changed anything. |
| 362 | i = self.readpipe_interrupted() |
| 363 | self.assertTrue(i) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 364 | |
| 365 | def test_siginterrupt_off(self): |
Jean-Paul Calderone | 6f137ca | 2010-05-09 03:18:57 +0000 | [diff] [blame] | 366 | """If a signal handler is installed and siginterrupt is called with |
| 367 | a false value for the second argument, when that signal arrives, it |
| 368 | does not interrupt a syscall that's in progress. |
| 369 | """ |
| 370 | signal.siginterrupt(self.signum, 0) |
| 371 | i = self.readpipe_interrupted() |
| 372 | self.assertFalse(i) |
| 373 | # Arrival of the signal shouldn't have changed anything. |
| 374 | i = self.readpipe_interrupted() |
| 375 | self.assertFalse(i) |
| 376 | |
| 377 | |
Brian Curtin | ef9efbd | 2010-08-06 19:27:32 +0000 | [diff] [blame] | 378 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 379 | class ItimerTest(unittest.TestCase): |
| 380 | def setUp(self): |
| 381 | self.hndl_called = False |
| 382 | self.hndl_count = 0 |
| 383 | self.itimer = None |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 384 | self.old_alarm = signal.signal(signal.SIGALRM, self.sig_alrm) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 385 | |
| 386 | def tearDown(self): |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 387 | signal.signal(signal.SIGALRM, self.old_alarm) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 388 | if self.itimer is not None: # test_itimer_exc doesn't change this attr |
| 389 | # just ensure that itimer is stopped |
| 390 | signal.setitimer(self.itimer, 0) |
| 391 | |
| 392 | def sig_alrm(self, *args): |
| 393 | self.hndl_called = True |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 394 | if support.verbose: |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 395 | print("SIGALRM handler invoked", args) |
| 396 | |
| 397 | def sig_vtalrm(self, *args): |
| 398 | self.hndl_called = True |
| 399 | |
| 400 | if self.hndl_count > 3: |
| 401 | # it shouldn't be here, because it should have been disabled. |
| 402 | raise signal.ItimerError("setitimer didn't disable ITIMER_VIRTUAL " |
| 403 | "timer.") |
| 404 | elif self.hndl_count == 3: |
| 405 | # disable ITIMER_VIRTUAL, this function shouldn't be called anymore |
| 406 | signal.setitimer(signal.ITIMER_VIRTUAL, 0) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 407 | if support.verbose: |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 408 | print("last SIGVTALRM handler call") |
| 409 | |
| 410 | self.hndl_count += 1 |
| 411 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 412 | if support.verbose: |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 413 | print("SIGVTALRM handler invoked", args) |
| 414 | |
| 415 | def sig_prof(self, *args): |
| 416 | self.hndl_called = True |
| 417 | signal.setitimer(signal.ITIMER_PROF, 0) |
| 418 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 419 | if support.verbose: |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 420 | print("SIGPROF handler invoked", args) |
| 421 | |
| 422 | def test_itimer_exc(self): |
| 423 | # XXX I'm assuming -1 is an invalid itimer, but maybe some platform |
| 424 | # defines it ? |
| 425 | self.assertRaises(signal.ItimerError, signal.setitimer, -1, 0) |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 426 | # Negative times are treated as zero on some platforms. |
| 427 | if 0: |
| 428 | self.assertRaises(signal.ItimerError, |
| 429 | signal.setitimer, signal.ITIMER_REAL, -1) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 430 | |
| 431 | def test_itimer_real(self): |
| 432 | self.itimer = signal.ITIMER_REAL |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 433 | signal.setitimer(self.itimer, 1.0) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 434 | if support.verbose: |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 435 | print("\ncall pause()...") |
| 436 | signal.pause() |
| 437 | |
| 438 | self.assertEqual(self.hndl_called, True) |
| 439 | |
R. David Murray | edcfeba | 2010-04-21 01:51:57 +0000 | [diff] [blame] | 440 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
Gregory P. Smith | 0f61dd0 | 2010-10-17 02:57:19 +0000 | [diff] [blame] | 441 | @unittest.skipIf(sys.platform in ('freebsd6', 'netbsd5'), |
| 442 | 'itimer not reliable (does not mix well with threading) on some BSDs.') |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 443 | def test_itimer_virtual(self): |
| 444 | self.itimer = signal.ITIMER_VIRTUAL |
| 445 | signal.signal(signal.SIGVTALRM, self.sig_vtalrm) |
| 446 | signal.setitimer(self.itimer, 0.3, 0.2) |
| 447 | |
Mark Dickinson | a14c2ed | 2009-10-31 10:38:43 +0000 | [diff] [blame] | 448 | start_time = time.time() |
Stefan Krah | 0d2fa4a | 2010-04-20 08:13:03 +0000 | [diff] [blame] | 449 | while time.time() - start_time < 60.0: |
Mark Dickinson | 2e30440 | 2009-10-04 18:43:54 +0000 | [diff] [blame] | 450 | # use up some virtual time by doing real work |
| 451 | _ = pow(12345, 67890, 10000019) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 452 | if signal.getitimer(self.itimer) == (0.0, 0.0): |
| 453 | break # sig_vtalrm handler stopped this itimer |
Stefan Krah | 0d2fa4a | 2010-04-20 08:13:03 +0000 | [diff] [blame] | 454 | else: # Issue 8424 |
Benjamin Peterson | 558cd64 | 2010-05-15 17:52:12 +0000 | [diff] [blame] | 455 | self.skipTest("timeout: likely cause: machine too slow or load too " |
| 456 | "high") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 457 | |
| 458 | # virtual itimer should be (0.0, 0.0) now |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 459 | self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0)) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 460 | # and the handler should have been called |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 461 | self.assertEqual(self.hndl_called, True) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 462 | |
R. David Murray | edcfeba | 2010-04-21 01:51:57 +0000 | [diff] [blame] | 463 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
| 464 | @unittest.skipIf(sys.platform=='freebsd6', |
| 465 | 'itimer not reliable (does not mix well with threading) on freebsd6') |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 466 | def test_itimer_prof(self): |
| 467 | self.itimer = signal.ITIMER_PROF |
| 468 | signal.signal(signal.SIGPROF, self.sig_prof) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 469 | signal.setitimer(self.itimer, 0.2, 0.2) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 470 | |
Mark Dickinson | a14c2ed | 2009-10-31 10:38:43 +0000 | [diff] [blame] | 471 | start_time = time.time() |
Stefan Krah | 0d2fa4a | 2010-04-20 08:13:03 +0000 | [diff] [blame] | 472 | while time.time() - start_time < 60.0: |
Mark Dickinson | a14c2ed | 2009-10-31 10:38:43 +0000 | [diff] [blame] | 473 | # do some work |
| 474 | _ = pow(12345, 67890, 10000019) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 475 | if signal.getitimer(self.itimer) == (0.0, 0.0): |
| 476 | break # sig_prof handler stopped this itimer |
Stefan Krah | 0d2fa4a | 2010-04-20 08:13:03 +0000 | [diff] [blame] | 477 | else: # Issue 8424 |
Benjamin Peterson | 558cd64 | 2010-05-15 17:52:12 +0000 | [diff] [blame] | 478 | self.skipTest("timeout: likely cause: machine too slow or load too " |
| 479 | "high") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 480 | |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 481 | # profiling itimer should be (0.0, 0.0) now |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 482 | self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0)) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 483 | # and the handler should have been called |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 484 | self.assertEqual(self.hndl_called, True) |
| 485 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 486 | def test_main(): |
Brian Curtin | 4214356 | 2010-08-07 03:47:21 +0000 | [diff] [blame] | 487 | support.run_unittest(BasicSignalTests, InterProcessSignalTests, |
| 488 | WakeupSignalTests, SiginterruptTest, |
| 489 | ItimerTest, WindowsSignalTests) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 490 | |
| 491 | |
| 492 | if __name__ == "__main__": |
| 493 | test_main() |