1 | /*
|
---|
2 | * Copyright 2000-2004 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 |
|
---|
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 | /**
|
---|
27 | * Simple testcase for the ExecuteWatchdog class.
|
---|
28 | *
|
---|
29 | */
|
---|
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 | /**
|
---|
50 | * Dangerous method to obtain the classpath for the test. This is
|
---|
51 | * severely tighted to the build.xml properties.
|
---|
52 | */
|
---|
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 | // JDK 1.1 needs classes.zip in -classpath argument
|
---|
61 | 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 | //System.out.println("Testing with classpath: " + System.getProperty("java.class.path"));
|
---|
77 | 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 | // test that the watchdog ends the process
|
---|
111 | 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("return code is invalid: " + retCode, retCode!=0);
|
---|
119 | 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 | // test a process that runs and failed
|
---|
124 | public void testFailed() throws Exception {
|
---|
125 | Process process = getProcess(-1); // process should abort
|
---|
126 | 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 | // I assume that starting this takes less than TIME_OUT/2 ms...
|
---|
137 | Thread thread = new Thread(){
|
---|
138 | public void run(){
|
---|
139 | try {
|
---|
140 | process.waitFor();
|
---|
141 | } catch(InterruptedException e){
|
---|
142 | // not very nice but will do the job
|
---|
143 | fail("process interrupted in thread");
|
---|
144 | }
|
---|
145 | }
|
---|
146 | };
|
---|
147 | thread.start();
|
---|
148 |
|
---|
149 | // wait for TIME_OUT/2, there should be about TIME_OUT/2 ms remaining before timeout
|
---|
150 | thread.join(TIME_OUT/2);
|
---|
151 |
|
---|
152 | // now stop the watchdog.
|
---|
153 | watchdog.stop();
|
---|
154 |
|
---|
155 | // wait for the thread to die, should be the end of the process
|
---|
156 | thread.join();
|
---|
157 |
|
---|
158 | // process should be dead and well finished
|
---|
159 | assertEquals(0, process.exitValue());
|
---|
160 | assertTrue("process should not have been killed", !watchdog.killedProcess());
|
---|
161 | }
|
---|
162 | }
|
---|