1 /*
2 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
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 */
23
24
25 package org.graalvm.compiler.test;
26
27 import static org.graalvm.compiler.debug.DebugContext.DEFAULT_LOG_STREAM;
28 import static org.graalvm.compiler.debug.DebugContext.NO_DESCRIPTION;
29
30 import java.io.IOException;
31 import java.io.PrintStream;
32 import java.io.PrintWriter;
33 import java.lang.reflect.Field;
34 import java.lang.reflect.Method;
35 import java.nio.file.FileVisitResult;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.SimpleFileVisitor;
39 import java.nio.file.attribute.BasicFileAttributes;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collection;
43 import java.util.Collections;
44 import java.util.List;
45 import java.util.concurrent.TimeUnit;
46
47 import org.graalvm.compiler.debug.DebugContext;
48 import org.graalvm.compiler.debug.DebugDumpHandler;
49 import org.graalvm.compiler.debug.DebugHandlersFactory;
50 import org.graalvm.compiler.debug.GlobalMetrics;
51 import org.graalvm.compiler.options.OptionValues;
52 import org.graalvm.compiler.serviceprovider.GraalServices;
53 import org.junit.After;
54 import org.junit.Assert;
55 import org.junit.AssumptionViolatedException;
56 import org.junit.internal.ComparisonCriteria;
57 import org.junit.internal.ExactComparisonCriteria;
58 import org.junit.rules.DisableOnDebug;
59 import org.junit.rules.TestRule;
441 return debug;
442 }
443 }
444 final DebugContext.Description descr;
445 if (method == null) {
446 descr = NO_DESCRIPTION;
447 } else {
448 descr = new DebugContext.Description(method, id == null ? method.getName() : id);
449 }
450 DebugContext debug = DebugContext.create(options, descr, globalMetrics, DEFAULT_LOG_STREAM, getDebugHandlersFactories());
451 cached.add(debug);
452 return debug;
453 }
454
455 private static final GlobalMetrics globalMetrics = new GlobalMetrics();
456
457 static {
458 Runtime.getRuntime().addShutdownHook(new Thread("GlobalMetricsPrinter") {
459 @Override
460 public void run() {
461 globalMetrics.print(new OptionValues(OptionValues.newOptionMap()));
462 }
463 });
464 }
465 private final ThreadLocal<List<DebugContext>> cachedDebugs = new ThreadLocal<>();
466
467 @After
468 public void afterTest() {
469 List<DebugContext> cached = cachedDebugs.get();
470 if (cached != null) {
471 for (DebugContext debug : cached) {
472 debug.close();
473 debug.closeDumpHandlers(true);
474 }
475 }
476 }
477
478 private static final double TIMEOUT_SCALING_FACTOR = Double.parseDouble(System.getProperty("graaltest.timeout.factor", "1.0"));
479
480 /**
481 * Creates a {@link TestRule} that applies a given timeout.
487 Timeout timeout = new Timeout((long) (length * TIMEOUT_SCALING_FACTOR), timeUnit);
488 try {
489 return new DisableOnDebug(timeout);
490 } catch (LinkageError ex) {
491 return timeout;
492 }
493 }
494
495 /**
496 * @see #createTimeout
497 */
498 public static TestRule createTimeoutSeconds(int seconds) {
499 return createTimeout(seconds, TimeUnit.SECONDS);
500 }
501
502 /**
503 * @see #createTimeout
504 */
505 public static TestRule createTimeoutMillis(long milliseconds) {
506 return createTimeout(milliseconds, TimeUnit.MILLISECONDS);
507 }
508
509 /**
510 * Tries to recursively remove {@code directory}. If it fails with an {@link IOException}, the
511 * exception's {@code toString()} is printed to {@link System#err} and the exception is
512 * returned.
513 */
514 public static IOException removeDirectory(Path directory) {
515 try {
516 Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
517 @Override
518 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
519 Files.delete(file);
520 return FileVisitResult.CONTINUE;
521 }
522
523 @Override
524 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
525 Files.delete(dir);
526 return FileVisitResult.CONTINUE;
|
1 /*
2 * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
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 */
23
24
25 package org.graalvm.compiler.test;
26
27 import static org.graalvm.compiler.debug.DebugContext.DEFAULT_LOG_STREAM;
28 import static org.graalvm.compiler.debug.DebugContext.NO_DESCRIPTION;
29
30 import java.io.IOException;
31 import java.io.PrintStream;
32 import java.io.PrintWriter;
33 import java.lang.reflect.Field;
34 import java.lang.reflect.Method;
35 import java.nio.file.FileVisitResult;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.nio.file.SimpleFileVisitor;
40 import java.nio.file.attribute.BasicFileAttributes;
41 import java.nio.file.attribute.FileAttribute;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.List;
47 import java.util.concurrent.TimeUnit;
48
49 import org.graalvm.compiler.debug.DebugContext;
50 import org.graalvm.compiler.debug.DebugDumpHandler;
51 import org.graalvm.compiler.debug.DebugHandlersFactory;
52 import org.graalvm.compiler.debug.GlobalMetrics;
53 import org.graalvm.compiler.options.OptionValues;
54 import org.graalvm.compiler.serviceprovider.GraalServices;
55 import org.junit.After;
56 import org.junit.Assert;
57 import org.junit.AssumptionViolatedException;
58 import org.junit.internal.ComparisonCriteria;
59 import org.junit.internal.ExactComparisonCriteria;
60 import org.junit.rules.DisableOnDebug;
61 import org.junit.rules.TestRule;
443 return debug;
444 }
445 }
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 }
452 DebugContext debug = DebugContext.create(options, descr, globalMetrics, DEFAULT_LOG_STREAM, getDebugHandlersFactories());
453 cached.add(debug);
454 return debug;
455 }
456
457 private static final GlobalMetrics globalMetrics = new GlobalMetrics();
458
459 static {
460 Runtime.getRuntime().addShutdownHook(new Thread("GlobalMetricsPrinter") {
461 @Override
462 public void run() {
463 // globalMetrics.print(new OptionValues(OptionValues.newOptionMap()));
464 }
465 });
466 }
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) {
474 debug.close();
475 debug.closeDumpHandlers(true);
476 }
477 }
478 }
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.
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 }
510
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
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;
|