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)
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)
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
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