You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExecuteWatchdogTest.java 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright 2000-2005 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs;
  18. import org.apache.tools.ant.util.JavaEnvUtils;
  19. import junit.framework.*;
  20. import java.io.*;
  21. /**
  22. * Simple testcase for the ExecuteWatchdog class.
  23. *
  24. */
  25. public class ExecuteWatchdogTest extends TestCase {
  26. private final static int TIME_OUT = 5000;
  27. private final static String TEST_CLASSPATH = getTestClassPath();
  28. private final static int CLOCK_ERROR=200;
  29. private final static int TIME_OUT_TEST=TIME_OUT-CLOCK_ERROR;
  30. private ExecuteWatchdog watchdog;
  31. public ExecuteWatchdogTest(String name) {
  32. super(name);
  33. }
  34. protected void setUp(){
  35. watchdog = new ExecuteWatchdog(TIME_OUT);
  36. }
  37. /**
  38. * Dangerous method to obtain the classpath for the test. This is
  39. * severely tighted to the build.xml properties.
  40. */
  41. private static String getTestClassPath(){
  42. String classpath = System.getProperty("build.tests");
  43. if (classpath == null) {
  44. System.err.println("WARNING: 'build.tests' property is not available !");
  45. classpath = System.getProperty("java.class.path");
  46. }
  47. return classpath;
  48. }
  49. private Process getProcess(int timetorun) throws Exception {
  50. String[] cmdArray = {
  51. JavaEnvUtils.getJreExecutable("java"), "-classpath", TEST_CLASSPATH,
  52. TimeProcess.class.getName(), String.valueOf(timetorun)
  53. };
  54. //System.out.println("Testing with classpath: " + System.getProperty("java.class.path"));
  55. return Runtime.getRuntime().exec(cmdArray);
  56. }
  57. private String getErrorOutput(Process p) throws Exception {
  58. BufferedReader err = new BufferedReader( new InputStreamReader(p.getErrorStream()) );
  59. StringBuffer buf = new StringBuffer();
  60. String line;
  61. while ( (line = err.readLine()) != null){
  62. buf.append(line);
  63. }
  64. return buf.toString();
  65. }
  66. private int waitForEnd(Process p) throws Exception {
  67. int retcode = p.waitFor();
  68. if (retcode != 0){
  69. String err = getErrorOutput(p);
  70. if (err.length() > 0){
  71. System.err.println("ERROR:");
  72. System.err.println(err);
  73. }
  74. }
  75. return retcode;
  76. }
  77. public void testNoTimeOut() throws Exception {
  78. Process process = getProcess(TIME_OUT/2);
  79. watchdog.start(process);
  80. int retCode = waitForEnd(process);
  81. assertTrue("process should not have been killed", !watchdog.killedProcess());
  82. assertEquals(0, retCode);
  83. }
  84. // test that the watchdog ends the process
  85. public void testTimeOut() throws Exception {
  86. Process process = getProcess(TIME_OUT*2);
  87. long now = System.currentTimeMillis();
  88. watchdog.start(process);
  89. int retCode = process.waitFor();
  90. long elapsed = System.currentTimeMillis() - now;
  91. assertTrue("process should have been killed", watchdog.killedProcess());
  92. // assertTrue("return code is invalid: " + retCode, retCode!=0);
  93. assertTrue("elapse time of "+elapsed+" ms is less than timeout value of "+TIME_OUT_TEST+" ms", elapsed >= TIME_OUT_TEST);
  94. assertTrue("elapse time of "+elapsed+" ms is greater than run value of "+(TIME_OUT*2)+" ms", elapsed < TIME_OUT*2);
  95. }
  96. // test a process that runs and failed
  97. public void testFailed() throws Exception {
  98. Process process = getProcess(-1); // process should abort
  99. watchdog.start(process);
  100. int retCode = process.waitFor();
  101. assertTrue("process should not have been killed", !watchdog.killedProcess());
  102. assertTrue("return code is invalid: " + retCode, retCode!=0);
  103. }
  104. public void testManualStop() throws Exception {
  105. final Process process = getProcess(TIME_OUT*2);
  106. watchdog.start(process);
  107. // I assume that starting this takes less than TIME_OUT/2 ms...
  108. Thread thread = new Thread(){
  109. public void run(){
  110. try {
  111. process.waitFor();
  112. } catch(InterruptedException e){
  113. // not very nice but will do the job
  114. fail("process interrupted in thread");
  115. }
  116. }
  117. };
  118. thread.start();
  119. // wait for TIME_OUT/2, there should be about TIME_OUT/2 ms remaining before timeout
  120. thread.join(TIME_OUT/2);
  121. // now stop the watchdog.
  122. watchdog.stop();
  123. // wait for the thread to die, should be the end of the process
  124. thread.join();
  125. // process should be dead and well finished
  126. assertEquals(0, process.exitValue());
  127. assertTrue("process should not have been killed", !watchdog.killedProcess());
  128. }
  129. }