1 17 18 package org.apache.tools.ant.taskdefs; 19 20 import org.apache.tools.ant.util.JavaEnvUtils; 21 22 import java.net.*; 23 import junit.framework.*; 24 import java.io.*; 25 26 30 public class ExecuteWatchdogTest extends TestCase { 31 32 private final static int TIME_OUT = 5000; 33 34 private final static String TEST_CLASSPATH = getTestClassPath(); 35 36 private final static int CLOCK_ERROR=200; 37 private final static int TIME_OUT_TEST=TIME_OUT-CLOCK_ERROR; 38 39 private ExecuteWatchdog watchdog; 40 41 public ExecuteWatchdogTest(String name) { 42 super(name); 43 } 44 45 protected void setUp(){ 46 watchdog = new ExecuteWatchdog(TIME_OUT); 47 } 48 49 53 private static String getTestClassPath(){ 54 String classpath = System.getProperty("build.tests"); 55 if (classpath == null) { 56 System.err.println("WARNING: 'build.tests' property is not available !"); 57 classpath = System.getProperty("java.class.path"); 58 } 59 60 if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) { 62 classpath += File.pathSeparator 63 + System.getProperty("java.home") 64 + File.separator + "lib" 65 + File.separator + "classes.zip"; 66 } 67 68 return classpath; 69 } 70 71 private Process getProcess(int timetorun) throws Exception { 72 String [] cmdArray = { 73 JavaEnvUtils.getJreExecutable("java"), "-classpath", TEST_CLASSPATH, 74 TimeProcess.class.getName(), String.valueOf(timetorun) 75 }; 76 return Runtime.getRuntime().exec(cmdArray); 78 } 79 80 private String getErrorOutput(Process p) throws Exception { 81 BufferedReader err = new BufferedReader( new InputStreamReader(p.getErrorStream()) ); 82 StringBuffer buf = new StringBuffer (); 83 String line; 84 while ( (line = err.readLine()) != null){ 85 buf.append(line); 86 } 87 return buf.toString(); 88 } 89 90 private int waitForEnd(Process p) throws Exception { 91 int retcode = p.waitFor(); 92 if (retcode != 0){ 93 String err = getErrorOutput(p); 94 if (err.length() > 0){ 95 System.err.println("ERROR:"); 96 System.err.println(err); 97 } 98 } 99 return retcode; 100 } 101 102 public void testNoTimeOut() throws Exception { 103 Process process = getProcess(TIME_OUT/2); 104 watchdog.start(process); 105 int retCode = waitForEnd(process); 106 assertTrue("process should not have been killed", !watchdog.killedProcess()); 107 assertEquals(0, retCode); 108 } 109 110 public void testTimeOut() throws Exception { 112 Process process = getProcess(TIME_OUT*2); 113 long now = System.currentTimeMillis(); 114 watchdog.start(process); 115 int retCode = process.waitFor(); 116 long elapsed = System.currentTimeMillis() - now; 117 assertTrue("process should have been killed", watchdog.killedProcess()); 118 assertTrue("elapse time of "+elapsed+" ms is less than timeout value of "+TIME_OUT_TEST+" ms", elapsed >= TIME_OUT_TEST); 120 assertTrue("elapse time of "+elapsed+" ms is greater than run value of "+(TIME_OUT*2)+" ms", elapsed < TIME_OUT*2); 121 } 122 123 public void testFailed() throws Exception { 125 Process process = getProcess(-1); watchdog.start(process); 127 int retCode = process.waitFor(); 128 assertTrue("process should not have been killed", !watchdog.killedProcess()); 129 assertTrue("return code is invalid: " + retCode, retCode!=0); 130 } 131 132 public void testManualStop() throws Exception { 133 final Process process = getProcess(TIME_OUT*2); 134 watchdog.start(process); 135 136 Thread thread = new Thread (){ 138 public void run(){ 139 try { 140 process.waitFor(); 141 } catch(InterruptedException e){ 142 fail("process interrupted in thread"); 144 } 145 } 146 }; 147 thread.start(); 148 149 thread.join(TIME_OUT/2); 151 152 watchdog.stop(); 154 155 thread.join(); 157 158 assertEquals(0, process.exitValue()); 160 assertTrue("process should not have been killed", !watchdog.killedProcess()); 161 } 162 } 163 | Popular Tags |