[Python-Dev] Reaping threads and subprocesses
Serhiy Storchaka
storchaka at gmail.com
Sun Aug 11 20:23:35 CEST 2013
Some tests uses the following idiom:
def test_main():
try:
test.support.run_unittest(...)
finally:
test.support.reap_children()
Other tests uses the following idiom:
def test_main():
key = test.support.threading_setup()
try:
test.support.run_unittest(...)
finally:
test.support.threading_cleanup(*key)
or in other words:
@test.support.reap_threads
def test_main():
test.support.run_unittest(...)
These tests are not discoverable. There are some ways to make them
discoverable.
1. Create unittest.TestCase subclasses or mixins with overloaded the
run() method.
class ThreadReaped:
def run(self, result):
key = test.support.threading_setup()
try:
return super().run(result)
finally:
test.support.threading_cleanup(*key)
class ChildReaped:
def run(self, result):
try:
return super().run(result)
finally:
test.support.reap_children()
2. Create unittest.TestCase subclasses or mixins with overloaded
setUpClass() and tearDownClass() methods.
class ThreadReaped:
@classmethod
def setUpClass(cls):
cls._threads = test.support.threading_setup()
@classmethod
def tearDownClass(cls):
test.support.threading_cleanup(*cls._threads)
class ChildReaped:
@classmethod
def tearDownClass(cls):
test.support.reap_children()
3. Create unittest.TestCase subclasses or mixins with overloaded setUp()
and tearDown() methods.
class ThreadReaped:
def setUp(self):
self._threads = test.support.threading_setup()
def tearDown(self):
test.support.threading_cleanup(*self._threads)
class ChildReaped:
def tearDown(self):
test.support.reap_children()
4. Create unittest.TestCase subclasses or mixins with using addCleanup()
in constructor.
class ThreadReaped:
def __init__(self):
self.addCleanup(test.support.threading_cleanup,
*test.support.threading_setup())
class ChildReaped:
def __init__(self):
self.addCleanup(test.support.reap_children)
Of course instead subclassing we can use decorators which modify test class.
What method is better? Do you have other suggestions?
The issue where this problem was first occurred:
http://bugs.python.org/issue16968.
More information about the Python-Dev
mailing list