Commit 62e7ac8e authored by Johannes Bechberger's avatar Johannes Bechberger

Fix #47

parent 3a7be959
import logging
import os
import random
import shlex
import shutil
import tempfile
from datetime import datetime
import time
from threading import Timer
from typing import Dict
import subprocess
from mjtest.util.shell import execute
from mjtest.util.utils import get_mjtest_basedir, force_colored_output
import mjtest.util.utils
......@@ -201,3 +205,29 @@ class Environment:
raise
return dst_file
def run_command_till_timeout(self, cmd: str, *args: Tuple[str], timeout: float = 0) -> Tuple[bytes, bytes, int, bool]:
"""
Much slower than `run_command` but captures the command output even when a
timeout occurs.
Inspired by http://stackoverflow.com/a/10768774
"""
cmds = [cmd] + list(args)
cmds = [shlex.quote(c) for c in cmds]
proc = subprocess.Popen(cmds, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
timeouted = False
def kill_proc(proc):
timeouted = True
proc.kill()
stdout, stderr = None, None
if timeout >= 0.0:
timer = Timer(timeout, kill_proc, [proc])
timer.start()
stdout, stderr = proc.communicate()
timer.cancel()
else:
stdout, stderr = proc.communicate()
return stdout, stderr, proc.returncode, timeouted
\ No newline at end of file
......@@ -206,6 +206,7 @@ class JavaInfiniteLoopTest(BasicSyntaxTest):
return test_result
except SigKill as sig:
if sig.retcode == signal.SIGXCPU:
out, _, _, _ = self.env.run_command_till_timeout("./" + base_filename, timeout=1)
test_result.add_long_text("Output", out.decode())
if self._has_output_file:
out = out.decode().strip()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment