Bug 1433905 - [mozprocess] pid has to be None if no process exists yet. draft
authorHenrik Skupin <mail@hskupin.info>
Tue, 29 May 2018 15:48:21 +0200
changeset 801033 a4691a7badc2db905f443af6d8d72a796926ce1b
parent 801032 6fbb9b91e5456d7041392b527190c1e128a1f004
child 801034 43ead1bad121ad8482e84625ed803323c6ae5650
push id111546
push userbmo:hskupin@gmail.com
push dateTue, 29 May 2018 16:17:28 +0000
bugs1433905
milestone62.0a1
Bug 1433905 - [mozprocess] pid has to be None if no process exists yet. MozReview-Commit-ID: 9nXxygfCfpG
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
@@ -866,17 +866,20 @@ falling back to not using job objects fo
     # TODO Remove this method when consumers have been fixed
     def waitForFinish(self, timeout=None):
         print("MOZPROCESS WARNING: ProcessHandler.waitForFinish() is deprecated, "
               "use ProcessHandler.wait() instead", file=sys.stderr)
         return self.wait(timeout=timeout)
 
     @property
     def pid(self):
-        return self.proc.pid
+        if hasattr(self, "proc"):
+            return self.proc.pid
+        else:
+            return None
 
     @staticmethod
     def pid_exists(pid):
         if pid < 0:
             return False
 
         if isWin:
             try:
--- a/testing/mozbase/mozprocess/tests/manifest.ini
+++ b/testing/mozbase/mozprocess/tests/manifest.ini
@@ -2,11 +2,12 @@
 subsuite = mozbase, os == "linux"
 
 [test_detached.py]
 skip-if = os == "win"
 [test_kill.py]
 [test_misc.py]
 [test_output.py]
 [test_params.py]
+[test_pid.py]
 [test_poll.py]
 [test_process_reader.py]
 [test_wait.py]
new file mode 100644
--- /dev/null
+++ b/testing/mozbase/mozprocess/tests/test_pid.py
@@ -0,0 +1,53 @@
+#!/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 called"""
+
+        p = processhandler.ProcessHandler([self.python])
+        self.assertIsNone(p.pid)
+
+    def test_pid_while_running(self):
+        """Process is started, and pid is called"""
+
+        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 called"""
+
+        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()