Bug 1433905 - [mozprocess] Retrieving pid has to fail with RuntimeError if process hasn't been started yet. r=gbrown

Instead of an AttributeError a RuntimeError has to be thrown if
the underlying process hasn't been created yet.

Depends on D7392

Differential Revision: https://phabricator.services.mozilla.com/D7393

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Henrik Skupin 2018-10-04 10:48:50 +00:00
parent 2141453224
commit 8744982dbc
3 changed files with 61 additions and 6 deletions

View File

@ -789,9 +789,9 @@ falling back to not using job objects for managing child processes""", file=sys.
:param sig: Signal used to kill the process, defaults to SIGKILL
(has no effect on Windows)
"""
if not hasattr(self, 'proc'):
raise RuntimeError("Calling kill() on a non started process is not"
" allowed.")
if not hasattr(self, "proc"):
raise RuntimeError("Process hasn't been started yet")
self.proc.kill(sig=sig)
# When we kill the the managed process we also have to wait for the
@ -811,9 +811,9 @@ falling back to not using job objects for managing child processes""", file=sys.
# Ensure that we first check for the reader status. Otherwise
# we might mark the process as finished while output is still getting
# processed.
if not hasattr(self, 'proc'):
raise RuntimeError("Calling poll() on a non started process is not"
" allowed.")
if not hasattr(self, "proc"):
raise RuntimeError("Process hasn't been started yet")
elif self.reader.is_alive():
return None
elif hasattr(self.proc, "returncode"):
@ -872,6 +872,9 @@ falling back to not using job objects for managing child processes""", file=sys.
@property
def pid(self):
if not hasattr(self, "proc"):
raise RuntimeError("Process hasn't been started yet")
return self.proc.pid
@staticmethod

View File

@ -3,6 +3,7 @@ subsuite = mozbase, os == "linux"
skip-if = python == 3
[test_kill.py]
[test_misc.py]
[test_pid.py]
[test_poll.py]
[test_wait.py]
[test_output.py]

View File

@ -0,0 +1,51 @@
#!/usr/bin/env python
from __future__ import absolute_import
import os
import mozunit
from mozprocess import processhandler
import proctest
here = os.path.dirname(os.path.abspath(__file__))
class ProcTestPid(proctest.ProcTest):
"""Class to test process pid."""
def test_pid_before_run(self):
"""Process is not started, and pid is checked."""
p = processhandler.ProcessHandler([self.python])
with self.assertRaises(RuntimeError):
p.pid
def test_pid_while_running(self):
"""Process is started, and pid is checked."""
p = processhandler.ProcessHandler([self.python, self.proclaunch,
"process_normal_finish.ini"],
cwd=here)
p.run()
self.assertIsNotNone(p.pid)
self.determine_status(p, True)
p.kill()
def test_pid_after_kill(self):
"""Process is killed, and pid is checked."""
p = processhandler.ProcessHandler([self.python, self.proclaunch,
"process_normal_finish.ini"],
cwd=here)
p.run()
p.kill()
self.assertIsNotNone(p.pid)
self.determine_status(p)
if __name__ == '__main__':
mozunit.main()