Skip to content

Commit db11d63

Browse files
committed
Optimize setUpClass() and tearDownClass() in test_process
1 parent 96a784b commit db11d63

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

src/tests/test_process.py

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,26 @@ def setUpClass(cls):
6161
cls._cleanup_files()
6262
os.environ['BITMESSAGE_HOME'] = cls.home
6363
put_signal_file(cls.home, 'unittest.lock')
64-
starttime = int(time.time())
65-
subprocess.Popen(
66-
cls._process_cmd,
67-
stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # nosec
68-
timeout = starttime + 30
69-
while time.time() <= timeout:
64+
starttime = int(time.time()) - 0.5
65+
cls.process = psutil.Popen(
66+
cls._process_cmd, stderr=subprocess.STDOUT) # nosec
67+
68+
pidfile = os.path.join(cls.home, 'singleton.lock')
69+
for _ in range(10):
70+
time.sleep(1)
7071
try:
71-
if os.path.exists(os.path.join(cls.home,
72-
'singleton.lock')):
73-
pstat = os.stat(os.path.join(cls.home, 'singleton.lock'))
74-
if starttime <= pstat.st_mtime and pstat.st_size > 0:
75-
break
72+
pstat = os.stat(pidfile)
73+
if starttime <= pstat.st_mtime and pstat.st_size > 0:
74+
break # the pidfile is suitable
7675
except OSError:
77-
break
78-
time.sleep(1)
79-
if time.time() >= timeout:
80-
raise psutil.TimeoutExpired(
81-
"Timeout starting {}".format(cls._process_cmd))
82-
# wait a bit for the program to fully start
83-
# 10 sec should be enough
84-
time.sleep(10)
85-
cls.pid = int(cls._get_readline('singleton.lock'))
86-
cls.process = psutil.Process(cls.pid)
76+
continue
77+
78+
try:
79+
pid = int(cls._get_readline('singleton.lock'))
80+
cls.process = psutil.Process(pid)
81+
time.sleep(5)
82+
except (psutil.NoSuchProcess, TypeError):
83+
cls.flag = True
8784

8885
def setUp(self):
8986
if self.flag:
@@ -126,9 +123,15 @@ def _cleanup_files(cls):
126123
def tearDownClass(cls):
127124
"""Ensures that pybitmessage stopped and removes files"""
128125
try:
129-
if not cls._stop_process():
130-
cls.process.kill()
131-
except (psutil.NoSuchProcess, AttributeError):
126+
if not cls._stop_process(10):
127+
processes = cls.process.children(recursive=True)
128+
processes.append(cls.process)
129+
for p in processes:
130+
try:
131+
p.kill()
132+
except psutil.NoSuchProcess:
133+
pass
134+
except psutil.NoSuchProcess:
132135
pass
133136
finally:
134137
cls._cleanup_files()
@@ -188,18 +191,6 @@ def test_shutdown(self):
188191
self._stop_process(20),
189192
'%s has not stopped in 20 sec' % ' '.join(self._process_cmd))
190193

191-
@classmethod
192-
def tearDownClass(cls):
193-
"""Special teardown because pybitmessage is already stopped"""
194-
try:
195-
if cls.process.is_running():
196-
cls.process.kill()
197-
cls.process.wait(5)
198-
except (psutil.TimeoutExpired, psutil.NoSuchProcess):
199-
pass
200-
finally:
201-
cls._cleanup_files()
202-
203194

204195
class TestProcess(TestProcessProto):
205196
"""A test case for pybitmessage process"""

0 commit comments

Comments
 (0)