Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
Georg Brandl | c29863e | 2009-06-18 22:24:26 +0000 | [diff] [blame] | 3 | from contextlib import closing |
Neal Norwitz | bb89e68 | 2008-03-25 07:00:39 +0000 | [diff] [blame] | 4 | import gc |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +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 |
Jeffrey Yasskin | 413f588 | 2008-03-21 18:25:06 +0000 | [diff] [blame] | 8 | import subprocess |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 9 | import traceback |
Christian Heimes | acfd8ed | 2008-02-28 21:00:45 +0000 | [diff] [blame] | 10 | import sys, os, time, errno |
| 11 | |
Brian Curtin | 24af0e9 | 2010-08-06 19:41:01 +0000 | [diff] [blame^] | 12 | if sys.platform == 'os2' or sys.platform == 'riscos': |
Benjamin Peterson | 888a39b | 2009-03-26 20:48:25 +0000 | [diff] [blame] | 13 | raise unittest.SkipTest("Can't test signal on %s" % \ |
Christian Heimes | acfd8ed | 2008-02-28 21:00:45 +0000 | [diff] [blame] | 14 | sys.platform) |
| 15 | |
Guido van Rossum | cc5a91d | 1997-04-16 00:29:15 +0000 | [diff] [blame] | 16 | |
Armin Rigo | 8b2cbfd | 2004-08-07 21:27:43 +0000 | [diff] [blame] | 17 | class HandlerBCalled(Exception): |
| 18 | pass |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 19 | |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 20 | |
| 21 | def 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 Yasskin | ab56131 | 2008-04-02 04:07:44 +0000 | [diff] [blame] | 31 | def ignoring_eintr(__func, *args, **kwargs): |
| 32 | try: |
| 33 | return __func(*args, **kwargs) |
Jeffrey Yasskin | 2b860db | 2008-04-04 04:51:19 +0000 | [diff] [blame] | 34 | except EnvironmentError as e: |
Jeffrey Yasskin | e71d812 | 2008-04-04 16:48:19 +0000 | [diff] [blame] | 35 | if e.errno != errno.EINTR: |
Jeffrey Yasskin | ab56131 | 2008-04-02 04:07:44 +0000 | [diff] [blame] | 36 | raise |
| 37 | return None |
| 38 | |
| 39 | |
Brian Curtin | 24af0e9 | 2010-08-06 19:41:01 +0000 | [diff] [blame^] | 40 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 41 | class InterProcessSignalTests(unittest.TestCase): |
| 42 | MAX_DURATION = 20 # Entire test should last at most 20 sec. |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 43 | |
Neal Norwitz | bb89e68 | 2008-03-25 07:00:39 +0000 | [diff] [blame] | 44 | 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 Yasskin | ee76777 | 2008-04-06 23:04:28 +0000 | [diff] [blame] | 52 | def format_frame(self, frame, limit=None): |
| 53 | return ''.join(traceback.format_stack(frame, limit=limit)) |
| 54 | |
| 55 | def handlerA(self, signum, frame): |
Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 56 | self.a_called = True |
| 57 | if test_support.verbose: |
Jeffrey Yasskin | ee76777 | 2008-04-06 23:04:28 +0000 | [diff] [blame] | 58 | print "handlerA invoked from signal %s at:\n%s" % ( |
| 59 | signum, self.format_frame(frame, limit=1)) |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 60 | |
Jeffrey Yasskin | ee76777 | 2008-04-06 23:04:28 +0000 | [diff] [blame] | 61 | def handlerB(self, signum, frame): |
Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 62 | self.b_called = True |
| 63 | if test_support.verbose: |
Jeffrey Yasskin | ee76777 | 2008-04-06 23:04:28 +0000 | [diff] [blame] | 64 | 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 Norwitz | 9730bcb | 2006-01-23 07:50:06 +0000 | [diff] [blame] | 67 | |
Jeffrey Yasskin | 413f588 | 2008-03-21 18:25:06 +0000 | [diff] [blame] | 68 | def wait(self, child): |
| 69 | """Wait for child to finish, ignoring EINTR.""" |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 70 | while True: |
| 71 | try: |
Jeffrey Yasskin | 413f588 | 2008-03-21 18:25:06 +0000 | [diff] [blame] | 72 | child.wait() |
Jeffrey Yasskin | 6cda88e | 2008-03-21 05:51:37 +0000 | [diff] [blame] | 73 | return |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 74 | except OSError as e: |
| 75 | if e.errno != errno.EINTR: |
| 76 | raise |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 77 | |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 78 | 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. Hudson | 5c26e86 | 2004-06-11 18:09:28 +0000 | [diff] [blame] | 85 | |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 86 | # Variables the signals will modify: |
| 87 | self.a_called = False |
| 88 | self.b_called = False |
Tim Peters | 1742f33 | 2006-08-12 04:42:47 +0000 | [diff] [blame] | 89 | |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 90 | # Let the sub-processes know who to send signals to. |
| 91 | pid = os.getpid() |
Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 92 | if test_support.verbose: |
| 93 | print "test runner's pid is", pid |
Tim Peters | 1742f33 | 2006-08-12 04:42:47 +0000 | [diff] [blame] | 94 | |
Jeffrey Yasskin | ab56131 | 2008-04-02 04:07:44 +0000 | [diff] [blame] | 95 | 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 Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 100 | self.assertTrue(self.a_called) |
| 101 | self.assertFalse(self.b_called) |
| 102 | self.a_called = False |
Tim Peters | 1742f33 | 2006-08-12 04:42:47 +0000 | [diff] [blame] | 103 | |
Jeffrey Yasskin | ee76777 | 2008-04-06 23:04:28 +0000 | [diff] [blame] | 104 | # 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 Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 108 | try: |
Jeffrey Yasskin | 413f588 | 2008-03-21 18:25:06 +0000 | [diff] [blame] | 109 | child = subprocess.Popen(['kill', '-USR1', str(pid)]) |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 110 | # This wait should be interrupted by the signal's exception. |
| 111 | self.wait(child) |
Jeffrey Yasskin | ab56131 | 2008-04-02 04:07:44 +0000 | [diff] [blame] | 112 | time.sleep(1) # Give the signal time to be delivered. |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 113 | self.fail('HandlerBCalled exception not thrown') |
| 114 | except HandlerBCalled: |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 115 | self.assertTrue(self.b_called) |
| 116 | self.assertFalse(self.a_called) |
Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 117 | if test_support.verbose: |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 118 | print "HandlerBCalled exception caught" |
Neal Norwitz | ec3c5e3 | 2006-07-30 19:18:38 +0000 | [diff] [blame] | 119 | |
Jeffrey Yasskin | ab56131 | 2008-04-02 04:07:44 +0000 | [diff] [blame] | 120 | child = ignoring_eintr(subprocess.Popen, ['kill', '-USR2', str(pid)]) |
| 121 | if child: |
| 122 | self.wait(child) # Nothing should happen. |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 123 | |
| 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 Yasskin | ab56131 | 2008-04-02 04:07:44 +0000 | [diff] [blame] | 130 | # But if another signal arrives before the alarm, pause |
| 131 | # may return early. |
| 132 | time.sleep(1) |
Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 133 | except KeyboardInterrupt: |
| 134 | if test_support.verbose: |
| 135 | print "KeyboardInterrupt (the alarm() went off)" |
Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 136 | except: |
Jeffrey Yasskin | ab56131 | 2008-04-02 04:07:44 +0000 | [diff] [blame] | 137 | self.fail("Some other exception woke us from pause: %s" % |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 138 | traceback.format_exc()) |
| 139 | else: |
Jeffrey Yasskin | ab56131 | 2008-04-02 04:07:44 +0000 | [diff] [blame] | 140 | self.fail("pause returned of its own accord, and the signal" |
| 141 | " didn't arrive after another second.") |
Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 142 | |
Stefan Krah | 68b4e01 | 2010-04-20 07:59:10 +0000 | [diff] [blame] | 143 | # Issue 3864. Unknown if this affects earlier versions of freebsd also. |
R. David Murray | e0e8a87 | 2010-04-17 05:26:26 +0000 | [diff] [blame] | 144 | @unittest.skipIf(sys.platform=='freebsd6', |
| 145 | 'inter process signals not reliable (do not mix well with threading) ' |
| 146 | 'on freebsd6') |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 147 | 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 Brandl | c29863e | 2009-06-18 22:24:26 +0000 | [diff] [blame] | 154 | with closing(os.fdopen(os_done_r)) as done_r, \ |
| 155 | closing(os.fdopen(os_done_w, 'w')) as done_w: |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 156 | 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 Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 188 | |
| 189 | |
Brian Curtin | 24af0e9 | 2010-08-06 19:41:01 +0000 | [diff] [blame^] | 190 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 191 | class BasicSignalTests(unittest.TestCase): |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 192 | def trivial_signal_handler(self, *args): |
| 193 | pass |
| 194 | |
Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 195 | def test_out_of_range_signal_number_raises_error(self): |
| 196 | self.assertRaises(ValueError, signal.getsignal, 4242) |
| 197 | |
Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 198 | self.assertRaises(ValueError, signal.signal, 4242, |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 199 | self.trivial_signal_handler) |
Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 200 | |
| 201 | def test_setting_signal_handler_to_none_raises_error(self): |
| 202 | self.assertRaises(TypeError, signal.signal, |
| 203 | signal.SIGUSR1, None) |
| 204 | |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 205 | 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 Curtin | 24af0e9 | 2010-08-06 19:41:01 +0000 | [diff] [blame^] | 213 | @unittest.skipUnless(sys.platform == "win32", "Windows specific") |
| 214 | class 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) |
| 226 | sinal.signal(7, handler) |
| 227 | |
| 228 | |
| 229 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 230 | class WakeupSignalTests(unittest.TestCase): |
| 231 | TIMEOUT_FULL = 10 |
| 232 | TIMEOUT_HALF = 5 |
| 233 | |
| 234 | def test_wakeup_fd_early(self): |
| 235 | import select |
| 236 | |
| 237 | signal.alarm(1) |
| 238 | before_time = time.time() |
| 239 | # We attempt to get a signal during the sleep, |
| 240 | # before select is called |
| 241 | time.sleep(self.TIMEOUT_FULL) |
| 242 | mid_time = time.time() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 243 | self.assertTrue(mid_time - before_time < self.TIMEOUT_HALF) |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 244 | select.select([self.read], [], [], self.TIMEOUT_FULL) |
| 245 | after_time = time.time() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 246 | self.assertTrue(after_time - mid_time < self.TIMEOUT_HALF) |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 247 | |
| 248 | def test_wakeup_fd_during(self): |
| 249 | import select |
| 250 | |
| 251 | signal.alarm(1) |
| 252 | before_time = time.time() |
| 253 | # We attempt to get a signal during the select call |
| 254 | self.assertRaises(select.error, select.select, |
| 255 | [self.read], [], [], self.TIMEOUT_FULL) |
| 256 | after_time = time.time() |
Benjamin Peterson | 5c8da86 | 2009-06-30 22:57:08 +0000 | [diff] [blame] | 257 | self.assertTrue(after_time - before_time < self.TIMEOUT_HALF) |
Guido van Rossum | 02de897 | 2007-12-19 19:41:06 +0000 | [diff] [blame] | 258 | |
| 259 | def setUp(self): |
| 260 | import fcntl |
| 261 | |
| 262 | self.alrm = signal.signal(signal.SIGALRM, lambda x,y:None) |
| 263 | self.read, self.write = os.pipe() |
| 264 | flags = fcntl.fcntl(self.write, fcntl.F_GETFL, 0) |
| 265 | flags = flags | os.O_NONBLOCK |
| 266 | fcntl.fcntl(self.write, fcntl.F_SETFL, flags) |
| 267 | self.old_wakeup = signal.set_wakeup_fd(self.write) |
| 268 | |
| 269 | def tearDown(self): |
| 270 | signal.set_wakeup_fd(self.old_wakeup) |
| 271 | os.close(self.read) |
| 272 | os.close(self.write) |
| 273 | signal.signal(signal.SIGALRM, self.alrm) |
| 274 | |
Brian Curtin | 24af0e9 | 2010-08-06 19:41:01 +0000 | [diff] [blame^] | 275 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 276 | class SiginterruptTest(unittest.TestCase): |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 277 | |
| 278 | def setUp(self): |
| 279 | """Install a no-op signal handler that can be set to allow |
| 280 | interrupts or not, and arrange for the original signal handler to be |
| 281 | re-installed when the test is finished. |
| 282 | """ |
Brian Curtin | 24af0e9 | 2010-08-06 19:41:01 +0000 | [diff] [blame^] | 283 | self.signum = signal.SIGUSR1 |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 284 | oldhandler = signal.signal(self.signum, lambda x,y: None) |
| 285 | self.addCleanup(signal.signal, self.signum, oldhandler) |
| 286 | |
| 287 | def readpipe_interrupted(self): |
| 288 | """Perform a read during which a signal will arrive. Return True if the |
| 289 | read is interrupted by the signal and raises an exception. Return False |
| 290 | if it returns normally. |
| 291 | """ |
| 292 | # Create a pipe that can be used for the read. Also clean it up |
| 293 | # when the test is over, since nothing else will (but see below for |
| 294 | # the write end). |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 295 | r, w = os.pipe() |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 296 | self.addCleanup(os.close, r) |
| 297 | |
| 298 | # Create another process which can send a signal to this one to try |
| 299 | # to interrupt the read. |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 300 | ppid = os.getpid() |
| 301 | pid = os.fork() |
| 302 | |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 303 | if pid == 0: |
| 304 | # Child code: sleep to give the parent enough time to enter the |
| 305 | # read() call (there's a race here, but it's really tricky to |
| 306 | # eliminate it); then signal the parent process. Also, sleep |
| 307 | # again to make it likely that the signal is delivered to the |
| 308 | # parent process before the child exits. If the child exits |
| 309 | # first, the write end of the pipe will be closed and the test |
| 310 | # is invalid. |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 311 | try: |
| 312 | time.sleep(0.2) |
| 313 | os.kill(ppid, self.signum) |
| 314 | time.sleep(0.2) |
| 315 | finally: |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 316 | # No matter what, just exit as fast as possible now. |
Jeffrey Yasskin | cf26f54 | 2008-03-21 05:02:44 +0000 | [diff] [blame] | 317 | exit_subprocess() |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 318 | else: |
| 319 | # Parent code. |
| 320 | # Make sure the child is eventually reaped, else it'll be a |
| 321 | # zombie for the rest of the test suite run. |
| 322 | self.addCleanup(os.waitpid, pid, 0) |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 323 | |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 324 | # Close the write end of the pipe. The child has a copy, so |
| 325 | # it's not really closed until the child exits. We need it to |
| 326 | # close when the child exits so that in the non-interrupt case |
| 327 | # the read eventually completes, otherwise we could just close |
| 328 | # it *after* the test. |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 329 | os.close(w) |
| 330 | |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 331 | # Try the read and report whether it is interrupted or not to |
| 332 | # the caller. |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 333 | try: |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 334 | d = os.read(r, 1) |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 335 | return False |
| 336 | except OSError, err: |
| 337 | if err.errno != errno.EINTR: |
| 338 | raise |
| 339 | return True |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 340 | |
| 341 | def test_without_siginterrupt(self): |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 342 | """If a signal handler is installed and siginterrupt is not called |
| 343 | at all, when that signal arrives, it interrupts a syscall that's in |
| 344 | progress. |
| 345 | """ |
| 346 | i = self.readpipe_interrupted() |
| 347 | self.assertTrue(i) |
| 348 | # Arrival of the signal shouldn't have changed anything. |
| 349 | i = self.readpipe_interrupted() |
| 350 | self.assertTrue(i) |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 351 | |
| 352 | def test_siginterrupt_on(self): |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 353 | """If a signal handler is installed and siginterrupt is called with |
| 354 | a true value for the second argument, when that signal arrives, it |
| 355 | interrupts a syscall that's in progress. |
| 356 | """ |
| 357 | signal.siginterrupt(self.signum, 1) |
| 358 | i = self.readpipe_interrupted() |
| 359 | self.assertTrue(i) |
| 360 | # Arrival of the signal shouldn't have changed anything. |
| 361 | i = self.readpipe_interrupted() |
| 362 | self.assertTrue(i) |
Facundo Batista | 7e251e8 | 2008-02-23 15:07:35 +0000 | [diff] [blame] | 363 | |
| 364 | def test_siginterrupt_off(self): |
Jean-Paul Calderone | e54ddf1 | 2010-05-08 20:06:02 +0000 | [diff] [blame] | 365 | """If a signal handler is installed and siginterrupt is called with |
| 366 | a false value for the second argument, when that signal arrives, it |
| 367 | does not interrupt a syscall that's in progress. |
| 368 | """ |
| 369 | signal.siginterrupt(self.signum, 0) |
| 370 | i = self.readpipe_interrupted() |
| 371 | self.assertFalse(i) |
| 372 | # Arrival of the signal shouldn't have changed anything. |
| 373 | i = self.readpipe_interrupted() |
| 374 | self.assertFalse(i) |
| 375 | |
| 376 | |
Brian Curtin | 24af0e9 | 2010-08-06 19:41:01 +0000 | [diff] [blame^] | 377 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 378 | class ItimerTest(unittest.TestCase): |
| 379 | def setUp(self): |
| 380 | self.hndl_called = False |
| 381 | self.hndl_count = 0 |
| 382 | self.itimer = None |
Neal Norwitz | bb89e68 | 2008-03-25 07:00:39 +0000 | [diff] [blame] | 383 | self.old_alarm = signal.signal(signal.SIGALRM, self.sig_alrm) |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 384 | |
| 385 | def tearDown(self): |
Neal Norwitz | bb89e68 | 2008-03-25 07:00:39 +0000 | [diff] [blame] | 386 | signal.signal(signal.SIGALRM, self.old_alarm) |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 387 | if self.itimer is not None: # test_itimer_exc doesn't change this attr |
| 388 | # just ensure that itimer is stopped |
| 389 | signal.setitimer(self.itimer, 0) |
| 390 | |
| 391 | def sig_alrm(self, *args): |
| 392 | self.hndl_called = True |
| 393 | if test_support.verbose: |
| 394 | print("SIGALRM handler invoked", args) |
| 395 | |
| 396 | def sig_vtalrm(self, *args): |
| 397 | self.hndl_called = True |
| 398 | |
| 399 | if self.hndl_count > 3: |
| 400 | # it shouldn't be here, because it should have been disabled. |
| 401 | raise signal.ItimerError("setitimer didn't disable ITIMER_VIRTUAL " |
| 402 | "timer.") |
| 403 | elif self.hndl_count == 3: |
| 404 | # disable ITIMER_VIRTUAL, this function shouldn't be called anymore |
| 405 | signal.setitimer(signal.ITIMER_VIRTUAL, 0) |
| 406 | if test_support.verbose: |
| 407 | print("last SIGVTALRM handler call") |
| 408 | |
| 409 | self.hndl_count += 1 |
| 410 | |
| 411 | if test_support.verbose: |
| 412 | print("SIGVTALRM handler invoked", args) |
| 413 | |
| 414 | def sig_prof(self, *args): |
| 415 | self.hndl_called = True |
| 416 | signal.setitimer(signal.ITIMER_PROF, 0) |
| 417 | |
| 418 | if test_support.verbose: |
| 419 | print("SIGPROF handler invoked", args) |
| 420 | |
| 421 | def test_itimer_exc(self): |
| 422 | # XXX I'm assuming -1 is an invalid itimer, but maybe some platform |
| 423 | # defines it ? |
| 424 | self.assertRaises(signal.ItimerError, signal.setitimer, -1, 0) |
Neal Norwitz | bb89e68 | 2008-03-25 07:00:39 +0000 | [diff] [blame] | 425 | # Negative times are treated as zero on some platforms. |
| 426 | if 0: |
| 427 | self.assertRaises(signal.ItimerError, |
| 428 | signal.setitimer, signal.ITIMER_REAL, -1) |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 429 | |
| 430 | def test_itimer_real(self): |
| 431 | self.itimer = signal.ITIMER_REAL |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 432 | signal.setitimer(self.itimer, 1.0) |
| 433 | if test_support.verbose: |
| 434 | print("\ncall pause()...") |
| 435 | signal.pause() |
| 436 | |
| 437 | self.assertEqual(self.hndl_called, True) |
| 438 | |
Stefan Krah | 68b4e01 | 2010-04-20 07:59:10 +0000 | [diff] [blame] | 439 | # Issue 3864. Unknown if this affects earlier versions of freebsd also. |
R. David Murray | e0e8a87 | 2010-04-17 05:26:26 +0000 | [diff] [blame] | 440 | @unittest.skipIf(sys.platform=='freebsd6', |
| 441 | 'itimer not reliable (does not mix well with threading) on freebsd6') |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 442 | def test_itimer_virtual(self): |
| 443 | self.itimer = signal.ITIMER_VIRTUAL |
| 444 | signal.signal(signal.SIGVTALRM, self.sig_vtalrm) |
| 445 | signal.setitimer(self.itimer, 0.3, 0.2) |
| 446 | |
Mark Dickinson | 4b841d9 | 2009-10-31 10:36:06 +0000 | [diff] [blame] | 447 | start_time = time.time() |
Stefan Krah | 68b4e01 | 2010-04-20 07:59:10 +0000 | [diff] [blame] | 448 | while time.time() - start_time < 60.0: |
Mark Dickinson | 245d915 | 2009-10-04 18:38:39 +0000 | [diff] [blame] | 449 | # use up some virtual time by doing real work |
| 450 | _ = pow(12345, 67890, 10000019) |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 451 | if signal.getitimer(self.itimer) == (0.0, 0.0): |
| 452 | break # sig_vtalrm handler stopped this itimer |
Stefan Krah | 68b4e01 | 2010-04-20 07:59:10 +0000 | [diff] [blame] | 453 | else: # Issue 8424 |
Benjamin Peterson | 5b5134b | 2010-05-15 17:48:55 +0000 | [diff] [blame] | 454 | self.skipTest("timeout: likely cause: machine too slow or load too " |
| 455 | "high") |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 456 | |
| 457 | # virtual itimer should be (0.0, 0.0) now |
| 458 | self.assertEquals(signal.getitimer(self.itimer), (0.0, 0.0)) |
| 459 | # and the handler should have been called |
| 460 | self.assertEquals(self.hndl_called, True) |
| 461 | |
Stefan Krah | 68b4e01 | 2010-04-20 07:59:10 +0000 | [diff] [blame] | 462 | # Issue 3864. Unknown if this affects earlier versions of freebsd also. |
R. David Murray | e0e8a87 | 2010-04-17 05:26:26 +0000 | [diff] [blame] | 463 | @unittest.skipIf(sys.platform=='freebsd6', |
| 464 | 'itimer not reliable (does not mix well with threading) on freebsd6') |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 465 | def test_itimer_prof(self): |
| 466 | self.itimer = signal.ITIMER_PROF |
| 467 | signal.signal(signal.SIGPROF, self.sig_prof) |
Jeffrey Yasskin | 2b860db | 2008-04-04 04:51:19 +0000 | [diff] [blame] | 468 | signal.setitimer(self.itimer, 0.2, 0.2) |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 469 | |
Mark Dickinson | 4b841d9 | 2009-10-31 10:36:06 +0000 | [diff] [blame] | 470 | start_time = time.time() |
Stefan Krah | 68b4e01 | 2010-04-20 07:59:10 +0000 | [diff] [blame] | 471 | while time.time() - start_time < 60.0: |
Mark Dickinson | 4b841d9 | 2009-10-31 10:36:06 +0000 | [diff] [blame] | 472 | # do some work |
| 473 | _ = pow(12345, 67890, 10000019) |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 474 | if signal.getitimer(self.itimer) == (0.0, 0.0): |
| 475 | break # sig_prof handler stopped this itimer |
Stefan Krah | 68b4e01 | 2010-04-20 07:59:10 +0000 | [diff] [blame] | 476 | else: # Issue 8424 |
Benjamin Peterson | 5b5134b | 2010-05-15 17:48:55 +0000 | [diff] [blame] | 477 | self.skipTest("timeout: likely cause: machine too slow or load too " |
| 478 | "high") |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 479 | |
Jeffrey Yasskin | 2b860db | 2008-04-04 04:51:19 +0000 | [diff] [blame] | 480 | # profiling itimer should be (0.0, 0.0) now |
| 481 | self.assertEquals(signal.getitimer(self.itimer), (0.0, 0.0)) |
| 482 | # and the handler should have been called |
Martin v. Löwis | aef18b1 | 2008-03-24 13:31:16 +0000 | [diff] [blame] | 483 | self.assertEqual(self.hndl_called, True) |
| 484 | |
Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 485 | def test_main(): |
Brian Curtin | 24af0e9 | 2010-08-06 19:41:01 +0000 | [diff] [blame^] | 486 | if sys.platform == "win32": |
| 487 | support.run_unittest(WindowsSignalTests) |
| 488 | else: |
| 489 | support.run_unittest(BasicSignalTests, InterProcessSignalTests, |
| 490 | WakeupSignalTests, SiginterruptTest, ItimerTest) |
Georg Brandl | 9f2b93e | 2007-08-24 18:07:52 +0000 | [diff] [blame] | 491 | |
| 492 | |
| 493 | if __name__ == "__main__": |
| 494 | test_main() |