forked from btraceio/btrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimeTest.java
More file actions
302 lines (267 loc) · 10.6 KB
/
RuntimeTest.java
File metadata and controls
302 lines (267 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package support;
import com.sun.btrace.BTraceFunctionalTests;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Assert;
/**
*
* @author Jaroslav Bachorik
*/
abstract public class RuntimeTest {
protected static interface ResultValidator {
void validate(String stdout, String stderr, int retcode);
}
private static String cp = null;
private static String java = null;
private static String btraceExtPath = null;
public static void setup() {
URL url = BTraceFunctionalTests.class.getClassLoader().getResource("com/sun/btrace/client/Main.class");
try {
File f = new File(url.toURI());
while (f != null) {
if (f.getName().equals("build")) {
break;
}
f = f.getParentFile();
}
if (f != null) {
btraceExtPath = f.getAbsolutePath() + "/btrace-client.jar";
}
Assert.assertNotNull(btraceExtPath);
} catch (URISyntaxException e) {
throw new Error(e);
}
String toolsjar = null;
cp = System.getProperty("java.class.path");
StringTokenizer st = new StringTokenizer(cp, File.pathSeparator);
while (st.hasMoreTokens()) {
String elem = st.nextToken();
if (elem.contains("tools.jar")) {
toolsjar = elem;
}
}
if (toolsjar == null) {
URL rturl = String.class.getResource("/java/lang/String.class");
toolsjar = rturl.toString().replace("jar:file:", "").replace("jre/lib/rt.jar", "lib/tools.jar");
toolsjar = toolsjar.substring(0, toolsjar.indexOf('!'));
System.err.println(toolsjar);
}
btraceExtPath = btraceExtPath + File.pathSeparator + toolsjar;
java = System.getProperty("java.home").replace("/jre", "");
}
/**
* Display the otput from the test application
*/
protected boolean debugTestApp = false;
/**
* Run BTrace in debug mode
*/
protected boolean debugBTrace = false;
/**
* Run BTrace in unsafe mode
*/
protected boolean isUnsafe = false;
/**
* Timeout in ms to wait for the expected BTrace output
*/
protected long timeout = 10000L;
/**
* Track retransforming progress
*/
protected boolean trackRetransforms = false;
protected boolean attachDebugger = false;
protected void reset() {
debugTestApp = false;
debugBTrace = false;
isUnsafe = false;
timeout = 10000L;
}
@SuppressWarnings("DefaultCharset")
public void test(String testApp, final String testScript, int checkLines, ResultValidator v) throws Exception {
test(testApp, testScript, null, checkLines, v);
}
@SuppressWarnings("DefaultCharset")
public void test(String testApp, final String testScript, String[] cmdArgs, int checkLines, ResultValidator v) throws Exception {
List<String> args = new ArrayList<>(Arrays.asList(
java + "/bin/java",
"-cp",
cp
));
if (attachDebugger) {
args.add("-agentlib:jdwp=transport=dt_socket,server=y,address=8000");
}
args.add(testApp);
ProcessBuilder pb = new ProcessBuilder(args);
pb.environment().remove("JAVA_TOOL_OPTIONS");
Process p = pb.start();
final PrintWriter pw = new PrintWriter(p.getOutputStream());
final StringBuilder stdout = new StringBuilder();
final StringBuilder stderr = new StringBuilder();
final AtomicInteger ret = new AtomicInteger(-1);
final BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
final CountDownLatch testAppLatch = new CountDownLatch(1);
final AtomicReference<String> pidStringRef = new AtomicReference<>();
Thread outT = new Thread(new Runnable() {
@Override
public void run() {
try {
String l;
while ((l = stdoutReader.readLine()) != null) {
if (l.startsWith("ready:")) {
pidStringRef.set(l.split("\\:")[1]);
testAppLatch.countDown();
}
if (debugTestApp) {
System.out.println("[traced app] " + l);
}
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}, "STDOUT Reader");
outT.setDaemon(true);
final BufferedReader stderrReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
Thread errT = new Thread(new Runnable() {
@Override
public void run() {
try {
String l = null;
while ((l = stderrReader.readLine()) != null) {
testAppLatch.countDown();
if (debugTestApp) {
System.err.println("[traced app] " + l);
}
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}, "STDERR Reader");
errT.setDaemon(true);
outT.start();
errT.start();
testAppLatch.await();
String pid = pidStringRef.get();
if (pid != null) {
System.out.println("Target process ready: " + pid);
Process client = attach(pid, testScript, cmdArgs, checkLines, stdout, stderr);
System.out.println("Detached.");
pw.println("done");
pw.flush();
ret.set(client.waitFor());
outT.join();
errT.join();
}
v.validate(stdout.toString(), stderr.toString(), ret.get());
}
private Process attach(String pid, String trace, String[] cmdArgs, final int checkLines, final StringBuilder stdout, final StringBuilder stderr) throws Exception {
URL u = ClassLoader.getSystemResource(trace);
System.out.println(trace);
File traceFile = new File(u.toURI());
trace = traceFile.getAbsolutePath();
List<String> argVals = new ArrayList<>(Arrays.asList(
java + "/bin/java",
"-Dcom.sun.btrace.unsafe=" + isUnsafe,
"-Dcom.sun.btrace.debug=" + debugBTrace,
"-Dcom.sun.btrace.trackRetransforms=" + trackRetransforms,
"-cp",
btraceExtPath,
"com.sun.btrace.client.Main",
"-d", "/tmp/btrace-test",
"-pd", traceFile.getParentFile().getAbsolutePath(),
pid,
trace
));
if (cmdArgs != null) {
argVals.addAll(Arrays.asList(cmdArgs));
}
final ProcessBuilder pb = new ProcessBuilder(argVals);
pb.environment().remove("JAVA_TOOL_OPTIONS");
final Process p = pb.start();
final CountDownLatch l = new CountDownLatch(checkLines);
new Thread(new Runnable() {
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream(), StandardCharsets.UTF_8));
String line = null;
while ((line = br.readLine()) != null) {
stderr.append(line).append('\n');
System.out.println("[btrace err] " + line);
if (line.contains("Exception") || line.contains("Error")) {
for(int i=0;i<checkLines;i++) {
l.countDown();
}
}
}
} catch (Exception e) {
for(int i=0;i<checkLines;i++) {
l.countDown();
}
throw new Error(e);
}
}
}, "Stderr Reader").start();
new Thread(new Runnable() {
@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8));
String line = null;
while ((line = br.readLine()) != null) {
stdout.append(line).append('\n');
System.out.println("[btrace out] " + line);
if (!(debugBTrace && line.contains("DEBUG:"))) {
l.countDown();
}
}
} catch (Exception e) {
for(int i=0;i<checkLines;i++) {
l.countDown();
}
throw new Error(e);
}
}
}, "Stdout Reader").start();
l.await(timeout, TimeUnit.MILLISECONDS);
return p;
}
}