Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/CacheManagement/src/tests/sharedclasses/RunCommand.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2010, 2019 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
package tests.sharedclasses;
24
25
import java.io.InputStream;
26
27
import java.util.*;
28
import java.io.*;
29
30
/**
31
* Instances of StreamGobbler are used to capture output from the spawned process
32
*/
33
class StreamGobbler extends Thread {
34
private InputStream is; // Can be used for stderr/stdout
35
private String type; // some identifier tag for this gobbler (possibly STDERR or STDOUT)
36
37
private List<String> expected = new ArrayList<String>(); // Expected lines of output to be found in the gobbled output
38
private String waitForMessage;
39
40
private StringBuffer gobbledData = new StringBuffer();
41
private List lines = new ArrayList();
42
public volatile boolean finished = false;
43
public volatile boolean waitForMessageFound = false;
44
public volatile boolean ignoreRest = false;
45
46
StreamGobbler(InputStream is, String type, String[] expected, String waitForMessage) {
47
this.is = is;
48
this.type= type;
49
this.waitForMessage = waitForMessage;
50
if (expected!=null) {
51
for (int i = 0; i < expected.length; i++) {
52
String string = expected[i];
53
if (string!=null)
54
this.expected.add(string);
55
}
56
}
57
}
58
59
public String getType() { return type; }
60
61
public void run() {
62
try {
63
InputStreamReader isr = new InputStreamReader(is);
64
BufferedReader br = new BufferedReader(isr);
65
String line=null;
66
while ( (line = br.readLine()) != null) {
67
//System.out.println(type + ">" + line);
68
gobbledData.append(/*type + ">" + */line+"\n");
69
lines.add(line);
70
int pos = 0;
71
int found =-1;
72
for (Iterator iterator = expected.iterator(); iterator.hasNext();) {
73
String name = (String) iterator.next();
74
if (line.indexOf(name) != -1) {
75
found = pos;
76
break;
77
}
78
pos++;
79
}
80
if (found != -1) {
81
expected.remove(found);
82
}
83
if ((false == waitForMessageFound) && (null != waitForMessage) && (line.indexOf(waitForMessage) != -1)) {
84
waitForMessageFound = true;
85
}
86
}
87
finished=true;
88
} catch (IOException ioe) {
89
if (ignoreRest == false) {
90
ioe.printStackTrace();
91
}
92
}
93
}
94
95
public boolean allExpectedMessagesWereFound() {
96
return expected.isEmpty();
97
}
98
99
public void dumpOutput() {
100
System.out.println(gobbledData.toString());
101
}
102
103
public String getOutput() {
104
return gobbledData.toString();
105
}
106
107
public String[] getOutputAsLines() {
108
return (String[])lines.toArray(new String[]{});
109
}
110
}
111
112
public class RunCommand {
113
private static Process lastProcess;
114
public static String lastCommand, the2ndLastCommand;
115
public static String lastCommandStdout, the2ndLastCommandStdout;
116
public static String[] lastCommandStdoutLines;
117
public static String lastCommandStderr, the2ndLastCommandStderr;
118
public static String[] lastCommandStderrLines;
119
public static boolean logCommands = false;
120
public static int processRunawayTimeout = 120000; // 2 min timer, kill process if its taking longer!
121
122
/* if the process executed last is still running, it returns the process object, else returns null */
123
public static Process getLastProcess() {
124
try {
125
if (null != lastProcess) {
126
lastProcess.exitValue();
127
}
128
/* process got terminated */
129
return null;
130
} catch (IllegalThreadStateException e) {
131
/* process is still running */
132
return lastProcess;
133
}
134
}
135
136
public static void executeMayFail(String cmd) {
137
execute(cmd,(String[])null,(String[])null,true,true, null);
138
}
139
140
public static void execute(String cmd) {
141
execute(cmd,(String[])null,(String[])null,true,false, null);
142
}
143
144
public static void execute(String cmd,boolean careAboutExitValue) {
145
execute(cmd,(String[])null,(String[])null,careAboutExitValue,false, null);
146
}
147
148
public static void execute(String cmd, String waitForMessage) {
149
execute(cmd,(String[])null, (String[])null, false, false, waitForMessage);
150
}
151
152
public static void execute(String cmd,String sysoutMessages,String syserrMessages,boolean careAboutExitValue) {
153
execute(cmd,new String[]{sysoutMessages},new String[]{syserrMessages},careAboutExitValue,false, null);
154
}
155
156
public static void execute(String cmd,String[] expectedSysoutMessages,String[] expectedSyserrMessages,boolean careAboutExitValue, boolean MayFail, String waitForMessage)
157
{
158
int exitVal = 0;
159
Runtime rt = Runtime.getRuntime();
160
the2ndLastCommand = lastCommand;
161
the2ndLastCommandStdout = lastCommandStdout;
162
the2ndLastCommandStderr = lastCommandStderr;
163
lastCommand = null;
164
lastCommandStdout = null;lastCommandStderr = null;
165
lastCommandStdoutLines = null;lastCommandStderrLines = null;
166
lastProcess = null;
167
168
try {
169
if (logCommands) {
170
System.out.println("Executing command: "+cmd);
171
}
172
long processStartTime = System.currentTimeMillis();
173
lastProcess = rt.exec(cmd);
174
// any error message?
175
StreamGobbler errorGobbler = new
176
StreamGobbler(lastProcess.getErrorStream(), "ERROR",expectedSyserrMessages, null);
177
178
// any output?
179
StreamGobbler outputGobbler = new
180
StreamGobbler(lastProcess.getInputStream(), "OUTPUT",expectedSysoutMessages, waitForMessage);
181
182
// kick them off
183
errorGobbler.start();
184
outputGobbler.start();
185
186
// any error???
187
if (null == waitForMessage) {
188
exitVal = lastProcess.waitFor();
189
while (!outputGobbler.finished || !errorGobbler.finished) {
190
try { Thread.sleep(250); } catch (Exception e) {}
191
if ((System.currentTimeMillis()-processStartTime)>processRunawayTimeout) {
192
// kill the process, it is taking too long!
193
lastProcess.destroy();
194
try {
195
lastProcess.waitFor();
196
} catch (java.lang.InterruptedException e) {
197
e.printStackTrace();
198
}
199
System.err.println("Process killed, was executing for longer than "+(processRunawayTimeout/1000)+"seconds");
200
}
201
}
202
203
lastCommandStdout = outputGobbler.getOutput();
204
lastCommandStderr = errorGobbler.getOutput();
205
lastCommandStdoutLines = outputGobbler.getOutputAsLines();
206
lastCommandStderrLines = errorGobbler.getOutputAsLines();
207
lastCommand = cmd;
208
if (!outputGobbler.allExpectedMessagesWereFound()) {
209
outputGobbler.dumpOutput();
210
errorGobbler.dumpOutput();
211
throw new RuntimeException("Not all expected messages were found");
212
}
213
if (!errorGobbler.allExpectedMessagesWereFound()) {
214
outputGobbler.dumpOutput();
215
errorGobbler.dumpOutput();
216
throw new RuntimeException("Not all expected messages were found");
217
}
218
//System.out.println("ExitValue: " + exitVal);
219
if (careAboutExitValue && exitVal!=0) {
220
if (MayFail==false) {
221
outputGobbler.dumpOutput();
222
errorGobbler.dumpOutput();
223
throw new RuntimeException("TestFailed: exitvalue="+exitVal);
224
}
225
}
226
} else {
227
while (false == outputGobbler.waitForMessageFound) {
228
try {
229
Thread.sleep(250);
230
} catch (Exception e) {
231
}
232
if ((System.currentTimeMillis() - processStartTime) > processRunawayTimeout) {
233
// kill the process, it is taking too long!
234
lastProcess.destroy();
235
try {
236
lastProcess.waitFor();
237
} catch (java.lang.InterruptedException e) {
238
e.printStackTrace();
239
}
240
System.err.println("Process killed, was taking longer than "+(processRunawayTimeout/1000)+" seconds to generate expected output");
241
}
242
}
243
outputGobbler.ignoreRest = true;
244
errorGobbler.ignoreRest = true;
245
}
246
} catch (IOException ioe) {
247
throw new RuntimeException("Problem invoking command: "+ioe.toString());
248
} catch (InterruptedException ie) {
249
throw new RuntimeException("Problem invoking command: "+ie.toString());
250
}
251
252
}
253
}
254