Victor Stinner | 32eb840 | 2016-03-15 11:12:35 +0100 | [diff] [blame] | 1 | import os |
| 2 | import signal |
| 3 | import subprocess |
| 4 | import sys |
| 5 | import time |
| 6 | import unittest |
Victor Stinner | 0d63bac | 2019-12-11 11:30:03 +0100 | [diff] [blame] | 7 | from test import support |
Victor Stinner | 32eb840 | 2016-03-15 11:12:35 +0100 | [diff] [blame] | 8 | |
| 9 | |
| 10 | class SIGUSR1Exception(Exception): |
| 11 | pass |
| 12 | |
| 13 | |
| 14 | class InterProcessSignalTests(unittest.TestCase): |
| 15 | def setUp(self): |
| 16 | self.got_signals = {'SIGHUP': 0, 'SIGUSR1': 0, 'SIGALRM': 0} |
| 17 | |
| 18 | def sighup_handler(self, signum, frame): |
| 19 | self.got_signals['SIGHUP'] += 1 |
| 20 | |
| 21 | def sigusr1_handler(self, signum, frame): |
| 22 | self.got_signals['SIGUSR1'] += 1 |
| 23 | raise SIGUSR1Exception |
| 24 | |
Pablo Galindo | 2ab2afd | 2018-12-11 11:32:12 +0000 | [diff] [blame] | 25 | def wait_signal(self, child, signame): |
| 26 | if child is not None: |
| 27 | # This wait should be interrupted by exc_class |
| 28 | # (if set) |
| 29 | child.wait() |
Victor Stinner | 32eb840 | 2016-03-15 11:12:35 +0100 | [diff] [blame] | 30 | |
Victor Stinner | 0d63bac | 2019-12-11 11:30:03 +0100 | [diff] [blame] | 31 | timeout = support.SHORT_TIMEOUT |
Pablo Galindo | 2ab2afd | 2018-12-11 11:32:12 +0000 | [diff] [blame] | 32 | deadline = time.monotonic() + timeout |
Victor Stinner | 32eb840 | 2016-03-15 11:12:35 +0100 | [diff] [blame] | 33 | |
Pablo Galindo | 2ab2afd | 2018-12-11 11:32:12 +0000 | [diff] [blame] | 34 | while time.monotonic() < deadline: |
| 35 | if self.got_signals[signame]: |
Victor Stinner | 32eb840 | 2016-03-15 11:12:35 +0100 | [diff] [blame] | 36 | return |
Pablo Galindo | 2ab2afd | 2018-12-11 11:32:12 +0000 | [diff] [blame] | 37 | signal.pause() |
Victor Stinner | 32eb840 | 2016-03-15 11:12:35 +0100 | [diff] [blame] | 38 | |
| 39 | self.fail('signal %s not received after %s seconds' |
| 40 | % (signame, timeout)) |
| 41 | |
| 42 | def subprocess_send_signal(self, pid, signame): |
| 43 | code = 'import os, signal; os.kill(%s, signal.%s)' % (pid, signame) |
| 44 | args = [sys.executable, '-I', '-c', code] |
| 45 | return subprocess.Popen(args) |
| 46 | |
| 47 | def test_interprocess_signal(self): |
| 48 | # Install handlers. This function runs in a sub-process, so we |
| 49 | # don't worry about re-setting the default handlers. |
| 50 | signal.signal(signal.SIGHUP, self.sighup_handler) |
| 51 | signal.signal(signal.SIGUSR1, self.sigusr1_handler) |
| 52 | signal.signal(signal.SIGUSR2, signal.SIG_IGN) |
| 53 | signal.signal(signal.SIGALRM, signal.default_int_handler) |
| 54 | |
| 55 | # Let the sub-processes know who to send signals to. |
| 56 | pid = str(os.getpid()) |
| 57 | |
| 58 | with self.subprocess_send_signal(pid, "SIGHUP") as child: |
| 59 | self.wait_signal(child, 'SIGHUP') |
| 60 | self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 0, |
| 61 | 'SIGALRM': 0}) |
| 62 | |
Pablo Galindo | 2ab2afd | 2018-12-11 11:32:12 +0000 | [diff] [blame] | 63 | with self.assertRaises(SIGUSR1Exception): |
| 64 | with self.subprocess_send_signal(pid, "SIGUSR1") as child: |
| 65 | self.wait_signal(child, 'SIGUSR1') |
Victor Stinner | 32eb840 | 2016-03-15 11:12:35 +0100 | [diff] [blame] | 66 | self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 1, |
| 67 | 'SIGALRM': 0}) |
| 68 | |
| 69 | with self.subprocess_send_signal(pid, "SIGUSR2") as child: |
| 70 | # Nothing should happen: SIGUSR2 is ignored |
| 71 | child.wait() |
| 72 | |
Victor Stinner | 9abee72 | 2017-09-19 09:36:54 -0700 | [diff] [blame] | 73 | try: |
Pablo Galindo | 2ab2afd | 2018-12-11 11:32:12 +0000 | [diff] [blame] | 74 | with self.assertRaises(KeyboardInterrupt): |
| 75 | signal.alarm(1) |
| 76 | self.wait_signal(None, 'SIGALRM') |
Victor Stinner | 9abee72 | 2017-09-19 09:36:54 -0700 | [diff] [blame] | 77 | self.assertEqual(self.got_signals, {'SIGHUP': 1, 'SIGUSR1': 1, |
| 78 | 'SIGALRM': 0}) |
| 79 | finally: |
| 80 | signal.alarm(0) |
Victor Stinner | 32eb840 | 2016-03-15 11:12:35 +0100 | [diff] [blame] | 81 | |
| 82 | |
| 83 | if __name__ == "__main__": |
| 84 | unittest.main() |