31 """Setup module for given signal settings"""
39 """If init_signal is true, kill on init, otherwise just print info"""
43 basf2.B2INFO(f
"Killing {pid} in init (sig {int(self.init_signal)})")
45 basf2.B2INFO(
"initialize()")
48 """If init_signal is true raise error, if event_signal is true kill process, otherwise print info"""
50 basf2.B2FATAL(
"Processing should have been stopped in init!")
53 basf2.B2INFO(f
"Killing {pid} in event (sig {int(self.event_signal)})")
55 basf2.B2INFO(
"event()")
61signal.pthread_sigmask(signal.SIG_UNBLOCK, [signal.SIGQUIT])
64os.system(
'clear_basf2_ipc')
66tmpdir = tempfile.mkdtemp(prefix=
'b2signal_test')
68basf2.set_random_seed(
"something important")
71def run_test(init_signal, event_signal, abort, test_in_process):
74 @param event_signal kill
in event()
75 @param abort Should this test give a non-zero
return code?
76 @param test_in_process 0 input, 1 parallel, 2 output
79 testFile = tempfile.NamedTemporaryFile(dir=tmpdir, delete=False)
83 retbytes = os.wait()[1]
84 retcode = (retbytes & 0xff00) >> 8
85 killsig = retbytes & 0x00ff % 128
88 if killsig
in (init_signal, event_signal):
90 if retcode != 0
or killsig == signal.SIGTERM:
93 if killsig == 0
and retcode == 0:
97 print(killsig, retcode)
99 raise RuntimeError(f
"Killed with wrong signal {killsig}?")
101 raise RuntimeError(f
"Wrong exit code {retcode}")
103 fileExists = os.path.isfile(testFile.name)
104 if fileExists
and (
not abort
or event_signal == signal.SIGINT):
106 file_ok_ret = os.system(
'b2file-metadata-show ' + testFile.name)
107 basf2.B2WARNING(
"file_ok_ret: " + str(file_ok_ret))
109 raise RuntimeError(
"Root file not properly closed!")
112 ret = os.system(
'clear_basf2_ipc')
114 raise RuntimeError(
"Some IPC structures were not cleaned up")
116 basf2.B2WARNING(
"test ok.")
122 num_events = int(1e8)
126 main.add_module(
'EventInfoSetter', evtNumList=[num_events])
127 if test_in_process == 0:
128 testmod = main.add_module(
TestModule(init_signal, event_signal))
129 main.add_module(
'ProgressBar').set_property_flags(basf2.ModulePropFlags.PARALLELPROCESSINGCERTIFIED)
130 elif test_in_process == 1:
131 testmod = main.add_module(
TestModule(init_signal, event_signal))
132 testmod.set_property_flags(basf2.ModulePropFlags.PARALLELPROCESSINGCERTIFIED)
133 elif test_in_process == 2:
134 main.add_module(
'ProgressBar').set_property_flags(basf2.ModulePropFlags.PARALLELPROCESSINGCERTIFIED)
135 testmod = main.add_module(
TestModule(init_signal, event_signal))
136 main.add_module(
'RootOutput', outputFileName=testFile.name, updateFileCatalog=
False)
138 basf2.B2WARNING(
"Running with PID " + str(os.getpid()))
144 basf2.set_nprocesses(nproc)
145 for in_proc
in [0, 1, 2]:
146 if nproc == 0
and in_proc != 0:
148 basf2.B2WARNING(f
"== starting tests with nproc={nproc}, test_in_process={in_proc}")
151 run_test(
None,
None, abort=
False, test_in_process=in_proc)
153 run_test(signal.SIGINT,
None, abort=
True, test_in_process=in_proc)
154 run_test(
None, signal.SIGINT, abort=
True, test_in_process=in_proc)
155 run_test(signal.SIGTERM,
None, abort=
True, test_in_process=in_proc)
156 run_test(
None, signal.SIGTERM, abort=
True, test_in_process=in_proc)
157 run_test(signal.SIGQUIT,
None, abort=
True, test_in_process=in_proc)
158 run_test(
None, signal.SIGQUIT, abort=
True, test_in_process=in_proc)
161 run_test(signal.SIGSEGV,
None, abort=
True, test_in_process=in_proc)
162 run_test(
None, signal.SIGSEGV, abort=
True, test_in_process=in_proc)
165 except Exception
as e:
167 basf2.B2WARNING(f
"Exception occured for nproc={nproc}, test_in_process={in_proc}")
171print(
"=========================================================================")
172print(
"All signal tests finished successfully!")
173print(
"=========================================================================")
event_signal
signal to emit to ourselves during event()
init_signal
signal to emit to ourselves during initialize()
def __init__(self, init_signal, event_signal)