Debian Codegraph @

test/jpypetest/test_signals.py

raw symbols SCIP
1 # *****************************************************************************
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # See NOTICE file for details.
16 #
17 # *****************************************************************************
18 import os
19 import signal
20 import sys
21 import threading
22 import unittest
23 import jpype
24 import subrun
25
26
27 @subrun.TestCase
28 class SignalsTest(unittest.TestCase):
29
30 @classmethod
31 def setUpClass(cls):
32 # set up signal handling before starting jpype
33 cls.sigint_event = threading.Event()
34 cls.sigterm_event = threading.Event()
35
36 def sigint_handler(sig, frame):
37 cls.sigint_event.set()
38
39 def sigterm_handler(sig, frame):
40 cls.sigterm_event.set()
41
42 signal.signal(signal.SIGINT, sigint_handler)
43 signal.signal(signal.SIGTERM, sigterm_handler)
44
45 # start jpype with interrupt=False to pass back control to python
46 jpype.startJVM(interrupt=False)
47
48 def setUp(self):
49 if sys.platform == "win32":
50 raise unittest.SkipTest("signals test not applicable on windows")
51 self.sigint_event.clear()
52 self.sigterm_event.clear()
53
54 def testSigInt(self):
55 os.kill(os.getpid(), signal.SIGINT)
56
57 # the test is executed in the main thread. The signal cannot interrupt the threading.Event.wait() call
58 # so asserting the return value of `wait` does not work.
59 # However, after returning from the wait, the control should go to the signal handler, and the next `is_set`
60 # call will reflect the actual value of the flag.
61 self.sigint_event.wait(0.1)
62 self.assertTrue(self.sigint_event.is_set())
63 self.assertFalse(self.sigterm_event.is_set())
64
65 def testSigTerm(self):
66 os.kill(os.getpid(), signal.SIGTERM)
67
68 self.sigterm_event.wait(0.1)
69 if sys.version_info < (3, 10):
70 # python versions below 3.10 do not support PyErr_SetInterruptEx
71 # so SIGTERM will be sent as SIGINT to the interpreter
72 self.assertTrue(self.sigint_event.is_set())
73 self.assertFalse(self.sigterm_event.is_set())
74 else:
75 self.assertTrue(self.sigterm_event.is_set())
76 self.assertFalse(self.sigint_event.is_set())