@@ -61,29 +61,26 @@ def setUpClass(cls):
61
61
cls ._cleanup_files ()
62
62
os .environ ['BITMESSAGE_HOME' ] = cls .home
63
63
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 )
70
71
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
76
75
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
87
84
88
85
def setUp (self ):
89
86
if self .flag :
@@ -126,9 +123,15 @@ def _cleanup_files(cls):
126
123
def tearDownClass (cls ):
127
124
"""Ensures that pybitmessage stopped and removes files"""
128
125
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 :
132
135
pass
133
136
finally :
134
137
cls ._cleanup_files ()
@@ -188,18 +191,6 @@ def test_shutdown(self):
188
191
self ._stop_process (20 ),
189
192
'%s has not stopped in 20 sec' % ' ' .join (self ._process_cmd ))
190
193
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
-
203
194
204
195
class TestProcess (TestProcessProto ):
205
196
"""A test case for pybitmessage process"""
0 commit comments