Skip to content

Commit eee846b

Browse files
author
Theofilos Manitaras
committed
Improve unit testing of sigterm handling
* Increase the time of the actual submitted SleepTest. * Use `multiprocessing.Manager` and communicate with `Namespace` objects of the `Manager` instance between parent-child processes.
1 parent b85780b commit eee846b

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

unittests/test_policies.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -262,32 +262,35 @@ def test_dependencies(self):
262262

263263
def test_sigterm(self):
264264
# Wrapper of self.runall which is used from a child process and
265-
# passes any exception to the parent process
266-
def _runall(checks, conn):
265+
# passes any exception, number of cases and failures to the parent
266+
# process
267+
def _runall(checks, ns):
267268
exc = None
268269
try:
269270
self.runall(checks)
270271
except BaseException as e:
271272
exc = e
272-
273-
stats = self.runner.stats
274-
conn.send((exc, stats.num_cases(), len(stats.failures())))
275-
conn.close()
276-
277-
rd_endpoint, wr_endpoint = multiprocessing.Pipe(duplex=False)
278-
p = multiprocessing.Process(target=_runall,
279-
args=([SleepCheck(3)], wr_endpoint))
280-
p.start()
281-
282-
# The unused write endpoint has to be closed from the parent to
283-
# ensure that the `recv()` method of `rd_endpoint` returns
284-
wr_endpoint.close()
285-
286-
# Allow some time so that the SleepCheck is submitted
287-
time.sleep(1)
288-
p.terminate()
289-
p.join()
290-
exc, num_cases, num_failures = rd_endpoint.recv()
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+
exc = ns.exc
292+
num_cases = ns.num_cases
293+
num_failures = ns.num_failures
291294

292295
# Either the test is submitted and it fails due to the termination
293296
# or it is not yet submitted when the termination signal is sent

0 commit comments

Comments
 (0)