test/tools/javac/lib/JavacTestingAbstractThreadedTest.java

Thu, 10 Jan 2013 19:38:57 -0800

author
jjg
date
Thu, 10 Jan 2013 19:38:57 -0800
changeset 1490
fc4cb1577ad6
parent 1482
954541f13717
child 1520
5c956be64b9e
permissions
-rw-r--r--

8004834: Add doclint support into javadoc
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 2010, 2013, 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  */
    24 import java.io.PrintWriter;
    25 import java.io.StringWriter;
    26 import java.util.concurrent.ExecutorService;
    27 import java.util.concurrent.Executors;
    28 import java.util.concurrent.ThreadFactory;
    29 import java.util.concurrent.atomic.AtomicInteger;
    30 import javax.tools.JavaCompiler;
    31 import javax.tools.StandardJavaFileManager;
    32 import javax.tools.ToolProvider;
    34 /**
    35  * An abstract superclass for threaded tests.
    36  *
    37  * This class will try to read a property named test.concurrency.
    38  * The property can be provided by passing this option to jtreg:
    39  * -javaoption:-Dtest.concurrency=#
    40  *
    41  * If the property is not set the class will use a heuristic to determine the
    42  * maximum number of threads that can be fired to execute a given test.
    43  */
    44 public abstract class JavacTestingAbstractThreadedTest {
    46     protected static int getThreadPoolSize() {
    47         Integer testConc = Integer.getInteger("test.concurrency");
    48         if (testConc != null) return testConc;
    49         int cores = Runtime.getRuntime().availableProcessors();
    50         return Math.max(2, Math.min(8, cores / 2));
    51     }
    53     protected static void checkAfterExec() throws InterruptedException {
    54         checkAfterExec(true);
    55     };
    57     protected static boolean throwAssertionOnError = true;
    59     protected static boolean printAll = false;
    61     protected static StringWriter errSWriter = new StringWriter();
    62     protected static PrintWriter errWriter = new PrintWriter(errSWriter);
    64     protected static StringWriter outSWriter = new StringWriter();
    65     protected static PrintWriter outWriter = new PrintWriter(outSWriter);
    67     protected static void checkAfterExec(boolean printCheckCount)
    68             throws InterruptedException {
    69         pool.shutdown();
    70         while (!pool.isTerminated()) {
    71             Thread.sleep(10);
    72         }
    73         if (errCount.get() > 0) {
    74             if (throwAssertionOnError) {
    75                 closePrinters();
    76                 System.err.println(errSWriter.toString());
    77                 throw new AssertionError(
    78                     String.format("%d errors found", errCount.get()));
    79             } else {
    80                 System.err.println(
    81                         String.format("%d errors found", errCount.get()));
    82             }
    83         } else if (printCheckCount) {
    84             outWriter.println("Total check executed: " + checkCount.get());
    85         }
    86         closePrinters();
    87         if (printAll) {
    88             System.out.println(errSWriter.toString());
    89             System.out.println(outSWriter.toString());
    90         }
    91     }
    93     protected static void closePrinters() {
    94         errWriter.close();
    95         outWriter.close();
    96     }
    98     protected static void processException(Throwable t) {
    99         errCount.incrementAndGet();
   100         t.printStackTrace(errWriter);
   101         pool.shutdown();
   102     }
   104     //number of checks executed
   105     protected static AtomicInteger checkCount = new AtomicInteger();
   107     //number of errors found while running combo tests
   108     protected static AtomicInteger errCount = new AtomicInteger();
   110     //create default shared JavaCompiler - reused across multiple compilations
   111     protected static JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
   113     protected static ExecutorService pool = Executors.newFixedThreadPool(
   114             getThreadPoolSize(), new ThreadFactory() {
   115         @Override
   116         public Thread newThread(Runnable r) {
   117             Thread t = new Thread(r);
   118             t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
   119                 @Override
   120                 public void uncaughtException(Thread t, Throwable e) {
   121                     pool.shutdown();
   122                     errCount.incrementAndGet();
   123                     e.printStackTrace(System.err);
   124                 }
   125             });
   126             return t;
   127         }
   128     });
   130     /*
   131      * File manager is not thread-safe so it cannot be re-used across multiple
   132      * threads. However we cache per-thread FileManager to avoid excessive
   133      * object creation
   134      */
   135     protected static final ThreadLocal<StandardJavaFileManager> fm =
   136         new ThreadLocal<StandardJavaFileManager>() {
   137             @Override protected StandardJavaFileManager initialValue() {
   138                 return comp.getStandardFileManager(null, null, null);
   139             }
   140         };
   142 }

mercurial