1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 4 */ 5 package jdk.testlibrary; 6 7 import static jdk.testlibrary.Asserts.assertNotEquals; 8 import static jdk.testlibrary.Asserts.assertTrue; 9 10 import java.util.List; 11 import java.util.concurrent.CountDownLatch; 12 13 public class ProcessThread extends TestThread { 14 15 public ProcessThread(String threadName, List<String> cmd) { 16 super(new ProcessRunnable(cmd), threadName); 17 } 18 19 @Override 20 public void joinAndThrow() throws InterruptedException, Throwable { 21 ((ProcessRunnable) getRunnable()).stopProcess(); 22 super.joinAndThrow(); 23 } 24 25 public void joinAndWait() throws InterruptedException { 26 ((ProcessRunnable) getRunnable()).stopProcess(); 27 super.join(); 28 Thread.sleep(100); 29 } 30 31 static class ProcessRunnable extends XRun { 32 33 private final ProcessBuilder processBuilder; 34 private final CountDownLatch latch; 35 private volatile Process toolProcess; 36 37 public ProcessRunnable(List<String> cmd) { 38 super(); 39 this.processBuilder = new ProcessBuilder(cmd); 40 this.latch = new CountDownLatch(1); 41 } 42 43 @Override 44 public void xrun() throws Throwable { 45 this.toolProcess = processBuilder.start(); 46 // Release when process is started 47 latch.countDown(); 48 49 // Will block... 50 OutputAnalyzer output = new OutputAnalyzer(this.toolProcess); 51 52 assertTrue(output.getOutput().isEmpty(), "Should get an empty output, got: " 53 + Utils.NEW_LINE + output.getOutput()); 54 assertNotEquals(output.getExitValue(), 0, 55 "Process exited with unexpected exit code"); 56 } 57 58 public void stopProcess() throws InterruptedException { 59 // Wait until process is started 60 latch.await(); 61 if (this.toolProcess != null) { 62 this.toolProcess.destroy(); 63 } 64 } 65 66 } 67 68 }