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