Skip to content

Commit cbecc76

Browse files
committed
pythongh-126434: Add test
1 parent e5302e6 commit cbecc76

4 files changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import multiprocessing
2+
import os
3+
import signal
4+
import concurrent.futures
5+
6+
7+
def send_sigint(pid):
8+
print('send_sigint running')
9+
os.kill(pid, signal.SIGINT)
10+
11+
12+
def run_signal_handler_test():
13+
shutdown_event = multiprocessing.Event()
14+
15+
def sigterm_handler(_signo, _stack_frame):
16+
try:
17+
print('sigterm_handler running')
18+
shutdown_event.set()
19+
finally:
20+
print('sigterm_handler done')
21+
22+
signal.signal(signal.SIGINT, sigterm_handler)
23+
24+
with concurrent.futures.ProcessPoolExecutor() as executor:
25+
f = executor.submit(send_sigint, os.getpid())
26+
while not shutdown_event.is_set():
27+
pass
28+
print('Received shutdown event')
29+
f.result()
30+
31+
32+
if __name__ == '__main__':
33+
run_signal_handler_test()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os.path
2+
from test import support
3+
4+
def load_tests(*args):
5+
return support.load_package_tests(os.path.dirname(__file__), *args)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import unittest
2+
from test import support
3+
import sys
4+
import signal
5+
import os
6+
7+
8+
try:
9+
import multiprocessing
10+
from concurrent.futures import ProcessPoolExecutor
11+
_have_multiprocessing = True
12+
except (NotImplementedError, ModuleNotFoundError):
13+
_have_multiprocessing = False
14+
15+
16+
class TestEventSignalHandling(unittest.TestCase):
17+
@unittest.skipUnless(_have_multiprocessing,
18+
"requires multiprocessing")
19+
@unittest.skipUnless(hasattr(signal, 'signal'),
20+
"Requires signal.signal")
21+
@unittest.skipUnless(hasattr(signal, 'SIGINT'),
22+
"Requires signal.SIGINT")
23+
@unittest.skipUnless(hasattr(os, 'kill'),
24+
"Requires os.kill")
25+
@unittest.skipUnless(hasattr(os, 'getppid'),
26+
"Requires os.getppid")
27+
@support.requires_subprocess()
28+
def test_event_signal_handling(self):
29+
import subprocess
30+
script = support.findfile("event_signal.py", subdir="multiprocessingdata")
31+
for x in range(10):
32+
try:
33+
exit_code = subprocess.call([sys.executable, script], stdout=subprocess.DEVNULL, timeout=3)
34+
assert exit_code == 0
35+
except subprocess.TimeoutExpired:
36+
assert False, 'subprocess.Timeoutexpired for event_signal.py'
37+
38+
39+
if __name__ == '__main__':
40+
unittest.main()
41+

‎Makefile.pre.in‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,6 +2449,7 @@ TESTSUBDIRS= idlelib/idle_test \
24492449
test/leakers \
24502450
test/libregrtest \
24512451
test/mathdata \
2452+
test/multiprocessingdata \
24522453
test/regrtestdata \
24532454
test/regrtestdata/import_from_tests \
24542455
test/regrtestdata/import_from_tests/test_regrtest_b \
@@ -2519,6 +2520,7 @@ TESTSUBDIRS= idlelib/idle_test \
25192520
test/test_interpreters \
25202521
test/test_json \
25212522
test/test_module \
2523+
test/test_multiprocessing_event \
25222524
test/test_multiprocessing_fork \
25232525
test/test_multiprocessing_forkserver \
25242526
test/test_multiprocessing_spawn \

0 commit comments

Comments
 (0)