@Test public void testIllegalTimeout() { try { int exitCode = ProcessHelper.runWithTimeout(-1, process); fail(); } catch (Exception e) { assertEquals("negative timeout: -1", e.getMessage()); } }
static int runWithTimeout(long timeoutInSeconds, Process process) throws InterruptedException, IOException { Preconditions.checkArgument(timeoutInSeconds > 0, "negative timeout: %s", timeoutInSeconds); int exitCode; long startTimeNano = System.nanoTime(); while (true) { try { exitCode = process.exitValue(); closeStream(process); return exitCode; } catch (IllegalThreadStateException ex) { long running = System.nanoTime() - startTimeNano; if (running > TimeUnit.SECONDS.toNanos(timeoutInSeconds)) { break; } TimeUnit.NANOSECONDS.sleep(smartWait(running, startTimeNano, timeoutInSeconds)); } } process.destroy(); exitCode = 124; closeStream(process); return exitCode; }
ProcessHelper { static int runWithTimeout(long timeoutInSeconds, Process process) throws InterruptedException, IOException { Preconditions.checkArgument(timeoutInSeconds > 0, "negative timeout: %s", timeoutInSeconds); int exitCode; long startTimeNano = System.nanoTime(); while (true) { try { exitCode = process.exitValue(); closeStream(process); return exitCode; } catch (IllegalThreadStateException ex) { long running = System.nanoTime() - startTimeNano; if (running > TimeUnit.SECONDS.toNanos(timeoutInSeconds)) { break; } TimeUnit.NANOSECONDS.sleep(smartWait(running, startTimeNano, timeoutInSeconds)); } } process.destroy(); exitCode = 124; closeStream(process); return exitCode; } }
ProcessHelper { static int runWithTimeout(long timeoutInSeconds, Process process) throws InterruptedException, IOException { Preconditions.checkArgument(timeoutInSeconds > 0, "negative timeout: %s", timeoutInSeconds); int exitCode; long startTimeNano = System.nanoTime(); while (true) { try { exitCode = process.exitValue(); closeStream(process); return exitCode; } catch (IllegalThreadStateException ex) { long running = System.nanoTime() - startTimeNano; if (running > TimeUnit.SECONDS.toNanos(timeoutInSeconds)) { break; } TimeUnit.NANOSECONDS.sleep(smartWait(running, startTimeNano, timeoutInSeconds)); } } process.destroy(); exitCode = 124; closeStream(process); return exitCode; } private  ProcessHelper(); }
ProcessHelper { static int runWithTimeout(long timeoutInSeconds, Process process) throws InterruptedException, IOException { Preconditions.checkArgument(timeoutInSeconds > 0, "negative timeout: %s", timeoutInSeconds); int exitCode; long startTimeNano = System.nanoTime(); while (true) { try { exitCode = process.exitValue(); closeStream(process); return exitCode; } catch (IllegalThreadStateException ex) { long running = System.nanoTime() - startTimeNano; if (running > TimeUnit.SECONDS.toNanos(timeoutInSeconds)) { break; } TimeUnit.NANOSECONDS.sleep(smartWait(running, startTimeNano, timeoutInSeconds)); } } process.destroy(); exitCode = 124; closeStream(process); return exitCode; } private  ProcessHelper();  }
ProcessHelper { static int runWithTimeout(long timeoutInSeconds, Process process) throws InterruptedException, IOException { Preconditions.checkArgument(timeoutInSeconds > 0, "negative timeout: %s", timeoutInSeconds); int exitCode; long startTimeNano = System.nanoTime(); while (true) { try { exitCode = process.exitValue(); closeStream(process); return exitCode; } catch (IllegalThreadStateException ex) { long running = System.nanoTime() - startTimeNano; if (running > TimeUnit.SECONDS.toNanos(timeoutInSeconds)) { break; } TimeUnit.NANOSECONDS.sleep(smartWait(running, startTimeNano, timeoutInSeconds)); } } process.destroy(); exitCode = 124; closeStream(process); return exitCode; } private  ProcessHelper();   }