Skip to content

Commit

Permalink
pythongh-126434: Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarref committed Nov 12, 2024
1 parent e5302e6 commit cbecc76
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Lib/test/multiprocessingdata/event_signal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import multiprocessing
import os
import signal
import concurrent.futures


def send_sigint(pid):
print('send_sigint running')
os.kill(pid, signal.SIGINT)


def run_signal_handler_test():
shutdown_event = multiprocessing.Event()

def sigterm_handler(_signo, _stack_frame):
try:
print('sigterm_handler running')
shutdown_event.set()
finally:
print('sigterm_handler done')

signal.signal(signal.SIGINT, sigterm_handler)

with concurrent.futures.ProcessPoolExecutor() as executor:
f = executor.submit(send_sigint, os.getpid())
while not shutdown_event.is_set():
pass
print('Received shutdown event')
f.result()


if __name__ == '__main__':
run_signal_handler_test()
5 changes: 5 additions & 0 deletions Lib/test/test_multiprocessing_event/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os.path
from test import support

def load_tests(*args):
return support.load_package_tests(os.path.dirname(__file__), *args)
41 changes: 41 additions & 0 deletions Lib/test/test_multiprocessing_event/test_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import unittest
from test import support
import sys
import signal
import os


try:
import multiprocessing
from concurrent.futures import ProcessPoolExecutor
_have_multiprocessing = True
except (NotImplementedError, ModuleNotFoundError):
_have_multiprocessing = False


class TestEventSignalHandling(unittest.TestCase):
@unittest.skipUnless(_have_multiprocessing,
"requires multiprocessing")
@unittest.skipUnless(hasattr(signal, 'signal'),
"Requires signal.signal")
@unittest.skipUnless(hasattr(signal, 'SIGINT'),
"Requires signal.SIGINT")
@unittest.skipUnless(hasattr(os, 'kill'),
"Requires os.kill")
@unittest.skipUnless(hasattr(os, 'getppid'),
"Requires os.getppid")
@support.requires_subprocess()
def test_event_signal_handling(self):
import subprocess
script = support.findfile("event_signal.py", subdir="multiprocessingdata")
for x in range(10):
try:
exit_code = subprocess.call([sys.executable, script], stdout=subprocess.DEVNULL, timeout=3)
assert exit_code == 0
except subprocess.TimeoutExpired:
assert False, 'subprocess.Timeoutexpired for event_signal.py'


if __name__ == '__main__':
unittest.main()

2 changes: 2 additions & 0 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,7 @@ TESTSUBDIRS= idlelib/idle_test \
test/leakers \
test/libregrtest \
test/mathdata \
test/multiprocessingdata \
test/regrtestdata \
test/regrtestdata/import_from_tests \
test/regrtestdata/import_from_tests/test_regrtest_b \
Expand Down Expand Up @@ -2519,6 +2520,7 @@ TESTSUBDIRS= idlelib/idle_test \
test/test_interpreters \
test/test_json \
test/test_module \
test/test_multiprocessing_event \
test/test_multiprocessing_fork \
test/test_multiprocessing_forkserver \
test/test_multiprocessing_spawn \
Expand Down

0 comments on commit cbecc76

Please sign in to comment.