Skip to content

Commit a32e677

Browse files
author
Vasileios Karakasis
authored
Merge pull request #1227 from teojgo/bugfix/sigterm_unittest_runner
[bugfix] Make unit test for TERM signal handling more robust
2 parents 6d54313 + fb292ca commit a32e677

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2016-2020 Swiss National Supercomputing Centre (CSCS/ETH Zurich)
2+
# ReFrame Project Developers. See the top-level LICENSE file for details.
3+
#
4+
# SPDX-License-Identifier: BSD-3-Clause
5+
6+
#
7+
# Check for testing handling of the TERM signal
8+
#
9+
import os
10+
import signal
11+
import time
12+
13+
import reframe as rfm
14+
import reframe.utility.sanity as sn
15+
16+
17+
@rfm.simple_test
18+
class SelfKillCheck(rfm.RunOnlyRegressionTest):
19+
def __init__(self):
20+
self.local = True
21+
self.valid_systems = ['*']
22+
self.valid_prog_environs = ['*']
23+
self.executable = 'echo hello'
24+
self.sanity_patterns = sn.assert_found('hello', self.stdout)
25+
self.tags = {type(self).__name__}
26+
self.maintainers = ['TM']
27+
28+
def run(self):
29+
super().run()
30+
time.sleep(0.5)
31+
os.kill(os.getpid(), signal.SIGTERM)

unittests/test_policies.py

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import collections
77
import itertools
88
import os
9-
import multiprocessing
109
import pytest
1110
import time
1211
import tempfile
@@ -261,41 +260,17 @@ def test_dependencies(self):
261260
assert os.path.exists(os.path.join(check.outputdir, 'out.txt'))
262261

263262
def test_sigterm(self):
264-
# Wrapper of self.runall which is used from a child process and
265-
# passes any exception, number of cases and failures to the parent
266-
# process
267-
def _runall(checks, ns):
268-
exc = None
269-
try:
270-
self.runall(checks)
271-
except BaseException as e:
272-
exc = e
273-
finally:
274-
ns.exc = exc
275-
ns.num_cases = self.runner.stats.num_cases()
276-
ns.num_failures = len(self.runner.stats.failures())
277-
278-
with multiprocessing.Manager() as manager:
279-
ns = manager.Namespace()
280-
p = multiprocessing.Process(target=_runall,
281-
args=([SleepCheck(20)], ns))
282-
283-
p.start()
284-
285-
# Allow some time so that the SleepCheck is submitted.
286-
# The sleep time of the submitted test is much larger to
287-
# ensure that it does not finish before the termination signal
288-
time.sleep(0.2)
289-
p.terminate()
290-
p.join()
291-
292-
# Either the test is submitted and it fails due to the termination
293-
# or it is not yet submitted when the termination signal is sent
294-
assert (ns.num_cases, ns.num_failures) in {(1, 1), (0, 0)}
295-
with pytest.raises(ReframeForceExitError,
296-
match='received TERM signal'):
297-
if ns.exc:
298-
raise ns.exc
263+
self.loader = RegressionCheckLoader(
264+
['unittests/resources/checks_unlisted/selfkill.py']
265+
)
266+
checks = self.loader.load_all()
267+
with pytest.raises(ReframeForceExitError,
268+
match='received TERM signal'):
269+
self.runall(checks)
270+
271+
self.assert_all_dead()
272+
assert self.runner.stats.num_cases() == 1
273+
assert len(self.runner.stats.failures()) == 1
299274

300275
def test_dependencies_with_retries(self):
301276
self.runner._max_retries = 2

0 commit comments

Comments
 (0)