24 """Setup module for given signal settings"""
32 """If init_signal is true, kill on init, otherwise just print info"""
36 basf2.B2INFO(
"Killing %s in init (sig %d)" % (pid, self.
init_signal))
38 basf2.B2INFO(
"initialize()")
41 """If init_signal is true raise error, if event_signal is true kill process, otherwise print info"""
43 basf2.B2FATAL(
"Processing should have been stopped in init!")
46 basf2.B2INFO(
"Killing %s in event (sig %d)" % (pid, self.
event_signal))
48 basf2.B2INFO(
"event()")
54 signal.pthread_sigmask(signal.SIG_UNBLOCK, [signal.SIGQUIT])
57 os.system(
'clear_basf2_ipc')
59 tmpdir = tempfile.mkdtemp(prefix=
'b2signal_test')
61 basf2.set_random_seed(
"something important")
64 def run_test(init_signal, event_signal, abort, test_in_process):
66 @param init_signal kill in initialize()
67 @param event_signal kill in event()
68 @param abort Should this test give a non-zero return code?
69 @param test_in_process 0 input, 1 parallel, 2 output
72 testFile = tempfile.NamedTemporaryFile(dir=tmpdir, delete=
False)
76 retbytes = os.wait()[1]
77 retcode = (retbytes & 0xff00) >> 8
78 killsig = retbytes & 0x00ff % 128
81 if killsig
in (init_signal, event_signal):
83 if retcode != 0
or killsig == signal.SIGTERM:
86 if killsig == 0
and retcode == 0:
90 print(killsig, retcode)
92 raise RuntimeError(
"Killed with wrong signal %d?" % (killsig))
94 raise RuntimeError(
"Wrong exit code %d" % (retcode))
96 fileExists = os.path.isfile(testFile.name)
97 if fileExists
and (
not abort
or event_signal == signal.SIGINT):
99 file_ok_ret = os.system(
'b2file-metadata-show ' + testFile.name)
100 basf2.B2WARNING(
"file_ok_ret: " + str(file_ok_ret))
102 raise RuntimeError(
"Root file not properly closed!")
105 ret = os.system(
'clear_basf2_ipc')
107 raise RuntimeError(
"Some IPC structures were not cleaned up")
109 basf2.B2WARNING(
"test ok.")
115 num_events = int(1e8)
119 main.add_module(
'EventInfoSetter', evtNumList=[num_events])
120 if test_in_process == 0:
121 testmod = main.add_module(
TestModule(init_signal, event_signal))
122 main.add_module(
'ProgressBar').set_property_flags(basf2.ModulePropFlags.PARALLELPROCESSINGCERTIFIED)
123 elif test_in_process == 1:
124 testmod = main.add_module(
TestModule(init_signal, event_signal))
125 testmod.set_property_flags(basf2.ModulePropFlags.PARALLELPROCESSINGCERTIFIED)
126 elif test_in_process == 2:
127 main.add_module(
'ProgressBar').set_property_flags(basf2.ModulePropFlags.PARALLELPROCESSINGCERTIFIED)
128 testmod = main.add_module(
TestModule(init_signal, event_signal))
129 main.add_module(
'RootOutput', outputFileName=testFile.name, updateFileCatalog=
False)
131 basf2.B2WARNING(
"Running with PID " + str(os.getpid()))
137 basf2.set_nprocesses(nproc)
138 for in_proc
in [0, 1, 2]:
139 if nproc == 0
and in_proc != 0:
141 basf2.B2WARNING(
"== starting tests with nproc=%d, test_in_process=%d" % (nproc, in_proc))
144 run_test(
None,
None, abort=
False, test_in_process=in_proc)
146 run_test(signal.SIGINT,
None, abort=
True, test_in_process=in_proc)
147 run_test(
None, signal.SIGINT, abort=
True, test_in_process=in_proc)
148 run_test(signal.SIGTERM,
None, abort=
True, test_in_process=in_proc)
149 run_test(
None, signal.SIGTERM, abort=
True, test_in_process=in_proc)
150 run_test(signal.SIGQUIT,
None, abort=
True, test_in_process=in_proc)
151 run_test(
None, signal.SIGQUIT, abort=
True, test_in_process=in_proc)
154 run_test(signal.SIGSEGV,
None, abort=
True, test_in_process=in_proc)
155 run_test(
None, signal.SIGSEGV, abort=
True, test_in_process=in_proc)
158 except Exception
as e:
160 basf2.B2WARNING(
"Exception occured for nproc=%d, test_in_process=%d" % (nproc, in_proc))
164 print(
"=========================================================================")
165 print(
"All signal tests finished successfully!")
166 print(
"=========================================================================")
168 shutil.rmtree(tmpdir)