Optimize setUpClass() and tearDownClass() in test_process

This commit is contained in:
Dmitri Bogomolov 2020-11-02 14:05:54 +02:00
parent 96a784b58b
commit db11d6331f
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13

View File

@ -61,29 +61,26 @@ class TestProcessProto(unittest.TestCase):
cls._cleanup_files() cls._cleanup_files()
os.environ['BITMESSAGE_HOME'] = cls.home os.environ['BITMESSAGE_HOME'] = cls.home
put_signal_file(cls.home, 'unittest.lock') put_signal_file(cls.home, 'unittest.lock')
starttime = int(time.time()) starttime = int(time.time()) - 0.5
subprocess.Popen( cls.process = psutil.Popen(
cls._process_cmd, cls._process_cmd, stderr=subprocess.STDOUT) # nosec
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # nosec
timeout = starttime + 30 pidfile = os.path.join(cls.home, 'singleton.lock')
while time.time() <= timeout: for _ in range(10):
try:
if os.path.exists(os.path.join(cls.home,
'singleton.lock')):
pstat = os.stat(os.path.join(cls.home, 'singleton.lock'))
if starttime <= pstat.st_mtime and pstat.st_size > 0:
break
except OSError:
break
time.sleep(1) time.sleep(1)
if time.time() >= timeout: try:
raise psutil.TimeoutExpired( pstat = os.stat(pidfile)
"Timeout starting {}".format(cls._process_cmd)) if starttime <= pstat.st_mtime and pstat.st_size > 0:
# wait a bit for the program to fully start break # the pidfile is suitable
# 10 sec should be enough except OSError:
time.sleep(10) continue
cls.pid = int(cls._get_readline('singleton.lock'))
cls.process = psutil.Process(cls.pid) try:
pid = int(cls._get_readline('singleton.lock'))
cls.process = psutil.Process(pid)
time.sleep(5)
except (psutil.NoSuchProcess, TypeError):
cls.flag = True
def setUp(self): def setUp(self):
if self.flag: if self.flag:
@ -126,9 +123,15 @@ class TestProcessProto(unittest.TestCase):
def tearDownClass(cls): def tearDownClass(cls):
"""Ensures that pybitmessage stopped and removes files""" """Ensures that pybitmessage stopped and removes files"""
try: try:
if not cls._stop_process(): if not cls._stop_process(10):
cls.process.kill() processes = cls.process.children(recursive=True)
except (psutil.NoSuchProcess, AttributeError): processes.append(cls.process)
for p in processes:
try:
p.kill()
except psutil.NoSuchProcess:
pass
except psutil.NoSuchProcess:
pass pass
finally: finally:
cls._cleanup_files() cls._cleanup_files()
@ -188,18 +191,6 @@ class TestProcessShutdown(TestProcessProto):
self._stop_process(20), self._stop_process(20),
'%s has not stopped in 20 sec' % ' '.join(self._process_cmd)) '%s has not stopped in 20 sec' % ' '.join(self._process_cmd))
@classmethod
def tearDownClass(cls):
"""Special teardown because pybitmessage is already stopped"""
try:
if cls.process.is_running():
cls.process.kill()
cls.process.wait(5)
except (psutil.TimeoutExpired, psutil.NoSuchProcess):
pass
finally:
cls._cleanup_files()
class TestProcess(TestProcessProto): class TestProcess(TestProcessProto):
"""A test case for pybitmessage process""" """A test case for pybitmessage process"""