43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.test;
|
|
26 |
|
46640
|
27 |
import static org.graalvm.compiler.debug.DebugContext.DEFAULT_LOG_STREAM;
|
|
28 |
import static org.graalvm.compiler.debug.DebugContext.NO_DESCRIPTION;
|
|
29 |
|
57537
|
30 |
import java.io.IOException;
|
43972
|
31 |
import java.io.PrintStream;
|
|
32 |
import java.io.PrintWriter;
|
|
33 |
import java.lang.reflect.Field;
|
|
34 |
import java.lang.reflect.Method;
|
57537
|
35 |
import java.nio.file.FileVisitResult;
|
|
36 |
import java.nio.file.Files;
|
|
37 |
import java.nio.file.Path;
|
58299
|
38 |
import java.nio.file.Paths;
|
57537
|
39 |
import java.nio.file.SimpleFileVisitor;
|
|
40 |
import java.nio.file.attribute.BasicFileAttributes;
|
58299
|
41 |
import java.nio.file.attribute.FileAttribute;
|
46640
|
42 |
import java.util.ArrayList;
|
43972
|
43 |
import java.util.Arrays;
|
46640
|
44 |
import java.util.Collection;
|
|
45 |
import java.util.Collections;
|
52578
|
46 |
import java.util.List;
|
51436
|
47 |
import java.util.concurrent.TimeUnit;
|
43972
|
48 |
|
46640
|
49 |
import org.graalvm.compiler.debug.DebugContext;
|
|
50 |
import org.graalvm.compiler.debug.DebugDumpHandler;
|
48861
|
51 |
import org.graalvm.compiler.debug.DebugHandlersFactory;
|
|
52 |
import org.graalvm.compiler.debug.GlobalMetrics;
|
46640
|
53 |
import org.graalvm.compiler.options.OptionValues;
|
49873
|
54 |
import org.graalvm.compiler.serviceprovider.GraalServices;
|
46640
|
55 |
import org.junit.After;
|
43972
|
56 |
import org.junit.Assert;
|
52578
|
57 |
import org.junit.AssumptionViolatedException;
|
43972
|
58 |
import org.junit.internal.ComparisonCriteria;
|
|
59 |
import org.junit.internal.ExactComparisonCriteria;
|
51436
|
60 |
import org.junit.rules.DisableOnDebug;
|
|
61 |
import org.junit.rules.TestRule;
|
|
62 |
import org.junit.rules.Timeout;
|
43972
|
63 |
|
48861
|
64 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
43972
|
65 |
import sun.misc.Unsafe;
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Base class that contains common utility methods and classes useful in unit tests.
|
|
69 |
*/
|
|
70 |
public class GraalTest {
|
|
71 |
|
|
72 |
public static final Unsafe UNSAFE;
|
|
73 |
static {
|
|
74 |
try {
|
|
75 |
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
|
|
76 |
theUnsafe.setAccessible(true);
|
|
77 |
UNSAFE = (Unsafe) theUnsafe.get(Unsafe.class);
|
|
78 |
} catch (Exception e) {
|
|
79 |
throw new RuntimeException("exception while trying to get Unsafe", e);
|
|
80 |
}
|
|
81 |
}
|
|
82 |
|
|
83 |
protected Method getMethod(String methodName) {
|
|
84 |
return getMethod(getClass(), methodName);
|
|
85 |
}
|
|
86 |
|
|
87 |
protected Method getMethod(Class<?> clazz, String methodName) {
|
|
88 |
Method found = null;
|
|
89 |
for (Method m : clazz.getMethods()) {
|
|
90 |
if (m.getName().equals(methodName)) {
|
|
91 |
Assert.assertNull(found);
|
|
92 |
found = m;
|
|
93 |
}
|
|
94 |
}
|
|
95 |
if (found == null) {
|
|
96 |
/* Now look for non-public methods (but this does not look in superclasses). */
|
|
97 |
for (Method m : clazz.getDeclaredMethods()) {
|
|
98 |
if (m.getName().equals(methodName)) {
|
|
99 |
Assert.assertNull(found);
|
|
100 |
found = m;
|
|
101 |
}
|
|
102 |
}
|
|
103 |
}
|
|
104 |
if (found != null) {
|
|
105 |
return found;
|
|
106 |
} else {
|
|
107 |
throw new RuntimeException("method not found: " + methodName);
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
46459
|
111 |
protected Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
|
43972
|
112 |
try {
|
|
113 |
return clazz.getMethod(methodName, parameterTypes);
|
|
114 |
} catch (NoSuchMethodException | SecurityException e) {
|
|
115 |
throw new RuntimeException("method not found: " + methodName + "" + Arrays.toString(parameterTypes));
|
|
116 |
}
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
* Compares two given objects for {@linkplain Assert#assertEquals(Object, Object) equality}.
|
|
121 |
* Does a deep copy equality comparison if {@code expected} is an array.
|
|
122 |
*/
|
|
123 |
protected void assertDeepEquals(Object expected, Object actual) {
|
|
124 |
assertDeepEquals(null, expected, actual);
|
|
125 |
}
|
|
126 |
|
|
127 |
/**
|
|
128 |
* Compares two given objects for {@linkplain Assert#assertEquals(Object, Object) equality}.
|
|
129 |
* Does a deep copy equality comparison if {@code expected} is an array.
|
|
130 |
*
|
|
131 |
* @param message the identifying message for the {@link AssertionError}
|
|
132 |
*/
|
|
133 |
protected void assertDeepEquals(String message, Object expected, Object actual) {
|
|
134 |
if (ulpsDelta() > 0) {
|
|
135 |
assertDeepEquals(message, expected, actual, ulpsDelta());
|
|
136 |
} else {
|
|
137 |
assertDeepEquals(message, expected, actual, equalFloatsOrDoublesDelta());
|
|
138 |
}
|
|
139 |
}
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Compares two given values for equality, doing a recursive test if both values are arrays of
|
|
143 |
* the same type.
|
|
144 |
*
|
|
145 |
* @param message the identifying message for the {@link AssertionError}
|
|
146 |
* @param delta the maximum delta between two doubles or floats for which both numbers are still
|
|
147 |
* considered equal.
|
|
148 |
*/
|
|
149 |
protected void assertDeepEquals(String message, Object expected, Object actual, double delta) {
|
|
150 |
if (expected != null && actual != null) {
|
|
151 |
Class<?> expectedClass = expected.getClass();
|
|
152 |
Class<?> actualClass = actual.getClass();
|
|
153 |
if (expectedClass.isArray()) {
|
|
154 |
Assert.assertEquals(message, expectedClass, actual.getClass());
|
|
155 |
if (expected instanceof int[]) {
|
|
156 |
Assert.assertArrayEquals(message, (int[]) expected, (int[]) actual);
|
|
157 |
} else if (expected instanceof byte[]) {
|
|
158 |
Assert.assertArrayEquals(message, (byte[]) expected, (byte[]) actual);
|
|
159 |
} else if (expected instanceof char[]) {
|
|
160 |
Assert.assertArrayEquals(message, (char[]) expected, (char[]) actual);
|
|
161 |
} else if (expected instanceof short[]) {
|
|
162 |
Assert.assertArrayEquals(message, (short[]) expected, (short[]) actual);
|
|
163 |
} else if (expected instanceof float[]) {
|
|
164 |
Assert.assertArrayEquals(message, (float[]) expected, (float[]) actual, (float) delta);
|
|
165 |
} else if (expected instanceof long[]) {
|
|
166 |
Assert.assertArrayEquals(message, (long[]) expected, (long[]) actual);
|
|
167 |
} else if (expected instanceof double[]) {
|
|
168 |
Assert.assertArrayEquals(message, (double[]) expected, (double[]) actual, delta);
|
|
169 |
} else if (expected instanceof boolean[]) {
|
|
170 |
new ExactComparisonCriteria().arrayEquals(message, expected, actual);
|
|
171 |
} else if (expected instanceof Object[]) {
|
|
172 |
new ComparisonCriteria() {
|
|
173 |
@Override
|
|
174 |
protected void assertElementsEqual(Object e, Object a) {
|
|
175 |
assertDeepEquals(message, e, a, delta);
|
|
176 |
}
|
|
177 |
}.arrayEquals(message, expected, actual);
|
|
178 |
} else {
|
|
179 |
Assert.fail((message == null ? "" : message) + "non-array value encountered: " + expected);
|
|
180 |
}
|
|
181 |
} else if (expectedClass.equals(double.class) && actualClass.equals(double.class)) {
|
|
182 |
Assert.assertEquals((double) expected, (double) actual, delta);
|
|
183 |
} else if (expectedClass.equals(float.class) && actualClass.equals(float.class)) {
|
|
184 |
Assert.assertEquals((float) expected, (float) actual, delta);
|
|
185 |
} else {
|
|
186 |
Assert.assertEquals(message, expected, actual);
|
|
187 |
}
|
|
188 |
} else {
|
|
189 |
Assert.assertEquals(message, expected, actual);
|
|
190 |
}
|
|
191 |
}
|
|
192 |
|
|
193 |
/**
|
|
194 |
* Compares two given values for equality, doing a recursive test if both values are arrays of
|
|
195 |
* the same type. Uses {@linkplain StrictMath#ulp(float) ULP}s for comparison of floats.
|
|
196 |
*
|
|
197 |
* @param message the identifying message for the {@link AssertionError}
|
|
198 |
* @param ulpsDelta the maximum allowed ulps difference between two doubles or floats for which
|
|
199 |
* both numbers are still considered equal.
|
|
200 |
*/
|
|
201 |
protected void assertDeepEquals(String message, Object expected, Object actual, int ulpsDelta) {
|
|
202 |
ComparisonCriteria doubleUlpsDeltaCriteria = new ComparisonCriteria() {
|
|
203 |
@Override
|
|
204 |
protected void assertElementsEqual(Object e, Object a) {
|
|
205 |
assertTrue(message, e instanceof Double && a instanceof Double);
|
|
206 |
// determine acceptable error based on whether it is a normal number or a NaN/Inf
|
|
207 |
double de = (Double) e;
|
|
208 |
double epsilon = (!Double.isNaN(de) && Double.isFinite(de) ? ulpsDelta * Math.ulp(de) : 0);
|
|
209 |
Assert.assertEquals(message, (Double) e, (Double) a, epsilon);
|
|
210 |
}
|
|
211 |
};
|
|
212 |
|
|
213 |
ComparisonCriteria floatUlpsDeltaCriteria = new ComparisonCriteria() {
|
|
214 |
@Override
|
|
215 |
protected void assertElementsEqual(Object e, Object a) {
|
|
216 |
assertTrue(message, e instanceof Float && a instanceof Float);
|
|
217 |
// determine acceptable error based on whether it is a normal number or a NaN/Inf
|
|
218 |
float fe = (Float) e;
|
|
219 |
float epsilon = (!Float.isNaN(fe) && Float.isFinite(fe) ? ulpsDelta * Math.ulp(fe) : 0);
|
|
220 |
Assert.assertEquals(message, (Float) e, (Float) a, epsilon);
|
|
221 |
}
|
|
222 |
};
|
|
223 |
|
|
224 |
if (expected != null && actual != null) {
|
|
225 |
Class<?> expectedClass = expected.getClass();
|
|
226 |
Class<?> actualClass = actual.getClass();
|
|
227 |
if (expectedClass.isArray()) {
|
|
228 |
Assert.assertEquals(message, expectedClass, actualClass);
|
|
229 |
if (expected instanceof double[] || expected instanceof Object[]) {
|
|
230 |
doubleUlpsDeltaCriteria.arrayEquals(message, expected, actual);
|
|
231 |
return;
|
|
232 |
} else if (expected instanceof float[] || expected instanceof Object[]) {
|
|
233 |
floatUlpsDeltaCriteria.arrayEquals(message, expected, actual);
|
|
234 |
return;
|
|
235 |
}
|
|
236 |
} else if (expectedClass.equals(double.class) && actualClass.equals(double.class)) {
|
|
237 |
doubleUlpsDeltaCriteria.arrayEquals(message, expected, actual);
|
|
238 |
return;
|
|
239 |
} else if (expectedClass.equals(float.class) && actualClass.equals(float.class)) {
|
|
240 |
floatUlpsDeltaCriteria.arrayEquals(message, expected, actual);
|
|
241 |
return;
|
|
242 |
}
|
|
243 |
}
|
|
244 |
// anything else just use the non-ulps version
|
|
245 |
assertDeepEquals(message, expected, actual, equalFloatsOrDoublesDelta());
|
|
246 |
}
|
|
247 |
|
52910
|
248 |
/**
|
|
249 |
* @see "https://bugs.openjdk.java.net/browse/JDK-8076557"
|
|
250 |
*/
|
|
251 |
public static void assumeManagementLibraryIsLoadable() {
|
52578
|
252 |
try {
|
|
253 |
/* Trigger loading of the management library using the bootstrap class loader. */
|
|
254 |
GraalServices.getCurrentThreadAllocatedBytes();
|
|
255 |
} catch (UnsatisfiedLinkError | NoClassDefFoundError | UnsupportedOperationException e) {
|
|
256 |
throw new AssumptionViolatedException("Management interface is unavailable: " + e);
|
|
257 |
}
|
|
258 |
}
|
|
259 |
|
43972
|
260 |
/**
|
|
261 |
* Gets the value used by {@link #assertDeepEquals(Object, Object)} and
|
|
262 |
* {@link #assertDeepEquals(String, Object, Object)} for the maximum delta between two doubles
|
|
263 |
* or floats for which both numbers are still considered equal.
|
|
264 |
*/
|
|
265 |
protected double equalFloatsOrDoublesDelta() {
|
|
266 |
return 0.0D;
|
|
267 |
}
|
|
268 |
|
|
269 |
// unless overridden ulpsDelta is not used
|
|
270 |
protected int ulpsDelta() {
|
|
271 |
return 0;
|
|
272 |
}
|
|
273 |
|
|
274 |
@SuppressWarnings("serial")
|
|
275 |
public static class MultiCauseAssertionError extends AssertionError {
|
|
276 |
|
|
277 |
private Throwable[] causes;
|
|
278 |
|
|
279 |
public MultiCauseAssertionError(String message, Throwable... causes) {
|
|
280 |
super(message);
|
|
281 |
this.causes = causes;
|
|
282 |
}
|
|
283 |
|
|
284 |
@Override
|
|
285 |
public void printStackTrace(PrintStream out) {
|
|
286 |
super.printStackTrace(out);
|
|
287 |
int num = 0;
|
|
288 |
for (Throwable cause : causes) {
|
|
289 |
if (cause != null) {
|
|
290 |
out.print("cause " + (num++));
|
|
291 |
cause.printStackTrace(out);
|
|
292 |
}
|
|
293 |
}
|
|
294 |
}
|
|
295 |
|
|
296 |
@Override
|
|
297 |
public void printStackTrace(PrintWriter out) {
|
|
298 |
super.printStackTrace(out);
|
|
299 |
int num = 0;
|
|
300 |
for (Throwable cause : causes) {
|
|
301 |
if (cause != null) {
|
|
302 |
out.print("cause " + (num++) + ": ");
|
|
303 |
cause.printStackTrace(out);
|
|
304 |
}
|
|
305 |
}
|
|
306 |
}
|
|
307 |
}
|
|
308 |
|
|
309 |
/*
|
|
310 |
* Overrides to the normal JUnit {@link Assert} routines that provide varargs style formatting
|
|
311 |
* and produce an exception stack trace with the assertion frames trimmed out.
|
|
312 |
*/
|
|
313 |
|
|
314 |
/**
|
|
315 |
* Fails a test with the given message.
|
|
316 |
*
|
|
317 |
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
|
|
318 |
* okay)
|
|
319 |
* @see AssertionError
|
|
320 |
*/
|
|
321 |
public static void fail(String message, Object... objects) {
|
|
322 |
AssertionError e;
|
|
323 |
if (message == null) {
|
|
324 |
e = new AssertionError();
|
|
325 |
} else {
|
|
326 |
e = new AssertionError(String.format(message, objects));
|
|
327 |
}
|
|
328 |
// Trim the assert frames from the stack trace
|
|
329 |
StackTraceElement[] trace = e.getStackTrace();
|
|
330 |
int start = 1; // Skip this frame
|
|
331 |
String thisClassName = GraalTest.class.getName();
|
|
332 |
while (start < trace.length && trace[start].getClassName().equals(thisClassName) && (trace[start].getMethodName().equals("assertTrue") || trace[start].getMethodName().equals("assertFalse"))) {
|
|
333 |
start++;
|
|
334 |
}
|
|
335 |
e.setStackTrace(Arrays.copyOfRange(trace, start, trace.length));
|
|
336 |
throw e;
|
|
337 |
}
|
|
338 |
|
|
339 |
/**
|
|
340 |
* Asserts that a condition is true. If it isn't it throws an {@link AssertionError} with the
|
|
341 |
* given message.
|
|
342 |
*
|
|
343 |
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
|
|
344 |
* okay)
|
|
345 |
* @param condition condition to be checked
|
|
346 |
*/
|
|
347 |
public static void assertTrue(String message, boolean condition) {
|
|
348 |
assertTrue(condition, message);
|
|
349 |
}
|
|
350 |
|
|
351 |
/**
|
|
352 |
* Asserts that a condition is true. If it isn't it throws an {@link AssertionError} without a
|
|
353 |
* message.
|
|
354 |
*
|
|
355 |
* @param condition condition to be checked
|
|
356 |
*/
|
|
357 |
public static void assertTrue(boolean condition) {
|
|
358 |
assertTrue(condition, null);
|
|
359 |
}
|
|
360 |
|
|
361 |
/**
|
|
362 |
* Asserts that a condition is false. If it isn't it throws an {@link AssertionError} with the
|
|
363 |
* given message.
|
|
364 |
*
|
|
365 |
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
|
|
366 |
* okay)
|
|
367 |
* @param condition condition to be checked
|
|
368 |
*/
|
|
369 |
public static void assertFalse(String message, boolean condition) {
|
|
370 |
assertTrue(!condition, message);
|
|
371 |
}
|
|
372 |
|
|
373 |
/**
|
|
374 |
* Asserts that a condition is false. If it isn't it throws an {@link AssertionError} without a
|
|
375 |
* message.
|
|
376 |
*
|
|
377 |
* @param condition condition to be checked
|
|
378 |
*/
|
|
379 |
public static void assertFalse(boolean condition) {
|
|
380 |
assertTrue(!condition, null);
|
|
381 |
}
|
|
382 |
|
|
383 |
/**
|
|
384 |
* Asserts that a condition is true. If it isn't it throws an {@link AssertionError} with the
|
|
385 |
* given message.
|
|
386 |
*
|
|
387 |
* @param condition condition to be checked
|
|
388 |
* @param message the identifying message for the {@link AssertionError}
|
|
389 |
* @param objects arguments to the format string
|
|
390 |
*/
|
|
391 |
public static void assertTrue(boolean condition, String message, Object... objects) {
|
|
392 |
if (!condition) {
|
|
393 |
fail(message, objects);
|
|
394 |
}
|
|
395 |
}
|
|
396 |
|
|
397 |
/**
|
|
398 |
* Asserts that a condition is false. If it isn't it throws an {@link AssertionError} with the
|
|
399 |
* given message produced by {@link String#format}.
|
|
400 |
*
|
|
401 |
* @param condition condition to be checked
|
|
402 |
* @param message the identifying message for the {@link AssertionError}
|
|
403 |
* @param objects arguments to the format string
|
|
404 |
*/
|
|
405 |
public static void assertFalse(boolean condition, String message, Object... objects) {
|
|
406 |
assertTrue(!condition, message, objects);
|
|
407 |
}
|
46640
|
408 |
|
|
409 |
/**
|
|
410 |
* Gets the {@link DebugHandlersFactory}s available for a {@link DebugContext}.
|
|
411 |
*/
|
|
412 |
protected Collection<DebugHandlersFactory> getDebugHandlersFactories() {
|
|
413 |
return Collections.emptyList();
|
|
414 |
}
|
|
415 |
|
|
416 |
/**
|
|
417 |
* Gets a {@link DebugContext} object corresponding to {@code options}, creating a new one if
|
|
418 |
* none currently exists. Debug contexts created by this method will have their
|
|
419 |
* {@link DebugDumpHandler}s closed in {@link #afterTest()}.
|
|
420 |
*/
|
|
421 |
protected DebugContext getDebugContext(OptionValues options) {
|
47798
|
422 |
return getDebugContext(options, null, null);
|
|
423 |
}
|
|
424 |
|
|
425 |
/**
|
|
426 |
* Gets a {@link DebugContext} object corresponding to {@code options}, creating a new one if
|
48861
|
427 |
* none currently exists. Debug contexts created by this method will have their
|
47798
|
428 |
* {@link DebugDumpHandler}s closed in {@link #afterTest()}.
|
|
429 |
*
|
|
430 |
* @param options currently active options
|
|
431 |
* @param id identification of the compilation or {@code null}
|
|
432 |
* @param method method to use for a proper description of the context or {@code null}
|
|
433 |
* @return configured context for compilation
|
|
434 |
*/
|
|
435 |
protected DebugContext getDebugContext(OptionValues options, String id, ResolvedJavaMethod method) {
|
46640
|
436 |
List<DebugContext> cached = cachedDebugs.get();
|
|
437 |
if (cached == null) {
|
|
438 |
cached = new ArrayList<>();
|
|
439 |
cachedDebugs.set(cached);
|
|
440 |
}
|
|
441 |
for (DebugContext debug : cached) {
|
|
442 |
if (debug.getOptions() == options) {
|
|
443 |
return debug;
|
|
444 |
}
|
|
445 |
}
|
47798
|
446 |
final DebugContext.Description descr;
|
|
447 |
if (method == null) {
|
|
448 |
descr = NO_DESCRIPTION;
|
|
449 |
} else {
|
|
450 |
descr = new DebugContext.Description(method, id == null ? method.getName() : id);
|
|
451 |
}
|
48861
|
452 |
DebugContext debug = DebugContext.create(options, descr, globalMetrics, DEFAULT_LOG_STREAM, getDebugHandlersFactories());
|
46640
|
453 |
cached.add(debug);
|
|
454 |
return debug;
|
|
455 |
}
|
|
456 |
|
48861
|
457 |
private static final GlobalMetrics globalMetrics = new GlobalMetrics();
|
|
458 |
|
|
459 |
static {
|
|
460 |
Runtime.getRuntime().addShutdownHook(new Thread("GlobalMetricsPrinter") {
|
|
461 |
@Override
|
|
462 |
public void run() {
|
58299
|
463 |
// globalMetrics.print(new OptionValues(OptionValues.newOptionMap()));
|
48861
|
464 |
}
|
|
465 |
});
|
|
466 |
}
|
46640
|
467 |
private final ThreadLocal<List<DebugContext>> cachedDebugs = new ThreadLocal<>();
|
|
468 |
|
|
469 |
@After
|
|
470 |
public void afterTest() {
|
|
471 |
List<DebugContext> cached = cachedDebugs.get();
|
|
472 |
if (cached != null) {
|
|
473 |
for (DebugContext debug : cached) {
|
48861
|
474 |
debug.close();
|
46640
|
475 |
debug.closeDumpHandlers(true);
|
|
476 |
}
|
|
477 |
}
|
|
478 |
}
|
51436
|
479 |
|
|
480 |
private static final double TIMEOUT_SCALING_FACTOR = Double.parseDouble(System.getProperty("graaltest.timeout.factor", "1.0"));
|
|
481 |
|
|
482 |
/**
|
|
483 |
* Creates a {@link TestRule} that applies a given timeout.
|
|
484 |
*
|
|
485 |
* A test harness can scale {@code length} with a factor specified by the
|
|
486 |
* {@code graaltest.timeout.factor} system property.
|
|
487 |
*/
|
|
488 |
public static TestRule createTimeout(long length, TimeUnit timeUnit) {
|
|
489 |
Timeout timeout = new Timeout((long) (length * TIMEOUT_SCALING_FACTOR), timeUnit);
|
|
490 |
try {
|
|
491 |
return new DisableOnDebug(timeout);
|
|
492 |
} catch (LinkageError ex) {
|
|
493 |
return timeout;
|
|
494 |
}
|
|
495 |
}
|
|
496 |
|
|
497 |
/**
|
|
498 |
* @see #createTimeout
|
|
499 |
*/
|
|
500 |
public static TestRule createTimeoutSeconds(int seconds) {
|
|
501 |
return createTimeout(seconds, TimeUnit.SECONDS);
|
|
502 |
}
|
|
503 |
|
|
504 |
/**
|
|
505 |
* @see #createTimeout
|
|
506 |
*/
|
|
507 |
public static TestRule createTimeoutMillis(long milliseconds) {
|
|
508 |
return createTimeout(milliseconds, TimeUnit.MILLISECONDS);
|
|
509 |
}
|
57537
|
510 |
|
58299
|
511 |
public static class TemporaryDirectory implements AutoCloseable {
|
|
512 |
|
|
513 |
public final Path path;
|
|
514 |
private IOException closeException;
|
|
515 |
|
|
516 |
public TemporaryDirectory(Path dir, String prefix, FileAttribute<?>... attrs) throws IOException {
|
|
517 |
path = Files.createTempDirectory(dir == null ? Paths.get(".") : dir, prefix, attrs);
|
|
518 |
}
|
|
519 |
|
|
520 |
@Override
|
|
521 |
public void close() {
|
|
522 |
closeException = removeDirectory(path);
|
|
523 |
}
|
|
524 |
|
|
525 |
public IOException getCloseException() {
|
|
526 |
return closeException;
|
|
527 |
}
|
|
528 |
|
|
529 |
@Override
|
|
530 |
public String toString() {
|
|
531 |
return path.toString();
|
|
532 |
}
|
|
533 |
}
|
|
534 |
|
57537
|
535 |
/**
|
|
536 |
* Tries to recursively remove {@code directory}. If it fails with an {@link IOException}, the
|
|
537 |
* exception's {@code toString()} is printed to {@link System#err} and the exception is
|
|
538 |
* returned.
|
|
539 |
*/
|
|
540 |
public static IOException removeDirectory(Path directory) {
|
|
541 |
try {
|
|
542 |
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
|
|
543 |
@Override
|
|
544 |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
|
545 |
Files.delete(file);
|
|
546 |
return FileVisitResult.CONTINUE;
|
|
547 |
}
|
|
548 |
|
|
549 |
@Override
|
|
550 |
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
|
551 |
Files.delete(dir);
|
|
552 |
return FileVisitResult.CONTINUE;
|
|
553 |
}
|
|
554 |
});
|
|
555 |
} catch (IOException e) {
|
|
556 |
System.err.println(e);
|
|
557 |
return e;
|
|
558 |
}
|
|
559 |
return null;
|
|
560 |
}
|
43972
|
561 |
}
|