Bug 1433905 - [mozprocess] Retrieving pid has to fail with RuntimeError if process hasn't been started yet. r=gbrown
authorHenrik Skupin <mail@hskupin.info>
Thu, 04 Oct 2018 10:48:50 +0000
changeset 495367 4f05d89da460e4e8d626d530acd1df9ab4c56768
parent 495366 051b1c82dac88620fa79a48c6c269262c3888301
child 495368 70607539582dc03ea44a0179aab0a1ab88785e98
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgbrown
bugs1433905
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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
testing/mozbase/mozprocess/mozprocess/processhandler.py
testing/mozbase/mozprocess/tests/manifest.ini
testing/mozbase/mozprocess/tests/test_pid.py
--- a/testing/mozbase/mozprocess/mozprocess/processhandler.py
+++ b/testing/mozbase/mozprocess/mozprocess/processhandler.py
@@ -784,19 +784,19 @@ falling back to not using job objects fo
         process, only the root process will be killed.
 
         Note that this does not manage any state, save any output etc,
         it immediately kills the process.
 
         :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
         # reader thread to be finished. Otherwise consumers would have to assume
         # that it still has not completely shutdown.
         return self.wait()
 
     def poll(self):
@@ -806,19 +806,19 @@ falling back to not using job objects fo
         - None if the process hasn't terminated yet
         - A negative number if the process was killed by signal N (Unix only)
         - '0' if the process ended without failures
 
         """
         # 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"):
             return self.proc.returncode
         else:
             return self.proc.poll()
 
     def processOutput(self, timeout=None, outputTimeout=None):
@@ -867,16 +867,19 @@ falling back to not using job objects fo
                 if timeout is not None and count > timeout:
                     return None
 
         self.returncode = self.proc.wait()
         return self.returncode
 
     @property
     def pid(self):
+        if not hasattr(self, "proc"):
+            raise RuntimeError("Process hasn't been started yet")
+
         return self.proc.pid
 
     @staticmethod
     def pid_exists(pid):
         if pid < 0:
             return False
 
         if isWin:
--- a/testing/mozbase/mozprocess/tests/manifest.ini
+++ b/testing/mozbase/mozprocess/tests/manifest.ini
@@ -1,10 +1,11 @@
 [DEFAULT]
 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]
 [test_params.py]
 [test_process_reader.py]
new file mode 100644
--- /dev/null
+++ b/testing/mozbase/mozprocess/tests/test_pid.py
@@ -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()