Bug 1433905 - [mozprocess] "check_for_detached()" has to raise RuntimeError if process hasn't been started yet. r=gbrown
authorHenrik Skupin <mail@hskupin.info>
Thu, 04 Oct 2018 10:49:24 +0000
changeset 495368 70607539582dc03ea44a0179aab0a1ab88785e98
parent 495367 4f05d89da460e4e8d626d530acd1df9ab4c56768
child 495369 a9f6938eccfeecfd92fce314b42a76ba11401f41
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] "check_for_detached()" has to raise RuntimeError if process hasn't been started yet. r=gbrown Calling "check_for_detached()" doesn't make sense if the process hasn't been started yet, and as such has to raise a RuntimeError. Depends on D7393 Differential Revision: https://phabricator.services.mozilla.com/D7394
testing/mozbase/mozprocess/mozprocess/processhandler.py
testing/mozbase/mozprocess/tests/manifest.ini
testing/mozbase/mozprocess/tests/test_detached.py
--- a/testing/mozbase/mozprocess/mozprocess/processhandler.py
+++ b/testing/mozbase/mozprocess/mozprocess/processhandler.py
@@ -922,18 +922,18 @@ falling back to not using job objects fo
 
         In case of application restarts the process can spawn itself into a new process group.
         From now on the process can no longer be tracked by mozprocess anymore and has to be
         marked as detached. If the consumer of mozprocess still knows the new process id it could
         check for the detached state.
 
         new_pid is the new process id of the child process.
         """
-        if not self.proc:
-            return
+        if not hasattr(self, "proc"):
+            raise RuntimeError("Process hasn't been started yet")
 
         if isPosix:
             new_pgid = self._getpgid(new_pid)
 
             if new_pgid and new_pgid != self.proc.pgid:
                 self.proc.detached_pid = new_pid
                 print('Child process with id "%s" has been marked as detached because it is no '
                       'longer in the managed process group. Keeping reference to the process id '
--- a/testing/mozbase/mozprocess/tests/manifest.ini
+++ b/testing/mozbase/mozprocess/tests/manifest.ini
@@ -1,11 +1,13 @@
 [DEFAULT]
 subsuite = mozbase, os == "linux"
 skip-if = python == 3
+[test_detached.py]
+skip-if = os == "win"  # Bug 1493796
 [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_detached.py
@@ -0,0 +1,67 @@
+#!/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 ProcTestDetached(proctest.ProcTest):
+    """Class to test for detached processes."""
+
+    def test_check_for_detached_before_run(self):
+        """Process is not started yet when checked for detached."""
+        p = processhandler.ProcessHandler([self.python, self.proclaunch,
+                                           "process_normal_finish.ini"],
+                                          cwd=here)
+
+        with self.assertRaises(RuntimeError):
+            p.check_for_detached(1234)
+
+    def test_check_for_detached_while_running_with_current_pid(self):
+        """Process is started, and check for detached with original pid."""
+        p = processhandler.ProcessHandler([self.python, self.proclaunch,
+                                           "process_normal_finish.ini"],
+                                          cwd=here)
+        p.run()
+
+        orig_pid = p.pid
+        p.check_for_detached(p.pid)
+
+        self.assertEqual(p.pid, orig_pid)
+        self.assertIsNone(p.proc.detached_pid)
+
+        self.determine_status(p, True)
+        p.kill()
+
+    def test_check_for_detached_after_fork(self):
+        """Process is started, and check for detached with new pid."""
+        pass
+
+    def test_check_for_detached_after_kill(self):
+        """Process is killed before checking for detached pid."""
+        p = processhandler.ProcessHandler([self.python, self.proclaunch,
+                                           "process_normal_finish.ini"],
+                                          cwd=here)
+        p.run()
+        p.kill()
+
+        orig_pid = p.pid
+        p.check_for_detached(p.pid)
+
+        self.assertEqual(p.pid, orig_pid)
+        self.assertIsNone(p.proc.detached_pid)
+
+        self.determine_status(p)
+
+
+if __name__ == '__main__':
+    mozunit.main()