Ejemplo n.º 1
0
    def test_subprocess_timeout(self):
        p = subprocess.Popen([sys.executable,
                              "-c",
                              "import time; time.sleep(0.5);"],
                             stderr=subprocess.PIPE)
        with subprocess_timeout(p, 0.2) as timedout:
            retval = p.wait()
            p.stderr.close()
            self.assertEqual(timedout.value, True)
        self.assertNotEqual(retval, 0)

        p = create_process_group([sys.executable,
                                  "-c",
                                  process_group_timeout_test_code])
        with subprocess_timeout(p, 0.5, kill_pg=True):
            retval = p.wait()
            self.assertEqual(timedout.value, True)
        self.assertNotEqual(retval, 0)

        p = subprocess.Popen([sys.executable,
                              "-c",
                              "import time"])
        with subprocess_timeout(p, 0.5) as timedout:
            retval = p.wait()
            self.assertEqual(timedout.value, False)
        self.assertEqual(retval, 0)

        p = subprocess.Popen([sys.executable,
                              "-c",
                              "import time"])
        with subprocess_timeout(p, 0) as timedout:
            retval = p.wait()
            self.assertEqual(timedout.value, False)
        self.assertEqual(retval, 0)
Ejemplo n.º 2
0
def execute_command_array(command_array, timeout, verbose):
    """
    Executes the given command array in a subprocess group.

    :param command_array: The command array to execute.
    :param timeout:       Time to wait until killing the process.
    :param verbose:       Return the stdout and stderr of the subprocess or
                          not.
    :return:              A tuple of (result, message) where message gives
                          text information of what happened.
    """
    message = ""
    stdout_file = tempfile.TemporaryFile()
    p = create_process_group(command_array, stdout=stdout_file, stderr=subprocess.STDOUT, universal_newlines=True)
    with subprocess_timeout(p, timeout, kill_pg=True) as timedout:
        retval = p.wait()
        timed_out = timedout.value

    if retval != 0 or verbose:
        stdout_file.seek(0)
        message += stdout_file.read().decode(sys.stdout.encoding, errors="replace")

    stdout_file.close()

    if timed_out:
        message += (
            "This test failed because it was taking more than %f sec "
            "to execute. To change the timeout setting use the `-T` "
            "or `--timeout` argument.\n" % timeout
        )
        return 1, message  # Guaranteed fail, especially on race condition

    return retval, message
Ejemplo n.º 3
0
    def test_subprocess_timeout(self):
        p = subprocess.Popen([sys.executable,
                              "-c",
                              "import time; time.sleep(0.5);"],
                             stderr=subprocess.PIPE)
        with subprocess_timeout(p, 0.2) as timedout:
            retval = p.wait()
            p.stderr.close()
            self.assertEqual(timedout.value, True)
        self.assertNotEqual(retval, 0)

        p = create_process_group([sys.executable,
                                  "-c",
                                  process_group_timeout_test_code])
        with subprocess_timeout(p, 0.5, kill_pg=True):
            retval = p.wait()
            self.assertEqual(timedout.value, True)
        self.assertNotEqual(retval, 0)

        p = subprocess.Popen([sys.executable,
                              "-c",
                              "import time"])
        with subprocess_timeout(p, 0.5) as timedout:
            retval = p.wait()
            self.assertEqual(timedout.value, False)
        self.assertEqual(retval, 0)

        p = subprocess.Popen([sys.executable,
                              "-c",
                              "import time"])
        with subprocess_timeout(p, 0) as timedout:
            retval = p.wait()
            self.assertEqual(timedout.value, False)
        self.assertEqual(retval, 0)
Ejemplo n.º 4
0
def execute_command_array(command_array, timeout, verbose):
    """
    Executes the given command array in a subprocess group.

    :param command_array: The command array to execute.
    :param timeout:       Time to wait until killing the process.
    :param verbose:       Return the stdout and stderr of the subprocess or
                          not.
    :return:              A tuple of (result, message) where message gives
                          text information of what happened.
    """
    message = ""
    stdout_file = tempfile.TemporaryFile()
    p = create_process_group(command_array,
                             stdout=stdout_file,
                             stderr=subprocess.STDOUT,
                             universal_newlines=True)
    with subprocess_timeout(p, timeout, kill_pg=True) as timedout:
        retval = p.wait()
        timed_out = timedout.value

    if retval != 0 or verbose:
        stdout_file.seek(0)
        message += stdout_file.read().decode(sys.stdout.encoding,
                                             errors="replace")

    stdout_file.close()

    if timed_out:
        message += ("This test failed because it was taking more than %f sec "
                    "to execute. To change the timeout setting use the `-T` "
                    "or `--timeout` argument.\n" % timeout)
        return 1, message  # Guaranteed fail, especially on race condition

    return retval, message
Ejemplo n.º 5
0
 def test_create_process_group(self):
     p = create_process_group(
         [sys.executable, "-c", process_group_test_code], stdout=subprocess.PIPE, stderr=subprocess.PIPE
     )
     retval = p.wait()
     if retval != 0:
         for line in p.stderr:
             print(line, end="")
         raise Exception("Subprocess did not exit correctly")
     output = [i for i in p.stdout]
     p.stderr.close()
     p.stdout.close()
     pid, pgid = [int(i.strip()) for i_out in output for i in i_out.split()]
     if platform.system() != "Windows":
         # There is no way of testing this on windows with the current
         # python modules subprocess and os
         self.assertEqual(p.pid, pgid)
Ejemplo n.º 6
0
 def test_create_process_group(self):
     p = create_process_group(
         [sys.executable, '-c', process_group_test_code],
         stdout=subprocess.PIPE,
         stderr=subprocess.PIPE)
     retval = p.wait()
     if retval != 0:
         for line in p.stderr:
             print(line, end='')
         raise Exception('Subprocess did not exit correctly')
     output = [i for i in p.stdout]
     p.stderr.close()
     p.stdout.close()
     pid, pgid = [int(i.strip()) for i_out in output for i in i_out.split()]
     if platform.system() != 'Windows':
         # There is no way of testing this on windows with the current
         # python modules subprocess and os
         self.assertEqual(p.pid, pgid)