testing/mozbase/mozprocess/tests/test_mozprocess_kill.py
author Francesco Pischedda <francesco.pischedda@gmail.com>
Fri, 30 Sep 2016 16:08:37 +0200
changeset 362898 f7b6a16eb1f610c2a0291ebcc8474c72b4e993ca
parent 304074 72bf30ffd77166d0813caf50218e0f31bedbe219
child 370911 55405892e588fa851979d8ea31a656853666b40f
permissions -rw-r--r--
Bug 1280573 - Add testing/mozbase to flake8 linter: r=ahal added testing/mozbase to tools/lint/flake8.lint fixed a first batch of PEP8 errors/warnings at first the commad autopep8 -i --max-line-length 99 -r -j 8 . has been used to fix simpler problems, run from testing/mozbase some of the issues can not easily fixed : - undefined 'names' in code for example isLinux - isLinux and isBsd "fixed" with # noqa - undefined 'message' resolved with return fmt.format(... - undefined 'structured' resolved replacing those with mozlog - long comments - some remaining - addressed with # noqa - package level import everything - addressed with # flake8: noqa restored testing/mozbase/mozdevice/mozdevice/Zeroconf.py fixed issues reported on mozreview fixed ')' in testing/mozbase/mozprocess/mozprocess/qijo.py imports finally fixed multiline string at testing/mozbase/manifestparser/tests/test_manifestparser.py:114 ^^^ and again, but now with ./mach python-test --path-only testing/mozbase/manifestparser/tests/test_manifestparser.py passing fixed testing/mozbase/manifestparser/tests/test_convert_directory.py assert fixed this error: 10:15:21 INFO - return lambda line: stack_fixer_module.fixSymbols(line) 10:15:21 INFO - TypeError: fixSymbols() takes exactly 2 arguments (1 given) fixed two spaces lint error even of # noqa comments restored assignement to lambda with # noqa to silence the lint error global noqa for testing/mozbase/manifestparser/tests/test_filters.py stupid is/is not error... MozReview-Commit-ID: 1FpJF54GqIi

#!/usr/bin/env python

import os
import time
import unittest
import proctest
import signal
from mozprocess import processhandler

here = os.path.dirname(os.path.abspath(__file__))


class ProcTestKill(proctest.ProcTest):
    """ Class to test various process tree killing scenatios """

    def test_kill_before_run(self):
        """Process is not started, and kill() is called"""

        p = processhandler.ProcessHandler([self.python, '-V'])
        self.assertRaises(RuntimeError, p.kill)

    def test_process_kill(self):
        """Process is started, we kill it"""

        p = processhandler.ProcessHandler([self.python, self.proclaunch,
                                           "process_normal_finish_python.ini"],
                                          cwd=here)
        p.run()
        p.kill()

        self.determine_status(p, expectedfail=('returncode',))

    def test_process_kill_deep(self):
        """Process is started, we kill it, we use a deep process tree"""

        p = processhandler.ProcessHandler([self.python, self.proclaunch,
                                           "process_normal_deep_python.ini"],
                                          cwd=here)
        p.run()
        p.kill()

        self.determine_status(p, expectedfail=('returncode',))

    def test_process_kill_deep_wait(self):
        """Process is started, we use a deep process tree, we let it spawn
           for a bit, we kill it"""

        p = processhandler.ProcessHandler([self.python, self.proclaunch,
                                           "process_normal_deep_python.ini"],
                                          cwd=here)
        p.run()
        # Let the tree spawn a bit, before attempting to kill
        time.sleep(3)
        p.kill()

        self.determine_status(p, expectedfail=('returncode',))

    def test_process_kill_broad(self):
        """Process is started, we kill it, we use a broad process tree"""

        p = processhandler.ProcessHandler([self.python, self.proclaunch,
                                           "process_normal_broad_python.ini"],
                                          cwd=here)
        p.run()
        p.kill()

        self.determine_status(p, expectedfail=('returncode',))

    @unittest.skipUnless(processhandler.isPosix, "posix only")
    def test_process_kill_with_sigterm(self):
        script = os.path.join(here, 'infinite_loop.py')
        p = processhandler.ProcessHandler([self.python, script])

        p.run()
        p.kill()

        self.assertEquals(p.proc.returncode, -signal.SIGTERM)

    @unittest.skipUnless(processhandler.isPosix, "posix only")
    def test_process_kill_with_sigint_if_needed(self):
        script = os.path.join(here, 'infinite_loop.py')
        p = processhandler.ProcessHandler([self.python, script, 'deadlock'])

        p.run()
        time.sleep(1)
        p.kill()

        self.assertEquals(p.proc.returncode, -signal.SIGKILL)

if __name__ == '__main__':
    unittest.main()